---
title: Entity Discovery
slug: klevu-magento-v4/custom-entities-discovery
docTags: 
createdAt: 2024-08-16T07:31:47.634Z
---

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`&#x20;
e.g.

:::CodeblockTabs
Vendor/Module/etc/di.xml

```xml
<type name="Klevu\Indexing\Service\EntityDiscoveryOrchestratorService">
    <arguments>
        <argument name="discoveryProviders" xsi:type="array">
            <item name="custom_types"
                  xsi:type="object">Vendor\Module\Service\Provider\CustomTypeEntityDiscoveryProvider</item>
        </argument>
    </arguments>
</type>

```
:::

That `CustomTypeEntityDiscoveryProvider` will be a virtual type of `Klevu\Indexing\Service\Provider\EntityDiscoveryProvider`
e.g.

:::CodeblockTabs
Vendor/Module/etc/di.xml

```xml
<virtualType name="Vendor\Module\Service\Provider\CustomTypeEntityDiscoveryProvider"
             type="Klevu\Indexing\Service\Provider\EntityDiscoveryProvider">
    <arguments>
        <argument name="entityType" xsi:type="string">CUSTOM_TYPE</argument>
        <argument name="entityProviders" xsi:type="array">
            <item name="custom_type"
                  xsi:type="object">Vendor\Module\Service\Provider\CustomTypeEntityProvider</item>
        </argument>
        <argument name="isIndexableDeterminer"
                  xsi:type="object">Vendor\Module\Service\Determiner\IsIndexableDeterminer</argument>
    </arguments>
</virtualType>

```
:::

:::hint{type="warning"}
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
class CustomTypeEntityProvider implements EntityProviderInterface
{
    public function get(?StoreInterface $store = null, ?array $entityIds = []): ?\Generator
    {
        $customEntityCollection = $this->getCollection(store: $store, entityIds: $entityIds);
        foreach ($customEntityCollection as $customEntity) {
            yield $customEntity;
        }
    }
    
    private function getCollection(?StoreInterface $store, ?array $entityIds): CategoryCollection
    {
        $customEntityCollection = $this->customEntityCollectionFactory->create();
        if ($store) {
            $customEntityCollection->setStore(store: (int)$store->getId());
        }
        if ($entityIds) {
            $customEntityCollection->addFieldToFilter(
                Entity::DEFAULT_ENTITY_ID_FIELD,
                ['in' => implode(',', $entityIds)],
            );
        }

        return $customEntityCollection;
    }
}

```

