Question

I've created a simple CRUD module for Magento 2 and now I'm running the tests to see if I didn't break something.
Apparently there are a lot of things wrong with my module. I will be posting some questions about this in the near future.
First problem is that I get this error:

Data set: array (
  0 => '\\Sample\\News\\Ui\\Component\\Listing\\Column\\AuthorActions',
)
Incorrect argument sequence in class \Sample\News\Ui\Component\Listing\Column\AuthorActions in [BASE_DIR]/app/code/Sample/News/Ui/Component/Listing/Column/AuthorActions.php
Required: $context, $uiComponentFactory, $urlBuilder, $components, $data
Actual  : $urlBuilder, $context, $uiComponentFactory, $components, $data

My class constructor looks like this:

public function __construct(
    \Magento\Framework\UrlInterface $urlBuilder,
    \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
    \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
    array $components = [],
    array $data = []
)
{
    $this->_urlBuilder = $urlBuilder;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

Does this mean that for all my classes that extend an other class I have to put the constructor parameters in this order:

$requiredParentParam1,
$requiredParentParam2,
...
$requiredParentParamX,

$newParam1,
$newParam2,
....
$newParamY,

$optionalParentParam1,
...
$optionalParentParamZ

And what should I do with the optional parameters I add in my new class that are not in the parent class? Where should I place them?

Was it helpful?

Solution

I did not find this in the Magento 2 documentation, but when you extend a class with a contructor you must respect the parameters orders except for the optional parameters that must be kept as last.

So just try the following, it worked for me:

public function __construct(
    \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
    \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
    \Magento\Framework\UrlInterface $urlBuilder,
    array $components = [],
    array $data = []
)
{
    $this->_urlBuilder = $urlBuilder;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

If you need additional optional parameters put them at the end:

public function __construct(
    \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
    \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
    \Magento\Framework\UrlInterface $urlBuilder,
    array $components = [],
    array $data = [],
    array $myCustomData = []
)
{
    $this->_urlBuilder = $urlBuilder;
    parent::__construct($context, $uiComponentFactory, $components, $data);
}

Have a look in:

Magento\Framework\Code\Validator\ArgumentSequence::validate()
Magento\Framework\Code\Validator\ArgumentSequence::_buildsSequence()
Magento\Framework\Code\Validator\ArgumentSequence::_checkArgumentSequence()

In Magento 2 your parameters should not be order dependent, but reading the code, expecially in _checkArgumentSequence, I assume this is not true for extended classes.

I mean: They should work even if not in order, but they are not validated. I think this is done for some kind of compatibility for future releases (just my guess).

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top