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">
-
So you need to dynemically set page layout which is selected from admin?Dhiren Vasoya– Dhiren Vasoya2017年12月08日 10:56:48 +00:00Commented 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.Verdu– Verdu2017年12月08日 10:59:10 +00:00Commented Dec 8, 2017 at 10:59
-
I post the answer, please check.Dhiren Vasoya– Dhiren Vasoya2017年12月08日 11:07:58 +00:00Commented Dec 8, 2017 at 11:07
1 Answer 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;
}
}
-
Uncought Error call to undefined method ::setPageLayout()Verdu– Verdu2017年12月08日 11:27:26 +00:00Commented Dec 8, 2017 at 11:27
-
Can you show the code which you have try.Dhiren Vasoya– Dhiren Vasoya2017年12月08日 11:37:43 +00:00Commented Dec 8, 2017 at 11:37
-
I update my answer, please check now.Dhiren Vasoya– Dhiren Vasoya2017年12月08日 11:52:54 +00:00Commented Dec 8, 2017 at 11:52
-
-
Ok, If above code help you, please accept it, so it will help other in community as well.Dhiren Vasoya– Dhiren Vasoya2017年12月08日 12:01:08 +00:00Commented Dec 8, 2017 at 12:01
Explore related questions
See similar questions with these tags.