1

I created layout XML file for 2columns-left layout. I need to use two options 2columns-left and 2columns-right in system configuration. When admin selects 2columns-right it should set layout on Right side and vise versa.

How should i proceed?

enter image description here

5
+50

If you are doing this for a page, I assume you have a controller action for that page.
You can do this in your controller action:

namespace [Namespace]\[Module]\Controller\[Something];

class SomeAction extends \Magento\Framework\App\Action\Action 
{
    protected $resultPageFactory;
    protected $scopeConfig;
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Magento\Framework\View\Result\PageFactory $resultPageFactory,
       \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }


    public function execute()
    {
         $page = $this->resultPageFactory->create();
         $layout = $this->scopeConfig->getValue(
                       'path/to/config_value', 
                       \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_STORE
         );
         if ($layout) {
             $page->getConfig()->setPageLayout($layout);
         }
    }
}

I haven't tested the code, so watch out for typos.

  • Hello Marius, when i try above code, front page is blank. Why? – Payal Patel Aug 29 '16 at 9:50
  • try enabling the error reporting and see what's wrong – Marius Aug 29 '16 at 11:00
  • Already enabled error report and i also check in log file but no any error fond. – Payal Patel Aug 29 '16 at 11:11

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.