2

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?

0

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).

  • That was my problem. Nothing in the documentation. But my code works even if I put my new parameters above or below the required parent parameters. That's why I was puzzled. – Marius May 27 '16 at 9:33
  • Uh? Are you sure? Maybe something new in 2.0.6, I tried a couple of weeks ago and it broke my code. – Phoenix128_RiccardoT May 27 '16 at 9:34
  • 1
    I'm 100% sure. The instantiation of objects does not care about the order of the required parameters. You can even combine the new parameters with the parent parameters. It's just the tests that report an error – Marius May 27 '16 at 9:35
  • I know it does not care about order, but I was sure this was not the same on extended classes. Well to know ;) – Phoenix128_RiccardoT May 27 '16 at 9:36
  • I updated my post, have a look. – Phoenix128_RiccardoT May 27 '16 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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