Creating or replacing service with tag

Hello everyone,

I have one question about creating or replacing services in custom plugins. More precisely, I’m going to replace service “shopware_searchdbal.property_facet_handler_dbal”, service definition in shopware core looks like:

My service definition in my custom plugin looks like:

    public function replaceSearchdbalPropertyFacetHandlerDbalService()
    {
        $shopwareStorefrontPropertyGatewayService = Shopware()->Container()->get('shopware_storefront.property_gateway');
        $dbalQueryBuilderFactoryService = Shopware()->Container()->get('shopware_searchdbal.dbal_query_builder_factory');
        $queryAliasMapperService = Shopware()->Container()->get('query_alias_mapper');

        Shopware()->Container()->set(
            'shopware_searchdbal.property_facet_handler_dbal',
            new AmsPropertyFacetHandler(
                $shopwareStorefrontPropertyGatewayService,
                $dbalQueryBuilderFactoryService,
                $queryAliasMapperService
            )
        );
    }

my question is how to add tag in plugin service definition?

Thanks in advice.

Whoever is able to help?

I can help you, but what excatly do you want to do? To create your own service for some events or to decorate shopwares existing event?

I want to extend “PropertyFacetHandler” because I would like filters that have numerical values to be presented as sliders. I modified method (createCollectionResult)  from my custom PropertyFacetHandler:

                if ($useSlider) {

                    list($min, $max, $activeMin, $activeMax, $minField, $maxField) = $this->getStats($group, $criteria);

                    $results[] = new RangeFacetResult(
                        $facet->getName(),
                        $criteria->hasCondition($facet->getName()),
                        $group->getName(),
                        (float) $min,
                        (float) $max,
                        (float) $activeMin,
                        (float) $activeMax,
                        $minField,
                        $maxField,
                        [],
                        null,
                        0,
                        'frontend/listing/filter/facet-range.tpl'
                    );
                } elseif ($useMedia) {
                    $results[] = new MediaListFacetResult(
                        $facet->getName(),
                        $isActive,
                        $group->getName(),
                        $items,
                        $this->fieldName,
                        $group->getAttributes()
                    );
                } else {
                    $results[] = new ValueListFacetResult(
                        $facet->getName(),
                        $isActive,
                        $group->getName(),
                        $items,
                        $this->fieldName,
                        $group->getAttributes()
                    );
                }

And that all I want to modify. To do this, I need to overwrite the service:

shopware_searchdbal.property_facet_handler_dbal

But I don’t know how to add tag to service in plugin configuration.

In that case you have to decorate shopwares event.

Create custom service in services.xml, for example:
 

 

  • Then create class (SearchDbal) in “CustomPlugin/Events”
  • Create public method in that class (propertyFacetHandlerDbal)

In that method you have to get shopwares service id, and put it into the new class and then replace that class with the old one by returnin it into the service id
There is an example in the shopwares documentation about decorating an service, just go to the line where the title is “Decorate the WhiteListService