2

My product edit page is a painful mess at the moment. Things are not in a logical order at all. I can reorder attributes in a section, but I cannot seem to change the order of the sections.

It does not use the set in the Attribute Set management. There are also other modules jumping in odd places all over that aren't in the Attribute Set list at all

There really should be a way to reorder the sections to one's preference!

So my questions is: how can I override the sortOrder value for these sections?

Can I do it in di.xml? or do I need to do it via the ui_component interface somehow? or code?

I can't even be sure what the number does, because I have modules at 200 above modules at 10 but one with 999 at the bottom.

Background:

I have found entries like this both in the Magento code and in vendors modules:

    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool">
    <arguments>
        <argument name="modifiers" xsi:type="array">
            <item name="productgridextend" xsi:type="array">
                <item name="class" xsi:type="string">Company\Module\Ui\DataProvider\Product\Form\Modifier\Inventory</item>
                <item name="sortOrder" xsi:type="number">200</item>
            </item>
        </argument>
    </arguments>
</virtualType>

In other cases all I could find is a template entry in ui_component called product_form.xml

And many of the Magento entries do not have a sortOrder value, so there must be a mechanism at page building to decide these based on data

0

As far as I'm aware, the sortOrder on the modifiers you have specified, determines which UIComponent DataProvider runs when, not changing the specific fields sort order values.

DID NOT WORK

If you want to change those values, you could do some sort of messy ui_component update on a file at Vendor_Module/view/adminhtml/ui_component/product_form.xml with entries for the fields(fieldsets) sort order updates, but it sounds like there are a lot of different fieldsets being added from a number of places, so ordering it, isn't going to be a fun time.

I think the reason it's hard to re-order these, is because it's in the adminhtml section of the site, so looking nice isn't AS important...

An example product_form.xml that could move the sku field above the product name

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Example code used in a stack overflow answer
 */
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="product-details">
        <field name="name" sortOrder="10" formElement="input" />
        <field name="sku" sortOrder="20" formElement="input" />
    </fieldset>
</form>

That doesn't work, but instead creates additional fields with the same names and causes issues. The real way to re-order the UI component fields, would be to modify the response from the dataProviders getMeta response.

THE SOLUTION

Define a custom meta modifier, that modifies the sort orders.

// <Vendor_Module>/etc/adminhtml/di.xml
<?xml version="1.0"?>
<!--
/**
 * Example code used in a stack overflow answer
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool" type="Magento\Ui\DataProvider\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="stackoverflow-shifter" xsi:type="array">
                    <item name="class" xsi:type="string">Vendor\Module\Ui\DataProvider\Product\Form\Modifier\Shifter</item>
                    <item name="sortOrder" xsi:type="number">1000</item>
                </item>
            </argument>
        </arguments>
    </virtualType>
</config>

then in the Shifter modifier class you'll need to traverse the meta array structure (which will change at any time..) and modify the sort orders as following

<?php
/**
 * Example code used in a stack overflow answer
 */
namespace Vendor\Module\Ui\DataProvider\Product\Form\Modifier;

/**
 * Change the field sort orders
 *
 * @api
 * @since 101.0.0
 */
class Shifter extends \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier {

    /**
     * @var   \Magento\Framework\Stdlib\ArrayManager
     * @since 101.0.0
     */
    protected $arrayManager;
    /**
     * @param \Magento\Framework\Stdlib\ArrayManager $arrayManager
     */
    public function __construct(
        \Magento\Framework\Stdlib\ArrayManager $arrayManager
    ) {
        $this->arrayManager = $arrayManager;
    }

    /**
     * Shift around product edit form elements
     *
     * @param  array $meta
     * @return array
     * @since  101.0.0
     */
    public function modifyMeta(array $meta)
    {
        $meta = $this->swapSkuWithProductName($meta);

        return $meta;
    }

    /**
     * Swap sku container and product name container, by modifying the sort order
     *
     * @param $meta
     * @return mixed
     */
    protected function swapSkuWithProductName($meta) {

        $this->arrayManager->replace('product-details/children/container_sku/arguments/data/config/sortOrder', $meta, 10);
        $this->arrayManager->replace('product-details/children/container_name/arguments/data/config/sortOrder', $meta, 0);

        return $meta;
    }

    /**
     * @inheritDoc
     */
    public function modifyData(array $data) {
        return $data;
    }
}

The pitfall with this method is that it is all relative sort orders and unless you specify every single detail/element/fieldset you won't have full control of how it shows in the form.

Hope this helps

| improve this answer | |
  • Thanks, the sort orders make sense that way. – iphigenie Jul 9 at 7:42
  • I'll explore your suggestions. I only really care about the order of 5 or 6 of the attribute group sections, the fields seem ok, the other extensions can add themselves after that. You've told me where to look :) – iphigenie Jul 9 at 7:47

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.