21

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?

2
  • Have you looked for it? I see it in the block reader, but nothing in the move one. Don't think you can.
    – nevvermind
    Oct 4, 2015 at 20:36
  • Is there any other way for it without using ifconfig?
    – skynetch
    Oct 5, 2015 at 0:44

2 Answers 2

6

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.

3
  • Is there any extension with strong feature of ifconfig like 1.x?
    – skynetch
    May 20, 2016 at 16:00
  • Hey @Dmitry i don't think there is or one that i'm not aware of. What do you need the ifconfig for?
    – rob3000
    May 23, 2016 at 0:27
  • 1
    eg: <action method="setTemplate" ifconfig="config_path/group/field" condition="one_column"><template>page/1column.phtml</template></action> I meant "ifconfig" and "condition"
    – skynetch
    May 23, 2016 at 3:11
1

I recently came across a similar situation, The solution I was able to come up with was to add a custom layout handler to the page. and then move the element inside the layout handler.

hear you can see how to add a custom layout handler for a product page: https://www.atwix.com/magento-2/add-custom-layout-handle-to-product-page-magento-2/

add the needed condition there before adding the layout handler and then move the element inside the layout handler-XML file, in the above case: the XML name will be:product_page_with_custom_handle.xml.

if you still have doubt, please do refer to this thread as well:Magento 2: replacement for the <PRODUCT_TYPE_simple>, <PRODUCT_TYPE_configurable> etc... product type specific layout handles

eg :

<?php
namespace vendor\ModuleName\Plugin\Catalog\Product;

use Magento\Catalog\Helper\Product\View as ProductViewHelper;
use Magento\Catalog\Model\Product;
use Magento\Framework\DataObject;
use Magento\Framework\View\Result\Page as ResultPage;
use Magento\Framework\View\Result\Page;

/**
 *  Add Custom LayoutHandle To Product"
 */
class AddCustomLayoutHandleToProductPlugin
{
    const PRODUCT_LAYOUT_HANDLE = 'product_page_with_condition_check_true';
    /**
     * @param  ProductViewHelper $subject
     * @param  Page            $resultPage
     * @param  Product         $product
     * @param  null|DataObject $params
     * @return array
     */
    public function beforeInitProductLayout(
        ProductViewHelper $subject,
        $resultPage,
        $product,
        $params
    ) {
        if ($resultPage instanceof ResultPage && $product->getData('asd') == 'your_value') {
            $resultPage->addHandle([static::PRODUCT_LAYOUT_HANDLE]);
        }
        return [
            $resultPage,
            $product,
            $params
        ];
    }
}

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.