Setting Mage::getBaseUrl for Breadcrumbs in Layout
In Magento, setting crumbInfo/link as Mage::getBaseUrl() for breadcrumbs within a layout XML file can be achieved using the PHP block output method. Let’s delve into this process step by step.
Understanding the XML Structure
The provided layout XML is structured correctly to add custom crumbs to the breadcrumbs block:
<layout version="0.1.0">
<contacts_index_index translate="label">
<reference name="breadcrumbs">
<action method="addCrumb">
<crumbName>Accueil</crumbName>
<crumbInfo>
<label>Accueil</label>
<title>Accueil</title>
<link>/</link> <!-- How can I set Mage::getBaseUrl() here -->
</crumbInfo>
</action>
<action method="addCrumb">
<crumbName>Contactez-nous</crumbName>
<crumbInfo>
<label>Contactez-nous</label>
<title>Contactez-nous</title>
</crumbInfo>
</action>
</reference>
</contacts_index_index>
</layout>
The addCrumb action method is used to add a new crumb with the provided name, label, title, and link. The link field currently holds just a slash (/), which represents the base URL of your Magento store.
Using Mage::getBaseUrl in Layout XML
To set link as Mage::getBaseUrl(), we can use the block type="php" method within our layout XML:
- Add a new block inside the
breadcrumbsreference:
<reference name="breadcrumbs">
<block type="core/text" name="base_url_block">
<action method="setText"><text><![CDATA[<?php echo Mage::getBaseUrl() ?>]]></text></action>
</block>
</reference>
- Reference the new
base_url_blockinside your crumb:
<action method="addCrumb">
<crumbName>Accueil</crumbName>
<crumbInfo>
<label>Accueil</label>
<title>Accueil</title>
<link><block type="core/text" name="base_url_block"></block></link>
</crumbInfo>
</action>
In this setup, a new core/text block named base_url_block is created. The setText action uses the PHP output method to echo Mage::getBaseUrl().
Conclusion
To summarize, setting crumbInfo/link as Mage::getBaseUrl() in your layout XML involves creating a core/text block with the block type="php" method and referencing it within your crumb’s link field. This approach allows you to dynamically set the base URL for each breadcrumb.
Remember that this solution is specific to Magento 1.x. If you’re working on a modern Magento project, consider using Magento 2’s layout updates or XML templates for more flexible and maintainable solutions. For further guidance on Magento development best practices, visit LaravelCompany.