{"id":1593,"date":"2018-05-02T22:02:36","date_gmt":"2018-05-02T16:02:36","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1593"},"modified":"2021-10-23T13:45:58","modified_gmt":"2021-10-23T18:45:58","slug":"apply-a-default-xml-layout-for-magento-module","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/apply-a-default-xml-layout-for-magento-module\/","title":{"rendered":"Magento 1: Apply a Default XML Layout for a Module"},"content":{"rendered":"<p>When creating a multi-page module in Magento 1, you will want to apply some custom XML layout changes to the pages from this module.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML.jpg\" rel=\"attachment wp-att-594\" data-lasso-id=\"646\" data-rel=\"lightbox-gallery-vkWu5YKb\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML-500x500.jpg\" alt=\"\" class=\"wp-image-1594\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML-500x500.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML-150x150.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML-768x768.jpg 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML-600x600.jpg 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML.jpg 944w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption>Learn the correct way to apply default XML layout changes to module pages.<\/figcaption><\/figure><\/div>\n\n\n\n<p>For example, let&#8217;s say every page in this module needs a javascript file loaded in the head of the document.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Wrong Solution<\/h2>\n\n\n\n<p>You could add this to each page within the module in the layout XML, but that&#8217;s redundant and inefficient.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:default decode:true\">&lt;mymodule_controller_index translate=\"label\"&gt;\n\t&lt;reference name=\"head\"&gt;\n\t\t&lt;action method=\"addJs\"&gt;&lt;script&gt;my\/custom\/script.js&lt;\/script&gt;&lt;\/action&gt;\n\t&lt;\/reference&gt;\n&lt;\/mymodule_controller_index&gt;\n\n&lt;mymodule_controller_custompath translate=\"label\"&gt;\n\t&lt;reference name=\"head\"&gt;\n\t\t&lt;action method=\"addJs\"&gt;&lt;script&gt;my\/custom\/script.js&lt;\/script&gt;&lt;\/action&gt;\n\t&lt;\/reference&gt;\n&lt;\/mymodule_controller_custompath&gt;\n\n&lt;mymodule_controller_anotherpath translate=\"label\"&gt;\n\t&lt;reference name=\"head\"&gt;\n\t\t&lt;action method=\"addJs\"&gt;&lt;script&gt;my\/custom\/script.js&lt;\/script&gt;&lt;\/action&gt;\n\t&lt;\/reference&gt;\n&lt;\/mymodule_controller_anotherpath&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The Correct Solution<\/h2>\n\n\n\n<p>The correct way to add a default XML layout declaration for your module is to create a default XML handle that applies the changes to all of the pages within this module.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:default decode:true\">&lt;mymodule_controller_default translate=\"label\"&gt;\n\t&lt;reference name=\"head\"&gt;\n\t\t&lt;action method=\"addJs\"&gt;&lt;script&gt;my\/custom\/script.js&lt;\/script&gt;&lt;\/action&gt;\n\t&lt;\/reference&gt;\n&lt;\/mymodule_controller_default&gt;<\/pre>\n\n\n\n<p>This does nothing yet as Magento doesn&#8217;t know what to do with it just yet.<\/p>\n\n\n\n<p>To make Magento aware of this XML layout handle, we need to extend one function in our controller.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">public function loadLayout($handles = null, $generateBlocks = true, $generateXml = true)\n{\n\treturn parent::loadLayout( array('default','mymodule_controller_default '), $generateBlocks, $generateXml );\n}<\/pre>\n\n\n\n<p>And that&#8217;s it!&nbsp; That is the full solution.&nbsp; Now any page called through this controller will get the default XML layout applied to it.&nbsp; No extra work is required per page, etc. &#8211; just a one and done solution (as it should be).<\/p>","protected":false},"excerpt":{"rendered":"<p>When creating a multi-page module in Magento 1, you will want to apply some custom XML layout changes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1594,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[46,13,5],"tags":[],"class_list":["post-1593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-php-development","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2018\/05\/Magento-1x-Default-XML.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-pH","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1593"}],"version-history":[{"count":3,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1593\/revisions"}],"predecessor-version":[{"id":2022,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1593\/revisions\/2022"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1594"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}