- π΅π±Poland lamp5 Rzeszow
Quick tutorial how to use Custom breadcrumbs with Taxonomy Term entities
- Install EVA module
- go to the /admin/structure/views/view/taxonomy_term/edit/page_1 and turn off this page (this page override your default taxonomy term)
- in this views add new display -> type EVA
- setup entity type and bundle in second column
- go to your taxonomy "manage display" page, you can see Eva display
- Right now when you add CB, you can see correct breadcrumbs and you can use tokens.
- π¨π¦Canada efrainh
Hello, I found this issue on a website I'm working on, and I tried another approach to solve it.
We have a some views that create the pages of the pages of some specific taxonomy terms, and we configured custom breadcrumbs for each one of them, so what I did was:I defined a custom service "breadcrumb" in a custom module we have:
File: /modules/custom/my_custom_module/my_custom_module.services.yml
my_custom_module.breadcrumb: class: Drupal\my_custom_module\TermsBreadcrumbBuilder arguments: ['@config.factory', '@entity_type.manager', '@language_manager', '@request_stack', '@title_resolver', '@token', '@path_alias.manager', '@path.matcher', '@router.admin_context'] tags: - { name: breadcrumb_builder, priority: 1011 }
File: /modules/custom/my_custom_module/src/TermsBreadcrumbBuilder.php
In the class specified in the service, I used the "build" method to remove the parameters "view_id" and "display_id" and left taxonomy_term as the first one, and at the end I called the parent method, so the "build" method of the "Custom Breadcrumb" module runs using the right parameter.
namespace Drupal\my_custom_module; use Drupal\custom_breadcrumbs\BreadcrumbBuilder; use Drupal\Core\Routing\RouteMatchInterface; /** * Class TermsBreadcrumbBuilder. * * The module Custom Breadcrumbs doesn't work on Terms pages built with Views * because it expects the first parameter to be the taxonomy term, * but when the term pages are built with views, the 2 firsts parameters are * the View ID and the Display ID. * https://www.drupal.org/project/custom_breadcrumbs/issues/3258374 */ class TermsBreadcrumbBuilder extends BreadcrumbBuilder { /** * {@inheritdoc} */ public function build(RouteMatchInterface $route_match) { // If it's a term page, remove parameters so the breadcrumb work correctly. $params = $route_match->getParameters(); $taxonomy_term = $params->get('taxonomy_term'); // If the page is a taxonomy term, then the parameter taxonomy_term exists. if (isset($taxonomy_term)) { // Selected taxonomies that have breadcrumb (We use other way to build the breadcrumb for the others). $taxonomy_with_breadcrumb = ['my_taxonomy_1', 'my_taxonomy_2', 'my_taxonomy_3]; // Get the taxonomy of the current page. $taxonomy = $taxonomy_term->bundle(); if (in_array($taxonomy, $taxonomy_with_breadcrumb)) { // Remove firsts parameters so the taxonomy term is first. $params->remove('view_id'); $params->remove('display_id'); } } return parent::build($route_match); } }
I was thinking about creating a patch adding some of this code to validate if it's a taxonomy and removing the parameters. What do you think, lamp5?
- π΅π±Poland lamp5 Rzeszow
Hmm it makes sense but It will be nice to has test coverage for this functionality.
- Status changed to Needs work
over 1 year ago 12:31pm 7 September 2023