Indexing
...
Entities
Custom Entity Types
Entity Discovery
2min
To sync any entities first we have to discover them. In order to discover entities for a new entity type we must inject an EntityDiscoveryProvider into the EntityDiscoveryOrchestratorService e.g.
Vendor/Module/etc/di.xml
1<type name="Klevu\Indexing\Service\EntityDiscoveryOrchestratorService">
2 <arguments>
3 <argument name="discoveryProviders" xsi:type="array">
4 <item name="custom_types"
5 xsi:type="object">Vendor\Module\Service\Provider\CustomTypeEntityDiscoveryProvider</item>
6 </argument>
7 </arguments>
8</type>
9
That CustomTypeEntityDiscoveryProvider will be a virtual type of Klevu\Indexing\Service\Provider\EntityDiscoveryProvider e.g.
Vendor/Module/etc/di.xml
1<virtualType name="Vendor\Module\Service\Provider\CustomTypeEntityDiscoveryProvider"
2 type="Klevu\Indexing\Service\Provider\EntityDiscoveryProvider">
3 <arguments>
4 <argument name="entityType" xsi:type="string">CUSTOM_TYPE</argument>
5 <argument name="entityProviders" xsi:type="array">
6 <item name="custom_type"
7 xsi:type="object">Vendor\Module\Service\Provider\CustomTypeEntityProvider</item>
8 </argument>
9 <argument name="isIndexableDeterminer"
10 xsi:type="object">Vendor\Module\Service\Determiner\IsIndexableDeterminer</argument>
11 </arguments>
12</virtualType>
13
While CUSTOM_TYPE can be any string as long as it does not clash with any existing types, we advise you do not begin the string with KLEVU_. If we add more types in the future and they will begin KLEVU_ which could cause a name clash.
Create the CustomTypeEntityProvider which must implement Klevu\IndexingApi\Service\Provider\EntityProviderInterface
PHP
1class CustomTypeEntityProvider implements EntityProviderInterface
2{
3 public function get(?StoreInterface $store = null, ?array $entityIds = []): ?\Generator
4 {
5 $customEntityCollection = $this->getCollection(store: $store, entityIds: $entityIds);
6 foreach ($customEntityCollection as $customEntity) {
7 yield $customEntity;
8 }
9 }
10
11 private function getCollection(?StoreInterface $store, ?array $entityIds): CategoryCollection
12 {
13 $customEntityCollection = $this->customEntityCollectionFactory->create();
14 if ($store) {
15 $customEntityCollection->setStore(store: (int)$store->getId());
16 }
17 if ($entityIds) {
18 $customEntityCollection->addFieldToFilter(
19 Entity::DEFAULT_ENTITY_ID_FIELD,
20 ['in' => implode(',', $entityIds)],
21 );
22 }
23
24 return $customEntityCollection;
25 }
26}
27
Updated 03 Jan 2025


Did this page help you?