2

How can you just add a breadcrumb to views? Easy breadcrumb leaves out views. Views breadcrumb does not work correctly with Drupal 8. Is there a simple way with hooks?

EDIT 2: Maybe I mean something else?

In Drupal 7 (with custom breadcrumb) it looks like this: The view "Informationen" is called:

The view is retrieved

One node out of the view is called:

node out of the view

In Drupal 8 (with Easy breadcrumb) it looks like this: The view "Informationen" is called: D8 view called

One node out of the view is called:

enter image description here

I hope I could explain the difference.

3
  • 1
    Place breadcrumb block from the menu : Structure > Block layout path: drupal-823.local/admin/structure/block
    – developer
    Mar 21, 2017 at 11:59
  • 1
    Do You mean the "Views breadcrumb" block? I could'nt find it in that way. Mar 21, 2017 at 12:01
  • 1
    The default breadcrumb block ,you can add it to the required region by selecting Breadcrumbs from the Place block popup.
    – developer
    Mar 22, 2017 at 6:14

2 Answers 2

3

You should create a custom module (use core >= 8.3). Use this code as a base to start:

custom_breadcrumb.info.yml

name: 'Custom breadcrumbs'
description: 'Custom breadcrumbs implementation'
package: 'Custom'
# core: 8.x
type: module

version: '8.x-1.0'
core: '8.x'
project: 'custom_breadcrumb'
datestamp: 1443649740

custom_breadcrumb.services.yml

services:
  custom_breadcrumb.breadcrumb_views:
    class: Drupal\custom_breadcrumb\Breadcrumb\ViewsBreadcrumbBuilder
    tags:
      - { name: breadcrumb_builder, priority: 1 }

src/Breadcrumb/ViewsBreadcrumbBuilder.php

<?php

namespace Drupal\custom_breadcrumb\Breadcrumb;

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Link;

class ViewsBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use LinkGeneratorTrait;
  use StringTranslationTrait;

  /**
   * @inheritdoc
   */
  public function applies(RouteMatchInterface $route_match) {
    // This breadcrumb apply only for some views.
    $parameters = $route_match->getParameters()->all();

    if (isset($parameters['view_id'])) {

       $views_id = array(
         'VIEW_MACHINE_NAME_1',
         'VIEW_MACHINE_NAME_2',
       );

       if (in_array($parameters['view_id'], $views_id)) {
         return TRUE;
       }
       return FALSE;
    }
  }

  /**
   * @inheritdoc
   */
  public function build(RouteMatchInterface $route_match) {
    // Breadcrumbs set up (cache settings are so important!).
    $breadcrumb = new \Drupal\Core\Breadcrumb\Breadcrumb();
    $breadcrumb->addCacheContexts(["url"]);
    $breadcrumb->addCacheTags(["view_id:{$parameters['view_id']}"]);

    $parameters = $route_match->getParameters()->all();
    if (isset($parameters['view_id'])) {
      if ($parameters['view_id'] == 'VIEW_MACHINE_NAME_1') {
         $breadcrumb->addLink(Link::createFromRoute(t('VIEWS NAME 1'), '<none>'));
      }
      else if ($parameters['view_id'] == 'VIEW_MACHINE_NAME_2') {
        $breadcrumb->addLink(Link::createFromRoute(t('VIEWS NAME 2'), '<none>'));
      } 
    }

    // Reverse order for this to work!!!.
    $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
    return $breadcrumb;
  }
}

Remember to replace VIEW_MACHINE_NAME_1 and VIEW_MACHINE_NAME_2 with the machine name of your views. Also replace VIEWS NAME 1 and VIEWS NAME 2 with the breadcrumb title you want for each view.

19
  • 1
    Thanks, i want to try it. This is not yet my level, I am very excited to do that. Mar 21, 2017 at 12:02
  • 2
    I've tried it. Nothings happens. The module is installed, machine names und view name are changed. No reaction. I can not debug this, unfortunately, a pity. Thanks again. Mar 21, 2017 at 19:36
  • 1
    Have you altered the 4 variables I told you at the bottom of my answer? Do you know how to find the machine name of a view? If you edit it then you will find the machine name on the URL. For example, the machine name for the admin content view is content given this URL: admin/structure/views/view/content. Also, you can debug my module if you enable the contrib Devel module together with its submodule "Devel kint". Then, anywhere on the code do kint($VARIABLE_TO_DEBUG);. Finally, make sure all files have their correct extension and make sure the Breadcrumb builder is under src/Breadcrumb.
    – Roger
    Mar 22, 2017 at 7:41
  • 1
    Yes. I edited my question with images. Mar 22, 2017 at 17:12
  • 1
    Which exact version of Drupal 8 are you using? The code I shared to you is working fine on a Drupal 8.3.0-beta1 Once enabled, have you rebuild the cache?
    – Roger
    Mar 22, 2017 at 18:28
2

You can restrict the access to the view by configuring the pages section.

enter image description here

enter image description here

3
  • 1
    Thank You. I did so. The system block breadcrumb is already enabled. Btw: which admin theme do you use? It looks nice and better than seven. Regards Frank Mar 22, 2017 at 7:00
  • 1
    Admin theme is Seven. Also enabled the checkbox for : Use the administration theme when editing or creating content
    – developer
    Mar 22, 2017 at 7:13
  • It is an Adminimal theme
    – user83311
    Nov 10, 2019 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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