0

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"}"
1

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 page – Dexture Mar 4 at 8:15
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.