0

I have a custom module having layout XML file for the front end, in which the default page layout configuration is set to one column, I wish to give the user the ability to change the configuration as specified in the system configurations(like 2columns-left, 2columns-right, 3coulmns). I can get the values from configuration via helper in to the block file prepareLayout ,I would like to know if we can set the layout just like the pagetitle , metakeyowords we use to set. How can we set and overwrite default values in prepare layout in block file? Thanks.

The XML default value is :

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
asked Dec 8, 2017 at 9:27
3
  • So you need to dynemically set page layout which is selected from admin? Commented Dec 8, 2017 at 10:56
  • yes , the user will choose his desire layout from the module configurations-which are similar to the magento default. Commented Dec 8, 2017 at 10:59
  • I post the answer, please check. Commented Dec 8, 2017 at 11:07

1 Answer 1

1

You can set page layout from the controller action. Suppose this is your action file avilable at this location.

app/code/Vendor/Extension/Controller/Youraction/Action.php

Put this code like this.

<?php 
namespace Vendor\Extension\Controller\Youraction;
use Magento\Framework\Controller\ResultFactory;
class Action extends \Magento\Framework\App\Action\Action
{
 public function __construct(
 \Magento\Framework\App\Action\Context $context,
 \Magento\Framework\View\Result\PageFactory $resultPageFactory
 ) {
 $this->resultPageFactory = $resultPageFactory;
 parent::__construct($context);
 }
 public function execute()
 {
 $page = $this->resultPageFactory->create(false, ['isIsolated' => true]); 
 // YOUR LOGIC TO GET SELECTED LAYOUT FROM ADMIN 
 $youradminlayout = 'XYZ'; 
 $page->getConfig()->setPageLayout($youradminlayout);
 return $page;
 }
}
answered Dec 8, 2017 at 11:07
7
  • Uncought Error call to undefined method ::setPageLayout() Commented Dec 8, 2017 at 11:27
  • Can you show the code which you have try. Commented Dec 8, 2017 at 11:37
  • I update my answer, please check now. Commented Dec 8, 2017 at 11:52
  • ok , I will get back soon Commented Dec 8, 2017 at 11:59
  • Ok, If above code help you, please accept it, so it will help other in community as well. Commented Dec 8, 2017 at 12:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.