Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento get layout for given page

Tags:

magento

I'm working on a Magento module where i want to fetch the layout from a frontend page. By frontend page i mean all types of pages, i.e. cms page, category, product, cart, my account, etc.

I've read the excellent magento-nofrills ebook from Alan Storm and i'm using his Commercebug plugin to help me develop my magento site. I've also spent some time on stackoverflow looking for a similair question, please forgive me if i have missed it.

If for example i have a page id or category id, how do i fetch the layout of that frontend page from code run in the backend?

I've been playing with the request and when i run this code in the frontend i can see that the module, controller, layouthandles etc are changed when for instance looking at a catalog page

$request = Mage::app()->getRequest();
$request->setModuleName('cms');
$request->setRouteName('cms');
$request->setControllerName('page');
$request->setActionName('view');
$request->setParam('page_id', 6);

But when i inspect the xml from the layout, i can't seem to force magento to show me the xml for (in this example) cms page with id=6:

Mage::app()->getLayout()->getNode()->asXml();

Maybe i'm thinking way too complicated though. I would like to check if a certain page has sidebars, which blocks and elements are shown on the page and in the sidebars (only active blocks, not the ignored ones).

Thanks in advance! Tim

like image 514
Tim Avatar asked Aug 11 '11 14:08

Tim


People also ask

How can I get layout in Magento 2?

You can get a list of called Layout XML for specific pages by Magento\Framework\App\View class. Call getLayout() method from Magento\Framework\App\View. php class. getLayout()->getUpdate()->getHandles() used for getting all the handles of a page.

Where can I find templates in Magento 2?

The Magento_Checkout layouts are located in app/code/Magento/Checkout/view/frontend/layout/ . After searching this directory for occurrences of “ minicart. phtml ”, we define that the layout we are looking for is app/code/Magento/Checkout/view/frontend/layout/default.

What is page layout in Magento 2?

In Magento, the basic components of page design are layouts, containers, and blocks. A layout represents the structure of a web page (1). Containers represent the placeholders within that web page structure (2). And blocks represent the UI controls or components within the container placeholders (3).

How do I override layout XML in Magento 2?

xml will override app/design/frontend/Magento/luma/Magento_Checkout/layout/checkout_cart_index. xml . To override page layout files, use the page_layout directory name instead of layout .


1 Answers

Call loadLayout() before getLayout().

You can check if a block is active in the layout by calling getBlock($block_name):

$left_block = $this->loadLayout()->getLayout()->getBlock('left');

If the block isn't part of the layout (or is ignored) then $left_block will be false. You can use getBlock() in conjunction with getSortedChildren() to see its child blocks:

$this->loadLayout()->getLayout()->getBlock('content')->getSortedChildren();

The above will return a string array of block names within the "content" block. Only names of active blocks will be returned.

like image 115
Joe Avatar answered Oct 06 '22 20:10

Joe