Sulu: how to query custom entity type?

408 Views Asked by At

I have defined custom entity type "matches" and it is back-end editable, everything works as expected.

Now I need a way to query content in that type. Can smart content field type do that? I expected that xml like this would do the trick:

   <property name="matches" type="smart_content">
        <!-- @see https://docs.sulu.io/en/2.2/reference/content-types/smart_content.html -->
        <meta>
            <title lang="en">Matches</title>
            <title lang="de">Streichhölzer</title>
        </meta>

        <params>
            <param name="provider" value="lists"/>
            <param name="types" value="matchevents"/>
            <param name="max_per_page" value="5"/>
            <param name="page_parameter" value="m"/>
        </params>
    </property>

But that didn't help. Can it be done with "smart_content" field type? I yes - how? If no - what would be the best way to achieve that?

1

There are 1 best solutions below

4
On BEST ANSWER

you have to create a custom DataProvider for your entity and then use your provider in the xml-definition.

So you have to create a repository which implements the DataProviderRepositoryInterface. This repository is used to query the entity. Furthermore you also have to create the actual dataprovider service. This class should extend form the BaseDataProvider and finally you have to make the service definition with the tag sulu.smart_content.data_provider and an alias.

The alias is used in the xml-file e.g.

   <property name="matches" type="smart_content">
    <meta>
        <title lang="en">Matches</title>
        <title lang="de">Streichhölzer</title>
    </meta>

    <params>
        <param name="provider" value="myCustomDataProviderAlias"/>
    </params>
</property>

Check out the documentation for a step by step tutorial on how to create a custom data provider. https://docs.sulu.io/en/2.2/cookbook/smart-content-data-provider.html#how-to-create-a-custom-dataprovider