How can I get the breadcrumb array from my template?
I'm able to get the object:
$this->getLayout()->getBlock('breadcrumbs');
and full breadcrumb path as final HTML
$this->getLayout()->getBlock('breadcrumbs')->toHtml;
But when I use getData or toJson it returns the object, not the breadcrumb data.
$this->getLayout()->getBlock('breadcrumbs')->getData;
Returns
array(2) { ["type"]=> string(36)
"Magento\Theme\Block\Html\Breadcrumbs" ["module_name"]=> string(13)
"Magento_Theme" }
$this->getLayout()->getBlock('breadcrumbs')->toJson;
Returns
string(81)
"{"type":"Magento\\Theme\\Block\\Html\\Breadcrumbs","module_name":"Magento_Theme"}"
2 Answers 2
I know this is a rather old question, but since I ran into sort of the same issue I thought I'd share my answer:
If you want to call it in a custom template, I suppose you should make a module first and create a custom Block class. Then use dependency injection to get the Helper\Data from the Magento_Catalog-module, as follows:
<?php
namespace Name\Module\Block;
use Magento\Catalog\Helper\Data;
class CustomBreadcrumbBlock {
protected $catalogData;
public function __construct(
Data $catalogData
) {
$this->catalogData = $catalogData;
}
public function getBreadcrumbPath() {
return $this->catalogData->getBreadcrumbPath();
}
}
Then you should be able to call $block->getBreadcrumbPath() in your custom block template, which will return an array of your breadcrumb path.
-
it will return breadcrumb only on catalog category and product pageDexture– Dexture2019年03月04日 08:15:23 +00:00Commented Mar 4, 2019 at 8:15
-
will this work on custom pages as well?VishalParkash– VishalParkash2021年01月13日 07:00:55 +00:00Commented Jan 13, 2021 at 7:00
-
@VishalParkash I'm not sure. It's been a while since I visited this part of M2. It's worth a try, though. I can't think of any reason why it wouldn't work.Daan van den Bergh– Daan van den Bergh2021年01月13日 11:03:24 +00:00Commented Jan 13, 2021 at 11:03
-
For me it did not work, I think Magento2 reads only the defined pages for custom magento2 breadcrumb does not work. It returns empty arrayVishalParkash– VishalParkash2021年01月13日 12:34:54 +00:00Commented Jan 13, 2021 at 12:34
You can also just extend the Magento\Catalog\Block\Breadcrumbs class when you make your custom block class. To use the getBreadcrumbsPath() method you write a public method to access the protected object $catalogData to use Data::getBreadcrumbsPath() (i.e. getCatalogDataHandler()).
Then in phtml-file you can do like $block->getCatalogDataHandler()->getBreadcrumbsPath().
The getBreadcrumbsPath() method gives you an assoc array with some category id as key and link and label in value-array so perhaps its a good idea anyway to write a public block-method that gives you exactly what you want. I just needed the id for the top level category.
CODE:$breadcrumbsBlock = $resultPage->getLayout()->getBlock('Magento\Catalog\Block\Breadcrumbs');
-
why didn't you post the code instead of writing this much of content?VishalParkash– VishalParkash2021年01月13日 07:00:01 +00:00Commented Jan 13, 2021 at 7:00
Explore related questions
See similar questions with these tags.