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.

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);
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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