Doesn't work on term pages

Created on 13 January 2022, over 2 years ago
Updated 7 September 2023, 10 months ago

Problem/Motivation

The module does not work on the pages of terms

Steps to reproduce

1. Add custom breadcrumbs
http://prntscr.com/26d4g6g
http://prntscr.com/26d4gmz
2. Clear cache
3. Go to term page Offices and commercial spaces

Breadcrumbs before:
Home / Offices and commercial spaces

Breadcrumbs after:
Home / Offices and commercial spaces

Drupal Version 9.3.0
PHP 8.0.12

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine proweb.ua

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡΅πŸ‡±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 10 months ago
  • πŸ‡΅πŸ‡±Poland lamp5 Rzeszow
Production build 0.69.0 2024