I want to know how to set the page layout dynamically based on an extension's admin configuration values.
I found how to change the page layout in the layout xml file, http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#layout_markup_columns, but couldn't find to do it based on admin configuration values.
In magento 1.x, I can do it.
<catalog_category_default>
<reference name="root">
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="one_column"><template>page/1column.phtml</template></action>
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="two_column_left"><template>page/2columns-left.phtml</template></action>
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="two_column_right"><template>page/2columns-right.phtml</template></action>
<action method="setTemplate" ifconfig="settings/category/page_layout" condition="three_column"><template>page/3columns.phtml</template></action>
</reference>
</catalog_category_default>
But in magento 2, I couldn't do it.
Anyone know how to do this?
3 Answers 3
For magento 2 you have to do it using Helper and controller.
In Controller you have to write in Index.php
<?php
namespace MageArray\Blog\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
protected $viewHelper;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\MageArray\Blog\Helper\Index\View $viewHelper,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->viewHelper = $viewHelper;
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$page = $this->resultPageFactory->create(false, ['isIsolated' => true]);
$this->viewHelper->prepareAndRender($page, $this);
return $page;
}
}
In namespace MageArray\Blog\Controller\Index;
Here MageArray is my NameSpace, Blog is my Module Name, Index is My controller Ans Class Name "Index" Is My action.
Now Create Helper File as i have View.php Helper and put code as below.
<?php
namespace MageArray\Blog\Helper\Index;
use Magento\Framework\View\Result\Page as ResultPage;
class View extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\Helper\Context $context,
) {
$this->_scopeConfig = $scopeConfig;
parent::__construct($context);
}
public function getStoreConfig($storePath){
$StoreConfig = $this->_scopeConfig->getValue($storePath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
return $StoreConfig;
}
public function initProductLayout(ResultPage $resultPage,$layout_id)
{
$post_list_layout = $this->getStoreConfig($layout_id);
$pageConfig = $resultPage->getConfig();
$pageConfig->setPageLayout($post_list_layout);
$update = $resultPage->getLayout()->getUpdate();
$controllerClass = $this->_request->getFullActionName();
return $this;
}
public function prepareAndRender(ResultPage $resultPage, $controller)
{
$this->initProductLayout($resultPage,'magearray/general/post_list_layout');
return $this;
}
}
make changes in namespace, module, helper and configPath names as per your requirements.
-
How can i change without using controller only from xml file Like in magento 1Deepak Mankotia– Deepak Mankotia2015年11月25日 07:45:43 +00:00Commented Nov 25, 2015 at 7:45
-
How can I change the category page and product detail page layouts?skynetch– skynetch2015年12月01日 16:46:14 +00:00Commented Dec 1, 2015 at 16:46
We can use something like below in Magento2:
<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="contact/contact/enabled" name="contact-us-link" />
Let me know if its helpful!
You can use "action" instruction with ifconfig like this:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="category.products.list">
<action method="setTemplate" ifconfig="path/to/config">
<argument name="text" translate="true" xsi:type="string">namespace::module/list.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
"Action" is deprecated for now. Hope magento developers will add this feature to other instructions in the future.