I want to use the "easy_breadcrumb" as well as I want to create my own customisations in breadcrumb for my specific custom module. But my customisations doesn't work for specific pages if I install / enable "easy_breadcrumb". It works superbly if I uninstall this module but not with.

I have created my custom module at /web/modules/custom/my_module. In this module have created a file at my_module/src/Breadcrumb/MyModuleBreadcrumbBuilder.php. Now, this class implements BreadcrumbBuilderInterface. In this class applies() function, I am simply returning TRUE and in build() function I am applying my logic to alter the breadcrumb. This breadcrumb altering is done for "Node Add", "Node Edit" and "Node View" of my specific content type that I have created.

Problem is,If I enable this module, none of my customisation works for these 3 things i.e. "Node Add / Edit or View" of that specific content type or even for default 'Article' Content type page that I have added. However, the customisation works great for my other pages for instance, View Pages (approx 9 pages). Not even dpm() function gets excepted from my breadcrumb file. And if I uninstall this "easy_breadcrumb" module, every customisation of mine works like a charm, even dpm() function of devel module executes.

I want to use "easy_breadcrumb" but I also want to do my own customisations in breadcrumbs for my specific content type.

What am I missing ? Any help would be greatly appreciated.

Thanks

P.S. I don't want to use any theme hooks as I haven't yet installed any custom theme of mine and don't want to screw with core themes.

Comments

sandip27 created an issue. See original summary.

sandip27’s picture

Issue summary: View changes
sandip27’s picture

Issue summary: View changes
sandip27’s picture

Also, the last item is getting repeated twice, I don't know for what reason.

Make a note, I haven't updated / changed any .theme file of theme and have not made any changes other than my MyModuleBreadcrumbBuilder.php file.

Thanks

greg boggs’s picture

I believe all you need to do is to make sure your service has a higher priority than EBs:

https://git.drupalcode.org/project/easy_breadcrumb/-/blob/2.x/easy_bread...

ankitjhakal’s picture

Hi All, Yes i am agree with Greg, You have to add high priority than module. I have done this before. I have added sample code for services.yml file.

custom_breadcrumbs.kenexa_jobs_breadcrumb:
    class: Drupal\custom_breadcrumbs\KenexaJobsCustomBreadcrumb
    arguments: ['@config.factory', '@path.current', '@path_alias.manager']
    tags:
      - { name: breadcrumb_builder, priority: 1004 }

Here you can see module use 1003 as priority if you will use 1004 than your code will work.

sandip27’s picture

Thank you @Greg and @Ankit for your inputs. However, after changing the priority, it started taking my changes in effect but to my surprise, now simply started ignoring the breadcrumbs by "easy_breadcrub". I don't see any more breadcrumbs in my other pages by this module now. Not even for Admin or general Add Content links.

Just to summarise, I want to keep the breadcrumbs by "easy_breadcrumbs" as is except my custom changes for my specific content type(s). Below is piece of code I have implemented in my MyModuleBreadcrumbBuilder.php file.

/**
 * {@inheritdoc}
 */
public function build(RouteMatchInterface $route_match) {
	$request = \Drupal::request();
	$breadcrumb = new Breadcrumb();
	$extra_crumb = FALSE;
	$breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
	$node = $route_match->getParameter('node');

	if ($node) {
		switch ($node->getType()) {
			case 'abc':
				$page_title = $this->t("ABC") . " - " . $node->get('field_quarter')->getString();
				$extra_crumb = TRUE;
				break;
			case 'pqr':
				$extra_crumb = TRUE;
			  	$page_title = $node->get('title')->getString();
				break;
			default:
				break;
		}
	} elseif ($route_match->getCurrentRouteMatch()->getRouteName() == "node.add") {
		switch ($route_match->getCurrentRouteMatch()->getRawParameters()->get('node_type')) {
			case 'abc':
				$page_title = $this->t("Add ABC");
				$extra_crumb = TRUE;
				break;
			case 'pqr':
				$page_title = $this->t("Add PQR");
				$extra_crumb = TRUE;
				break;
			default:
				break;
		}
	} else {
		switch ($route_match->getRouteName()) {
			case 'view.blah_blah_blah.page_1':
			case 'view.blah_blah_blah.page_2':
			case 'view.blah_blah_blah.page_3':
				$extra_crumb = TRUE;
				$page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
				break;
			default:
				break;
		}
	}
	if ($extra_crumb) {
		$breadcrumb->addLink(Link::createFromRoute($this->t('XYZ'), 'page_manager.blah-panels_variant-0'));
	}
	if (isset($page_title) && !empty($page_title)) {
		$breadcrumb->addLink(Link::createFromRoute($page_title, '<none>'));
	}

	// Don't forget to add cache control by a route.
	// Otherwise all pages will have the same breadcrumb.
	$breadcrumb->addCacheContexts(['route']);

	// Return object of type breadcrumb.
	return $breadcrumb;
}

Would be really great if you could help me to fix this.

ankitjhakal’s picture

Hi @sandip27, I am attaching the code how it will work. In my past experience i have done the same thing. Where i created different file for each content type. please check below code once you will get your answer.

class JobsCampaignCustomBreadcrumb implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

  /**
   * The entity type manager.
   *
   * @var \Drupal\path_alias\AliasManagerInterface
   */
  protected $pathAlias;

  /**
   * Constructs the SpecialPagesCustomBreadcrumb.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path service.
   * @param \Drupal\path_alias\AliasManagerInterface $path_alias
   *   The path alias service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, CurrentPathStack $current_path, AliasManagerInterface $path_alias) {
    $this->configFactory = $config_factory;
    $this->currentPath = $current_path;
    $this->pathAlias = $path_alias;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    // Breadcrumb for 'jobs-campaign' pages.
    if ($route_match->getRouteName() == 'entity.node.canonical') {
      $node = $route_match->getParameter('node');
      if ($node->bundle() == 'jobs_campaign' && $this->checkForIgnoredPaths()) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $node = $route_match->getParameter('node');
    $breadcrumb->addCacheableDependency($node);
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home', [], ['context' => 'breadcrumb']), '<front>'));
    $breadcrumb->addLink(Link::fromTextAndUrl($this->t('%title', ['%title' => $node->label()]), Url::fromRoute('<none>')));
    $breadcrumb->addCacheContexts(['route', 'url.path']);
    return $breadcrumb;
  }
ankitjhakal’s picture

I think whats wrong with you is, you are returning true in applies method. Try to add your content types condition in applies method rather then build method.

sandip27’s picture

Status: Active » Fixed

aah ! Silly mistake.. For testing purpose, I had simply removed the conditions and returned TRUE for all in order to take my customisations effect. Forgot to put them back in. Putting those conditions back made it work !!

Thank you @ankit for help and Thank you @Greg for this awesome module and support.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.