Sulu: custom data provider, enableTypes() parameters

325 Views Asked by At

I'm following official sulu documentation about creating custom data provider:

https://docs.sulu.io/en/2.2/cookbook/smart-content-data-provider.html#dataprovider

Here, inside ExampleDataProvider class, inside getConfiguration() method there is chunk of code:

        ->enableTypes([
            ['type' => 'example-type-1', 'title' => 'type-translation-key-1'],
            ['type' => 'example-type-2', 'title' => 'type-translation-key-2'],
        ])

And I for my repository class I'm using DataProviderRepositoryTrait trait, as suggested there.

What are those types? Can they be excluded. I assume they have something to do with filters of smart field type, but I don't understand how this works.

Update:

What the actual problem is - when I try to open edit page at back-end, where my smart type appears I get the error:

[Semantical Error] line 0, col 84 near 'type WHERE entity.id': Error: Class App\Entity\MatchEvent has no association named type

I tried just excluding the code that is adding enabled types, but it's the same.

By error, I would say that some sql query is still looking for some type parameters even I excluded enabled types, but I don't know where and how to fix this.

I found some similar issue here: How do i solve Semantical error: "Class has no association named.."

So if the problem is the same it means that field "type" should be added to my custom entity type. But it's not mentioned in this tutorial article?

I figured out that:

    public function appendJoins(QueryBuilder $queryBuilder, $alias, $locale)
    {
      $queryBuilder->addSelect('type')->leftJoin($alias . '.type', 'type');
    }

which I had to add inside my repository class when implementing DataProviderRepositoryInterface interface is adding this 'type' to query. If I comment out that line I don't get error any more, but my field never finish loading (spinner is running infinitely)

2

There are 2 best solutions below

4
On BEST ANSWER

Yes you are right, this is for the filtering by types. These types are e.g. the template-keys for pages and the title is for the translation in the admin ui.

The selected types can be used in your CustomDataProvider with the Sulu/Component/Content/SmartContent/QueryBuilder to filter the items by a specific type.

The DataProviderRepositoryTrait uses the type-filtering on the these lines. You can also overwrite the appendTypeRelation method, if the alias is not correct for your custom entity.

But if you don't need filtering by types, you can just ignore it.

0
On

Solved the problem. Thing is (if I figured it out well) that I have to either add field "type" to my custom entity (not mentioned in tutorial) or I have to comment out that one line in appendJoins() method so that field won't be added to sql query and cause the error.

Beside that I had some other issue, which caused my code not working even when that line is commented, but that's not relevant to this issue.