Exception #0 (Exception): Notice: Undefined variable: crumbs in /breadcrumbs.phtml on Custom Layout
-
09-10-2020 - |
Question
Ive created a new page template xml which looks like:
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<update handle="empty"/>
<referenceContainer name="page.wrapper" htmlClass="site-wrapper" htmlId="magento">
<container name="header.container" as="header_container" label="Page Header Container" htmlTag="header" htmlClass="page-header" htmlId="header" before="main.content"/>
<container name="business.types.top" as="business_types_top" label="Business Types Top" after="header.container"/>
<container name="business.types.bottom" as="business_types_bottom" label="Business Types Bottom" after="main.content"/>
<container name="footer-container" as="footer" before="before.body.end" label="Page Footer Container" htmlTag="footer" htmlClass="page-footer" htmlId="footer"/>
</referenceContainer>
</layout>
And in my default.xml I'm trying to add the breadcrumbs.phtml to the business.types.top container like:
<referenceContainer name="business.types.top">
<block class="Magento\Framework\View\Element\Template" name="home.breadcrumbs" template="Magento_Theme::html/breadcrumbs.phtml"></block>
</referenceContainer>
However I receive the following error?
1 exception(s):
Exception #0 (Exception): Notice: Undefined variable: crumbs in /templates/html/breadcrumbs.phtml on line 10.
Can anyone please advise on how I can get the breadcrumbs to work on this page?
Thanks
UPDATE: This is my breadcrumbs.phtml, which is working on other pages using the default page templates:
<?php if ($crumbs && is_array($crumbs)) : ?>
<section class="section-breadcrumbs">
<div class="container">
<ul class="as-breadcrumbs">
<li><a href="/" title="Home">Home</a></li>
<?php foreach ($crumbs as $crumbName => $crumbInfo) : ?>
<li class="item <?php /* @escapeNotVerified */ echo $crumbName ?>">
<?php if ($crumbInfo['link']) : ?>
<a href="<?php /* @escapeNotVerified */ echo $crumbInfo['link'] ?>">
<?php
if ($block->escapeHtml($crumbInfo['label']) != "Home") {
echo "Products";
} else {
echo $block->escapeHtml($crumbInfo['label']);
}
?>
</a>
<?php elseif ($crumbInfo['last']) : ?>
<p class="active"><?php echo $block->escapeHtml($crumbInfo['label']) ?></p>
<?php else: ?>
<?php echo $block->escapeHtml($crumbInfo['label']) ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
Solution
Try to change a condition inside your template file from:
<?php if ($crumbs && is_array($crumbs)) : ?>
to:
<?php if (!empty($crumbs) && is_array($crumbs)) : ?>
PS: Do not change core template, use own template file
UPDATE:
The $crumbs
variable comes from the block Magento\Theme\Block\Html\Breadcrumbs
from the method _toHtml
:
/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (is_array($this->_crumbs)) {
reset($this->_crumbs);
$this->_crumbs[key($this->_crumbs)]['first'] = true;
end($this->_crumbs);
$this->_crumbs[key($this->_crumbs)]['last'] = true;
}
$this->assign('crumbs', $this->_crumbs);
return parent::_toHtml();
}
where the $this->_crumbs
is a $crumbs
(in future). They are transformed in the Magento\Framework\View\Element\Template
in the method fetchView
on line ~255 :
$html = $templateEngine->render($this->templateContext, $fileName, $this->_viewVars); // crumbs stored in the _viewVars attribute
To add a crumbs you should use method Magento\Theme\Block\Html\Breadcrumbs::assign('crumbs', $arrayOfBreadcrumbs);
:
/**
* Assign variable
*
* @param string|array $key
* @param mixed $value
* @return $this
*/
public function assign($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $subKey => $subValue) {
$this->assign($subKey, $subValue);
}
} else {
$this->_viewVars[$key] = $value;
}
return $this;
}
I think you have no breadcrumbs on your page because you are using an empty layout (<update handle="empty"/>
).
OTHER TIPS
Please check crumbs
variable in your /templates/html/breadcrumbs.phtml file. it should be $crumbs
.