0

Magento2 How can I add breadcrumb dynamically on custom pages?

Home > FAQ > Carpet FAQ
Home > FAQ > Shutters FAQ

Where Carpet & Shutters are dynamic

3 Answers 3

2

You can achieve by using the below code in the block file.

  public function _prepareLayout()
        {
            $breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs');
            $baseUrl = $this->_storeManager->getStore()->getBaseUrl();
    
            if ($breadcrumbsBlock) {
    
                $breadcrumbsBlock->addCrumb(
                    'home',
                    [
                    'label' => __('FAQ'), //lable on breadCrumbes
                    'title' => __('Carpet FAQ'),
                    'link' => $baseUrl
                    ]
                );
                $breadcrumbsBlock->addCrumb(
                    'coderkube',
                    [
                    'label' => __('Carpet FAQ'),
                    'title' => __('Carpet FAQ'),
                    'link' => '' //set link path
                    ]
                );
            }
            $this->pageConfig->getTitle()->set(__('FAQ')); // set page name
            return parent::_prepareLayout();
        }
0

In your custom controller you can add breadcrumbs using below code.

resultPageFactory is Magento\Framework\View\Result\PageFactory

public function execute(){  
    $resultPage = $this->resultPageFactory->create();
    $breadcrumbsBlock = $resultPage->getLayout()->getBlock('breadcrumbs');
    if ($breadcrumbsBlock) {
        $breadcrumbsBlock->addCrumb(
            'home',
            [
                'label'    => __('Home'),
                'link'     => $this->_url->getUrl('')
            ]
        );
        $breadcrumbsBlock->addCrumb(
            'faq',
            [
                'label'    => __('Faq'),
                'link'     => $this->_url->getUrl('faq')
            ]
        );
        /* you can add here your confition for dynamic element*/
        $breadcrumbsBlock->addCrumb(
            'carpet_faq',
            [
                'label'    => __('Carpet FAQ'),
                'link'     => $this->_url->getUrl('carpetfaq')
            ]
        );
    }
    return $resultPage;
}
1
  • 1
    I tried to add via controller but not working
    – Arjun
    Sep 30, 2020 at 4:43
0

If you want to add breadcrumb through Plugins, you cannot use the method _prepareLayout because it is protected. You can use setLayout method instead with after method :

<?php

namespace YourVendor\YourModule\Plugins\Block;

class AddCustomBreadcrumb
{

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this->_storeManager = $storeManager;
    }

    public function afterSetLayout($subject, $result)
    {
        $breadcrumbsBlock = $subject->getLayout()->getBlock('breadcrumbs');
    
        if ($breadcrumbsBlock) {

            $breadcrumbsBlock->addCrumb(
                'home',
                [
                    'label' => __('Home'), //lable on breadCrumbes
                    'title' => __('Back Home'),
                    'link' => $this->_storeManager->getStore()->getBaseUrl(),
                ]
            );
            $breadcrumbsBlock->addCrumb(
                'storelocator',
                [
                    'label' => __('Carpet FAQ'),
                    'title' => __('Carpet FAQ'),
                    'link' => '' //set link path
                ]
            );
        }

        return $result;
    }
}

Now you need to declare your plugin in di.xml : Edit/Add the file app/code/VENDOR/EXTENSION/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

  <type name="Vendor\Module\Block\BlockName">
    <plugin name="YourVendor_Module::add_custom_breadcrumb" type="YourVendor\YourModule\Plugins\Block\AddCustomBreadcrumb" sortOrder="999"/>
  </type>
</config>

Your Answer

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

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