1

Using Magento 2.1.7 on Ubuntu 16.04.

I have a layout file in my custom theme (<my_theme_dir>/Magento_Theme/layout/default.xml) that removes a couple of footer links, adds a couple others, and specifies an the order for all of them. However, this order is ignored for the blocks that are only referenced in the file, rather than being fully present. Example:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="footer_links">
            <referenceBlock name="privacy-policy-link" remove="true" />
            <referenceBlock name="search-term-popular-link" remove="true" />

            <block class="Magento\Framework\View\Element\Html\Link\Current" name="my-link-name" before="-">
                <arguments>
                    <argument name="label" xsi:type="string">My Link</argument>
                    <argument name="path" xsi:type="string">my-link-path</argument>
                </arguments>
            </block>

            <referenceBlock name="catalog-search-advanced-link" before="sales-guest-form-link" after="" />
            <referenceBlock name="sales-guest-form-link" before="contact-us-link" after="" />
            <referenceBlock name="contact-us-link" after="-" />
        </referenceBlock>
    </body>
</page>

This results in the following order:

  • My link
  • Contact Us
  • Orders and Returns
  • Advanced Search

Which is not the order I specified. However, if I change the "contact us" block so that it's defined fully within the current file:

<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="contact/contact/enabled" name="contact-us-link">
    <arguments>
        <argument name="label" xsi:type="string" translate="true">Contact Us</argument>
        <argument name="path" xsi:type="string">contact</argument>
    </arguments>
</block>

it moves to the bottom as I would expect.

This leads me to believe that I simply can't use the before and after attributes to order blocks that are only referenced. They have to be defined in full for that to work.

This seems less than ideal to me, since if one of those blocks is changed in a future update or something, my override won't know and will continue to display it as it used to be. Is this not worth worrying about? Or is there a way to specify order when some blocks are locally-defined and some are referenced?

2 Answers 2

1

Ok, a long time later I finally figured this out. Posting here in case anybody finds it on google in the future.

The before and after attributes are apparently considered a block argument, so you can simply override it in the arguments section of the referenceBlock node:

<referenceBlock name="catalog-search-advanced-link">
    <arguments>
        <argument name="before" xsi:type="string">sales-guest-form-link</argument>
    </arguments>
</referenceBlock>

However, if all you're trying to do is change the order of the block (i.e. you're not changing the path, label, etc) there's an even easier way, with a move node:

<move element="catalog-search-advanced-link" destination="footer_links" before="sales-guest-form-link" />

This solution worked on Magento 2.2.4.

0

In my case the block after which I wanted to position my block already had after="-".

So always check if the target block already has after/before attributes and change accordingly.

Also check for any <move element="target.block" after/before /> is not already defined, Where target.block is your block relative to which you need to position your block.

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.