3

When we are injecting some class into another using constructor we are making this:

protected $someClass

public function __construct(SomeClass $someClass)
{
    $this->someClass = $someClass;
}

But what if we need to pass some argument into this SomeClass?

Magento has following implementation:

Magento\Catalog\Model\Layer\FilterList

$this->filters = [
    $this->objectManager->create($this->filterTypes[self::CATEGORY_FILTER], ['layer' => $layer]),
];

I wan't to inject my own filter into plugin and pass into it ['layer' => $layer]

Please advice.

1 Answer 1

3
<type name="Vendor\Module\Model\Layer\Filter\Example">
    <arguments>
        <argument name="layer" xsi:type="object">Vendor\Module\Model\Layer\Example</argument>
    </arguments>
</type>

This class Vendor\Module\Model\Layer\Filter\Example should extends Magento\Catalog\Model\Layer\Filter\AbstractFilter
You can see more here https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/Model/Layer/FilterList.php#L39
Class Vendor\Module\Model\Layer\Example should extends \Magento\Catalog\Model\Layer
So you can pass argument layer class to class Filter\Example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.