Magento2 – Using ifconfig in Layout XML

ifconfiglayoutmagento2xml

I'm working with magento 2.

I can use ifconfig attribute in the block code, and it works well.

<block class="Magento\Catalog\Block\Category\View" name="category_desc_main_column" template="category/desc_main_column.phtml" ifconfig="config_path/group/field" before="category.products"/>

But I tried to use it for move, it didn't work.

<move element="category.image" destination="content" ifconfig="config_path/group/field" before="-"/>

Anyone know how to use it for moving?

Best Answer

From what i understand you can't use ifconfig on move. In the class Magento\Framework\View\Layout\Reader\Block.php there is a check for the attribute ifconfig:

$configPath = (string)$currentElement->getAttribute('ifconfig');

source:
https://github.com/magento/magento2/blob/2.3-develop/lib/internal/Magento/Framework/View/Layout/Reader/Block.php

However on the move block is doesn't actually check for the ifconfig attribute:

protected function scheduleMove(Layout\ScheduledStructure $scheduledStructure, Layout\Element $currentElement)
    {
        $elementName = (string)$currentElement->getAttribute('element');
        $destination = (string)$currentElement->getAttribute('destination');
        $alias = (string)$currentElement->getAttribute('as') ?: '';
        if ($elementName && $destination) {
            list($siblingName, $isAfter) = $this->beforeAfterToSibling($currentElement);
            $scheduledStructure->setElementToMove(
                $elementName,
                [$destination, $siblingName, $isAfter, $alias]
            );
        } else {
            throw new \Magento\Framework\Exception\LocalizedException(
                new \Magento\Framework\Phrase('Element name and destination must be specified.')
            );
        }
        return $this;
    }

https://github.com/magento/magento2/blob/2.3-develop/lib/internal/Magento/Framework/View/Layout/Reader/Move.php#L49

In Theroy you shouldn't need the ifconfig on the move if there is already an ifconfig on the block as the block won't be rendered and thus not moved.

Hope that makes sense.