Skip to main content

Magento 2 Arrays in Dependency Injection Context

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

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">
Smile\ElasticsuiteCatalog\Model\Autocomplete\Product\Attribute\ DataProvider
<item name="40" xsi:type="object">
<item name="50" xsi:type="object">

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