1

I am trying to sort the Mass Action menu in the sales order grid, I have added <item name="sortOrder" xsi:type="number">600</item> to the sales_order_grid.xml but it has no affect.

Is it still possible in version 2.3.2 to sort the menu?

This is what I have so far

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
    <massaction name="listing_massaction">
        <action name="add_to_archive">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">add_to_archive</item>
                    <item name="label" xsi:type="string" translate="true">Add To Archive</item>
                    <item name="url" xsi:type="url" path="massAddToArchive"/>
                    <item name="sortOrder" xsi:type="number">600</item>
                </item>
            </argument>
        </action>
    </massaction>
    <paging name="listing_paging"/>
</listingToolbar>

2 Answers 2

0

Unfortunately this is not possible and will likely even yield an error. You can take a look at Magento_Ui:etc/ui_configuration.xsd to see what can be used:

            <xs:element name="action">
                <xs:complexType>
                    <xs:choice minOccurs="1" maxOccurs="unbounded">
                        <xs:element ref="label"/>
                        <xs:element name="type" type="xs:string"/>
                        <xs:element name="url" type="urlType"/>
                        <xs:element name="param" type="argumentType"/>
                    </xs:choice>
                    <xs:attribute name="name" use="required"/>
                </xs:complexType>
            </xs:element>

The current sort order is then dependent on the merge order of the ui component definition files which is the same as the module sequence orders (what is written out to app/etc/config.php).

The only option beyond changing the module sequence I see at the moment would be to change the behaviour of the Massaction blocks - see descendants of Magento\Backend\Block\Widget\Grid\Massaction\AbstractMassaction.

1

This can be done using a plugin:

adminhtml/di.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Ui\Component\MassAction">
        <plugin name="CustomVendor_CustomActions::sort_actions" type="CustomVendor\CustomActions\Plugin\Component\MassAction" sortOrder="100"/>
    </type>
</config>

Plugin\Component\MassAction.php:

<?php

namespace CustomVendor\CustomAction\Plugin\Component;

use Magento\Framework\App\RequestInterface;

class MassAction
{

    /** @var RequestInterface */
    private $_request;

    /**
     * MassAction constructor.
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface  $request
    )
    {
        $this->_request = $request;
    }

    /**
     * @param \Magento\Ui\Component\MassAction $massAction
     */
    public function afterPrepare(\Magento\Ui\Component\MassAction $massAction)
    {
        if ($this->_request->getFullActionName() === 'sales_order_index') {

            $config = $massAction->getData('config');

            if (isset($config['actions']) && $config['actions']) {

                $count = 0;

                foreach (["custom_action_1" => 0, "custom_action_2" => 1] as $actionType => $position) {

                    /** @var $config mixed[][] */
                    foreach ($config['actions'] as $key => $action) {

                        if ($action['type'] == $actionType) {
                            $newPosition = max((int) $position, $count);
                            $this->moveElement($config['actions'], $key, $newPosition);
                            break;
                        }
                    }
                    $count++;
                }

                $massAction->setData('config', $config);
            }
        }
    }

    /**
     * @param array $actions
     * @param int $oldPosition
     * @param int $newPosition
     */
    public function moveElement(&$actions, $oldPosition, $newPosition)
    {
        $outputArray = array_splice($actions, $oldPosition, 1);
        array_splice($actions, $newPosition, 0, $outputArray);
    }

}

I had no luck using module sequence or sortOrder param in XML to achieve this. Plugin is the only method that worked for me in Magento 2.4.3.

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.