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

7

1 Answer 1

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.

2
  • Hello Marius, when i try above code, front page is blank. Why? Aug 29, 2016 at 9:50
  • 1
    try enabling the error reporting and see what's wrong
    – Marius
    Aug 29, 2016 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.