3

I have a ui component with a listing of products in magento 2.2, with a mass action function implemented. however when it submits, i need it to also pass over a parameters from the query string.

example listing url: https://mymagentostore.com/admin/wishlist/projects/additem/wishlist_id/1234/

here is my mass action:

<massaction name="listing_massaction" >
        <action name="additem">             
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">additem</item>
                    <item name="label" xsi:type="string" translate="true">Add Selected Items to Project</item>
                    <item name="url" xsi:type="url" path="wishlist/project/saveitems"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Add Item</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you want to add selected item(s) to this projects?</item>
                    </item>
                </item>
            </argument>                    
        </action>
    </massaction>

I need to find a way to pass wishlist_id=1234 also when the mass action is submitted.

2 Answers 2

5

1) Add class to attribute to action, and replace url item with urlPath:

<massaction name="listing_massaction" >
    <action name="additem" class="Vendor\Module\Ui\Component\Action\AddItem">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">additem</item>
                <item name="label" xsi:type="string" translate="true">Add Selected Items to Project</item>
                <item name="urlPath" xsi:type="string">wishlist/project/saveitems</item>
                <item name="confirm" xsi:type="array">
                    <item name="title" xsi:type="string" translate="true">Add Item</item>
                    <item name="message" xsi:type="string" translate="true">Are you sure you want to add selected item(s) to this projects?</item>
                </item>
            </item>
        </argument>
    </action>
</massaction>

2) Create class extending the action component and implement the prepare method. There you can set the URL basing on the param from request (or from anywhere else).

namespace Vendor\Module\Ui\Component\Action;

class AddItem extends \Magento\Ui\Component\Action
{

    protected $urlBuilder;
    protected $request;

    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\UrlInterface $urlBuilder,
        array $components = array(),
        array $data = array(),
        $actions = null
    ) {
        parent::__construct($context, $components, $data, $actions);

        $this->urlBuilder = $urlBuilder;
        $this->request = $request;
    }

    public function prepare()
    {
        parent::prepare();

        $config = $this->getConfiguration();

        $params = array('wishlist_id' => $this->request->getParam('wishlist_id');

        $config['url'] = $this->urlBuilder->getUrl($config['urlPath'], $params);

        $this->setData('config', $config);
    }
}
1
  1. You should create class extending the action component and implement the prepare method. Better use $this->redirect->getRefererUrl() to get previous url. It's useful in cases when one ui_component insude anouther one and you want use some params like id and so on.
<?php

namespace Vendor\Module\Ui\Component\MassAction;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Ui\Component\Action;

class YourClass extends Action
{
    /**
     * @var RedirectInterface
     */
    protected RedirectInterface $redirect;

    /**
     * @var UrlInterface
     */
    protected UrlInterface $urlBuilder;

    /**
     * @param ContextInterface $context
     * @param RedirectInterface $redirect
     * @param UrlInterface $urlBuilder
     * @param string[] $components
     * @param string[] $data
     * @param $actions
     */
    public function __construct(
        ContextInterface $context,
        RedirectInterface $redirect,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = [],
        $actions = null
    ) {
        parent::__construct($context, $components, $data, $actions);

        $this->urlBuilder = $urlBuilder;
        $this->redirect = $redirect;
    }

    /**
     * @inheritDoc
     */
    public function prepare(): void
    {
        parent::prepare();

        $config = $this->getConfiguration();
        $routeParams = null;

        $refererUrl = $this->redirect->getRefererUrl();
        if ($refererUrl && str_contains($refererUrl, 'some_param')) {

            $pattern = '/some_param\/(\d+)/';
            $matches = [];
            preg_match($pattern, $refererUrl, $matches);

            if (!empty($matches[1])) {
                $routeParams = ['some_param' => $matches[1]];
            }
        }

        $config['url'] = $this->urlBuilder->getUrl($config['urlPath'], $routeParams);
        unset($config['urlPath']);
        $this->setData('config', $config);
    }
}

Add class to attribute to action and following config:

            <massaction name="listing_massaction" >
            <action name="your_class" class="Vendor\Module\Ui\Component\MassAction\YourClass">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">your_class</item>
                        <item name="label" xsi:type="string" translate="true">Your label</item>
                        <item name="urlPath" xsi:type="string">route/controller/action</item>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Title</item>
                            <item name="message" xsi:type="string" translate="true">Message?</item>
                        </item>
                    </item>
                </argument>
            </action>
            </massaction>

Your Answer

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

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