Search code examples
shopware6

Admin panel search for custom entity not working


I am trying to add a search in the admin panel for a custom module with a custom entity. I am using this tutorial: https://developer.shopware.com/docs/guides/plugins/plugins/administration/search-custom-data#potential-pitfalls.

While the whole plugin works fine, I cannot understand why the search itself does not.

According to the mentioned tutorial, I added the shopware.composite_search.definition tag to the entity definition. Then I used the addServiceProviderDecorator method on the searchTypeService component and that worked as well. In the panel, the ability to select my module in the search appeared. But as soon as I try to use it I get the following error:

Uncaught (in promise) Error: sw-search-bar - Api service XXX not found

If I understood the tutorial correctly then this service should be available automatically right? Or when it comes to custom solutions it doesn't work like that?

I checked in the console what services are available there and after calling console.log(Shopware.ApiService.getServices()) I actually don't have this service or any related to my plugin. Does this mean that I have to create it from scratch manually? If so, how do I understand the description here https://developer.shopware.com/docs/guides/plugins/plugins/administration/search-custom-data#add-search-tag? In my opinion, it suggests that such a service is automatically available.


Solution

  • You can omit the entityService property entirely. The documentation fails to mention that it is optional. If you wanted to alter the default behavior how search results are loaded, you could write your own custom service to do so.

    // src/Administration/Resources/app/administration/src/app/component/structure/sw-search-bar/index.js
    // ...
    
    // If searchType has an "entityService" load by service, otherwise load by entity
    if (this.searchTypes[this.currentSearchType]?.entityService) {
        this.loadTypeSearchResultsByService(searchTerm);
        return;
    }
    

    What the documentation also fails to mention, is that fields that should be searchable, must be flagged accordingly in your entity definition.

    (new TranslatedField('name'))->addFlags(new SearchRanking(SearchRanking::HIGH_SEARCH_RANKING))