1

How I can override the breadcrumb.php file .as If I am using in app/design/frontend/Oc/jb/Magento_Catalog/Block/Breadcrumb.php its giving error.

UPDATED CODE

 // @codingStandardsIgnoreFile
/**
 * Catalog breadcrumbs
 */
namespace Oc\Jb\Block;
use Magento\Catalog\Helper\Data;
use Magento\Framework\View\Element\Template\Context;
class MyBreadcrumbs extends \Magento\Catalog\Block\Breadcrumbs
{
 public function __construct()
 {
 Data $catalogData
 Context $context;
 $data = array();
 parent::__construct($context,$catalogData, $data);
 }
 public function getTitleSeparator($store = null)
 {
 $separator = (string)$this->_scopeConfig->getValue('catalog/seo/title_separator', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
 return ' ' . $separator . ' ';
 }
 public function getCategory ($product) { 
 $_categoryFactory = $this->_objectManager->create('Magento\Catalog\Model\CategoryFactory');
 // for multiple categories, select only the first one
 // remember, index = 0 is 'Default' category
 if (! $product->getCategoryIds())
 return null;
 if (isset ( $product->getCategoryIds()[1]))
 $categoryId = $product->getCategoryIds()[1] ;
 else
 $categoryId = $product->getCategoryIds()[0] ;
 // Additionally for other types of attributes (select, multiselect, ..)
 $category = $_categoryFactory->create()->load($categoryId);
 return ['label' => $category->getName(), 'url' => $category->getUrl() ] ;
 }
 /**
 * Preparing layout
 *
 * @return \Magento\Catalog\Block\Breadcrumbs
 */
 protected function _prepareLayout()
 {
 $product = $this->_coreRegistry->registry('current_product'); 
 //return parent::_prepareLayout();
 if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
 $breadcrumbsBlock->addCrumb(
 'home',
 [
 'label' => __('Home'),
 'title' => __('Go to Home Page'),
 'link' => $this->_storeManager->getStore()->getBaseUrl()
 ]
 );
 $title = [];
 $path = $this->_catalogData->getBreadcrumbPath();
 // If we are at the product page and the $path does not include a category, 
 // then we will append the category link here manually
 // Magento doesn't seem to append category paths to breadcrums consistently
 // Reported here; https://github.com/magento/magento2/issues/7967
 if($product != null ) {
 // check for category path
 $foundCatPath=false;
 foreach ($path as $name => $breadcrumb) {
 if ( strpos ( $name, 'category' ) > -1 ) 
 $foundCatPath=true;
 }
 // append the category path
 if (! $foundCatPath) {
 $productCategory = $this->getCategory($product) ;
 if ($productCategory) {
 $categoryPath = [ 'category' => ['label' => $productCategory['label'] , 'link' => $productCategory['url']] ];
 $path = array_merge ($categoryPath ,$path );
 } 
 }
 }
 foreach ($path as $name => $breadcrumb) {
 $breadcrumbsBlock->addCrumb($name, $breadcrumb);
 $title[] = $breadcrumb['label'];
 }
 // print_r ($path );
 // die ();
 $this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($title)));
 }
 return parent::_prepareLayout();
 }
}
}
MGento
1,51910 silver badges22 bronze badges
asked Jan 4, 2018 at 16:02

3 Answers 3

2

You can also use a plugin/interceptor, which is architecturally a more streamline solution than others posted. You can accomplish this with either the "before" or "around" methods. Each has it's benefits.

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Foo_Bar',
 __DIR__
);

module.xml

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

di.xml

<?xml version="1.0"?>
<config>
 <type name="Magento\Theme\Block\Html\Breadcrumbs">
 <plugin name="foo_bar_plugin_breadcrumbs_modification" type="Foo\Bar\Plugin\BreadcrumbsPlugin" sortOrder="1" />
 </type>
</config>

BreadcrumbsPlugin.php

