1

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"}"
asked Jun 2, 2017 at 19:44

2 Answers 2

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.

answered Apr 5, 2018 at 13:33
4
  • it will return breadcrumb only on catalog category and product page Commented Mar 4, 2019 at 8:15
  • will this work on custom pages as well? Commented 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. Commented 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 array Commented Jan 13, 2021 at 12:34
1

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');

answered Sep 9, 2019 at 9:37
1
  • why didn't you post the code instead of writing this much of content? Commented Jan 13, 2021 at 7:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.