I am bit confused in this two handle like when to use this two tags. Please clearfy for me like when to use this two.
Magento Layout XML – Difference Between and Handle
layoutxml
Related Solutions
You can't do that. Specifically, you can't use the <update/>
tag in a CMS page's Layout Update XML field.
Magento processes the <update/>
nodes when it is initially loading the layout update files, and layout updates from the core_layout_update
table. This is done with the fetchRecursiveUpdates
function.
#File: app/code/core/Mage/Core/Model/Layout/Update.php
public function fetchPackageLayoutUpdates($handle)
{
//...
$this->fetchRecursiveUpdates($updateXml);
//...
return true;
}
public function fetchDbLayoutUpdates($handle)
{
//...
$this->fetchRecursiveUpdates($updateXml);
//...
}
//...
public function fetchRecursiveUpdates($updateXml)
{
foreach ($updateXml->children() as $child) {
if (strtolower($child->getName())=='update' && isset($child['handle'])) {
$this->merge((string)$child['handle']);
// Adding merged layout handle to the list of applied hanles
$this->addHandle((string)$child['handle']);
}
}
return $this;
}
A CMS page, however, is rendered later on via a helper method. The rendering of a CMS page includes this code
#File: app/code/core/Mage/Cms/Helper/Page.php
$action->loadLayoutUpdates();
$layoutUpdate = ($page->getCustomLayoutUpdateXml() && $inRange)
? $page->getCustomLayoutUpdateXml() : $page->getLayoutUpdateXml();
$action->getLayout()->getUpdate()->addUpdate($layoutUpdate);
$action->generateLayoutXml()->generateLayoutBlocks();
The loadLayoutUpdates
call is the standard loading of the layout, including the fetchRecursiveUpdates
mentioned above.
Then, Magento looks for a custom layout XML update set on the CMS page object
$layoutUpdate = ($page->getCustomLayoutUpdateXml() && $inRange)
? $page->getCustomLayoutUpdateXml() : $page->getLayoutUpdateXml();
and adds it directly to the list of updates to apply
$action->getLayout()->getUpdate()->addUpdate($layoutUpdate);
This means the scanning for the update
tag is never done.
If I was trying to implement something like this, I'd add a new block in the CMS page's layout update XML, and then add the shared layout items programmatically in the blocks _prepareLayout
method. (not sure off the top of my head if that would work w/r/t to timing, but that'd be where I'd start)
Once the layout is loaded (as it is for the controller_action_layout_generate_blocks_after
event) all of the actions have been processed. Adding a new handle to the update object at this point (or even manually merging its contents into the already-merged contents) doesn't make sense as you would have to regenerate things. You should take a different approach.
Stop and think what you need to do: unset JS on the head block, an instance of Mage_Page_Block_Html_Head
.
If you leave your current observer as it is configured, you can do
public function removePopupJs($observer)
{
$list = $this->getRemoveList(); // not sure how you want to transport values in
$root = $observer->getLayout()->getBlock('root');
$head = $observer->getLayout()->getBlock('head');
if (
is_object($root)
&& $root->getTemplate() == 'page/popup.phtml'
&& is_object($head)
)
{
foreach ($list as $type => $path) {
$head->removeItem($type,$path);
}
}
}
As I said, not sure how you want to propagate "remove this, not that" into the observer context. It would suck to maintain dual lists in separate "spaces", i.e. one in layout XML for adding and one in PHP for removing. Perhaps, if all JS needing removal is added in one layout XML file, you could re-load()
that file, or you could tag each JS-add directive with a custom attribute to select via XPATH.
You could also get rid of the observer and do the above in a helper method which just needs to be called after all JS has been added to the head block. Add your action to both the <customer_logged_out />
and <customer_logged_in />
handles as they will be executed on all page loads and after most other handles are executed.
There are other options as well. Layout it wicked flexible.
Related Topic
- Difference Between ‘Page’ and ‘Layout’ Root Tags in Magento 2
- Magento2 Module Layout – Relationship Between Module Layout Handle XML Files and Theme Update XML Files
- Does Module Sequence Impact Layout Handle XML Loading Order in Magento 2?
- Magento2 Category Field Custom Layout Update – How to Add Handle and Declaration
Best Answer
Reference
This is used to make a reference to an current existing block in the layout xml. Say you want to change a template of a block you would first reference it and then call set template. Or if you wanted to add some new javascript to the header.
Update
This is used to add a complete layout handle inside another one. For example if you had the layout handle
test_handle
and wanted to include the handlecatalog_product_view
you would do the following.In this case the
test_handle
and thecatalog_product_view
handle would be exactly the same and include the same blocks.