0

This is my layout file :

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="my_custom_layout_1" />
    <update handle="my_custom_layout_2" />
</page>

I want to update handle my_custom_layout_2 if value from system configuration is "abc" otherwise, I don't want to update that handle.

How to do that ?

Any help would be appreciated.

  • Hvae you tied using ifconfing as I guess it is not possible to do layout update like that. – Korbin Oct 21 at 12:57
  • @Korbin, We can not use ifconfig for update handle. – Kishan Savaliya Oct 21 at 13:05
  • Ifconfig will not working. Ifconfig only use for boolean value. – Niket Oct 21 at 13:10
1

You can do that using Controller action

You can use below code

<?php
namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ){
        $this->resultPageFactory = $resultPageFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();

        $config = $this->getConfig('test/test/test');
        if($config){
            $resultPage->addHandle('my_custom_layout_1');
        }else{
            $resultPage->addHandle('my_custom_layout_2');
        }

        return $resultPage;
    }
}

Hope this will help you!

  • Is it any possible way via xml? – Niket Oct 21 at 18:51
  • @Niket, we can add ifconfig condition in block but you need to update handler so you need to follow that using Controller or using Observer only. You can't do that using XML. Hope my answer is helpful to you. – Kishan Savaliya Oct 22 at 1:42
  • Your code is not working. Can you please help me? – Niket Oct 24 at 11:43
  • Yes sure let me know what you did here? – Kishan Savaliya Oct 24 at 11:44
  • 1
    Okay I'll send you one example within hour then I'll send module's link here – Kishan Savaliya Oct 24 at 11:48
0

You could use a plugin

https://github.com/DominicWatts/MaintenancePage/blob/1a1ce757adae972f6b5435e33da11ea21d5312a0/Plugin/Frontend/Magento/Framework/View/Result/Layout.php#L30-L43

    /**
     * @param \Magento\Framework\View\Result\Layout $subject
     * @return \Magento\Framework\View\Result\Layout
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function afterAddDefaultHandle(
        \Magento\Framework\View\Result\Layout $subject
    ) {
        $storeId = $this->storeManager->getStore()->getId();
        if ($this->pageHelper->isEnabled($storeId) && !$this->pageHelper->isDevAllowed($storeId)) {
            $subject->addHandle('maintenance_page_mode');
        }
        return $subject;
    }

See how it's based on config

https://github.com/DominicWatts/MaintenancePage/blob/1a1ce757adae972f6b5435e33da11ea21d5312a0/Helper/Page.php#L67-L79

    /**
     * Maintenance page
     * @param null $storeId
     * @return mixed
     */
    public function isEnabled($storeId = null)
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_MAINTENANCE_ENABLED,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $storeId
        );
    }
  • Is it any possible way via xml? – Niket Oct 21 at 18:51
  • You can define defaults in XML. You dont have to have system.xml show options in store configuration – Dominic Xigen Oct 21 at 18:53

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.