0

Can anybody tell how to move block in Magento2 using observer or maybe other class without using "move" tag in layout xml.

I know about this method in layout, but I can not use it.

<move element="name" destination="to" before="-"/>

In my case "move" depends on 2 store config values.

And YES, I saw this: Magento 2 - Move block or container only for specific Device

Thank

4

In your observer, check if the conditions for moving the block are met. if so, get the layout and manually add a layout update within the observer.

Here's an example that uses an observer on layout_load_before:

namespace Company\Module\Observer;

class LayoutLoadBefore implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * add custom layout updates
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $move = false;

        // add your checks here

        if ($move) {
            /** @var \Magento\Framework\View\LayoutInterface $layout */
            $layout = $observer->getData('layout');
            $layout->getUpdate()->addUpdate('<move element="blah" destination="to" before="-" />');
        }

        return $this;
    }
}

this event conveniently provides access to the layout. if you are stuck with a specific event, there are other ways to access the current layout to add the update.

| improve this answer | |

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.