Magento 2 Arrays in Dependency Injection Context

Magento 2 Arrays in Dependency Injection Context

In a recent project the client asked us to remove a few of the defined dataproviders for the autocomplete functionality. The consolidated DI configuration of the autocomplete element looks like this:

<type name="Magento\Search\Model\Autocomplete">
    <argument name="dataProviders" xsi:type="array">
      <item name="10" xsi:type="object">
      <item name="20" xsi:type="object">
      <item name="30" xsi:type="object">
DataProvider </item> <item name="40" xsi:type="object"> Smile\ElasticsuiteCatalog\Model\Autocomplete\Category\DataProvider </item> <item name="50" xsi:type="object"> Smile\ElasticsuiteCms\Model\Autocomplete\Page\DataProvider </item> </argument> </arguments> </type>

I tried several ways to remove elements from the dataProviders array, but none of them worked. At first I tried to redefine the array entries in my module configuration and set the ones I'd like to disable to a type of null. The Magento ObjectManager seemed to ignore those settings and used the configuration above. As an alternative I thought I could provide a custom NullDataProvider which would return nothing and replace the dataproviders I'd like to disable with the NullDataProvider. That also did not work as only the first entry was changed to the NullDataProvider. All other entries were kept in the state as defined above.

The only solution that worked for me was to extend the Autocomplete element and filter out the items via code:

class Autocomplete extends \Magento\Search\Model\Autocomplete
     * @param array $dataProviders
    public function __construct(array $dataProviders)
        // Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\DataProvider
        // Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\Attribute\
        // DataProvider
        // Smile\ElasticsuiteCms\Model\Autocomplete\Page\DataProvider


Eintrag von Stephan Hochdörfer am 13.09.2018

Tags: Magento, Magento2, Dependency Injection

Diese Webseite verwendet Cookies, um die Bedienfreundlichkeit zu erhöhen. Mit der Nutzung unserer Webseite wird das Einverständnis erklärt, dass wir Cookies verwenden. Weitere Informationen.