<?php
namespace Foo\Bar\Plugin;
use \Magento\Theme\Block\Html\Breadcrumbs;
class BreadcrumbsPlugin
{
 // This is probably the "correct" way to account for this scenario
 public function beforeAddCrumb(Breadcrumbs $breadcrumbs, $crumbName, $crumbInfo)
 {
 if (isset($crumbInfo['label'])) {
 // This is where you can modify the breadcrumb text
 $crumbInfo['label'] = __($crumbInfo['label'] . ' baz');
 }
 return [
 $crumbName,
 $crumbInfo,
 ];
 }
 // Alternatively, you can use the "around" method
 public function aroundAddCrumb(Breadcrumbs $breadcrumbs, callable $proceed, $crumbName, $crumbInfo)
 {
 if (isset($crumbInfo['label'])) {
 // This is where you can modify the breadcrumb text
 $crumbInfo['label'] = __($crumbInfo['label'] . ' baz');
 }
 $proceed($crumbName, $crumbInfo);
 }
}
answered Feb 14, 2018 at 12:31
2

You must create a module first.

1. Module creation. Create file app/code/Oc/Jb/etc/module.xml

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

and app/code/Oc/Jb/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
 \Magento\Framework\Component\ComponentRegistrar::MODULE,
 'Oc_Jb',
 __DIR__
);

2. Overriding Block Create file app/code/Oc/Jb/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">
<preference for="Magento\Catalog\Block\Breadcrumbs.php
" type="Oc\Jb\Block\MyBreadcrumbs.php" />
</config>

Create your block file Oc/Jb/Block/MyBreadcrumbs.php. During overriding you must take care of what the parent class's constructor needs. In your case for Magento\Catalog\Block\Breadcrumbs, you will have to provide Context $context, Data $catalogData, array $data = []. For reference check core file.

// @codingStandardsIgnoreFile
/**
 * Catalog breadcrumbs
 */
namespace Oc\Jb\Block;
use Magento\Catalog\Helper\Data;
use Magento\Framework\View\Element\Template\Context;
class MyBreadcrumbs extends \Magento\Catalog\Block\Breadcrumbs
{
 public function __construct()
 {
 Data $catalogData
 Context $context;
 $data = array();
 parent::__construct($context,$catalogData, $data);
 }
 //make necessary changes here.
}

3. Run following commands:

php bin/magento module:enable Oc_Jb
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento cache:clean
php bin/magento cache:flush
answered Jan 4, 2018 at 19:07
7
  • thanks for replying. I need to write all code in __constuct () ? As I want to update that code gist.github.com/mbahar/82703c4b95d924d9bc8e6990202fdeba Commented Jan 4, 2018 at 19:10
  • Please check my updated answer. Commented Jan 4, 2018 at 19:18
  • all step successfully done but still vendor is calling Commented Jan 4, 2018 at 19:58
  • I updated breadcrumb.php code which I am using now Commented Jan 4, 2018 at 20:00
  • tere is another way as well. I will post it tomorrow. I'm sleeping now. Commented Jan 4, 2018 at 20:10
1

You can't override php classes using theme. Just create magento module and override class using preferences : tutorial is here

answered Jan 4, 2018 at 16:17
5
  • I want this file to replace breadcrumbs.php gist.github.com/mbahar/82703c4b95d924d9bc8e6990202fdeba Commented Jan 4, 2018 at 16:19
  • Breadcrumbs.php is php class you cant override it like phtml files. Follow my instruction and you'll get success. Commented Jan 4, 2018 at 16:23
  • I am confuse in steps . Like what will be my namespace Inchoo\Helloworld\Controller\Index; And what will that path mean ? use Magento\Framework\App\Action\Context; . Can you share some steps if possible ? Commented Jan 4, 2018 at 16:24
  • First read this article inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2 Commented Jan 4, 2018 at 16:26
  • @Learner namespace is the same as theme namespace in your case Oc/Jb or another you want. Mostly used vendorname/modulepurpose. For example Google/ReCaptcha. Then create folders like Oc/Jb/Block/Catalog/Block/Breadcrumbs.php and di.xml like in tutorial in second link Commented Jan 4, 2018 at 16:42

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.