0

I am trying to add an action to a grid column, which calls a javascript function of an object in the uiRegistry that requires two arguments.

As an example I replaced the action column of the cms pages grid in the backend with the following column:

use Magento\Ui\Component\Listing\Columns\Column;

class TestActions extends Column {
    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $item[$this->getData('name')] = [
                    'test3' => [
                        'label' => __('Test'),
                        'callback' => [
                            [
                                'provider' => 'cms_page_listing.cms_page_listing',
                                'target' => 'set',
                                'params' => [
                                    'test',
                                    'value'
                                ]
                            ]
                        ]
                    ]
                ];
            }
        }

        return $dataSource;
    }
}

I expect this action to execute something like this:

require('uiRegistry').get('cms_page_listing.cms_page_listing').set('test','value');

But it will execute something like this:

require('uiRegistry').get('cms_page_listing.cms_page_listing').set(['test','value']);

Thus passing the two parameters as an array in a single argument. This leads to a javascript error:

TypeError: path.split is not a function

uiElements set method expects the first argument (path) to be a string and now tries to call split on an array. Am I missing something? Is there a way to make this work or did I stumble on a bug?

Addition

For a button on a form this works. You can specify an action with multiple arguments for a button like this:

[
    'targetName' => 'cms_page_listing.cms_page_listing',
    'actionName' => 'set',
    'params' => [
        'test',
        'value'
    ]
]

I think I stumbled onto a bug. If one compares the js files for an action and a button:

actions.js

tmpCallback = {
                action: registry.async(cb.provider),
                args: _.compact([cb.target, cb.params])
            };
            callbacks.push(tmpCallback);

button-adapter.js

callbacks.push({
                action: registry.async(action.targetName),
                args: _.union([action.actionName], action.params)
            });

In the button adapter args is a single array with the action name and the params. And in actions.js args is an array with the first element being the action name and the second element being an array of params.

Your Answer

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

Browse other questions tagged or ask your own question.