Breadcrumb Rules

Last updated on
9 September 2019

Flashpoint's out-of-the-box rules

The following are our current rules (as of 8.x-1.0-alpha24) for breadcrumbs of Flashpoint Entities.

  • Flashpoint Courses and Communities are Group entities, and use the default group breadcrumbs
  • Flashpoint entities without a rule specified below simply use Home > [entity]. Usually these entities are not meant to be viewed directly.
  • Flashpoint Community content has bundle-specific rules:
    • Posts use the pattern Home > [community] > [entity]
    • Discussion Posts use Home > [community] > Discussion > [entity]
    • Discussion Categories use Home > [community] > Manage Categories > [entity]
      • Discussion Categories are not meant to be viewed directly. This may change, but for now this is the rule for categories.
  • Flashpoint course content uses the pattern Home > [course] > [entity]

For developers

If you are a developer, note that for course content you should not provide a breadcrumb unless you have reason to override the default logic, and if you are creating a new type of community content, you will need to provide a custom breadcrumb.

If you are making a breadcrumb for the first time, look at the following:

mymodule.services.yml

services:
  mymodule.breadcrumb:
    class: Drupal\mymodule\Breadcrumb\MyModuleBreadcrumbBuilder
    tags:
      - { name: breadcrumb_builder, priority: 100 }

src/Breadcrumb/MyModuleBreadcrumbBuilder.php

This is for a single bundle case. You will need to make edits in applies() handle multi-bundle cases, and build() if different bundles have different breadcrumbs.

<?php

namespace Drupal\flashpoint_comm_content_event\Breadcrumb;

use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\group\Entity\GroupContent;

class MyModuleBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    $params = $route_match->getParameters()->all();
    if (isset($params['flashpoint_community_content']) && !empty($params['flashpoint_community_content'])) {
      $flashpoint_community_content = $params['flashpoint_community_content'];

      /*
       * This breadcrumb applies to mytype content.
       */
      if ($flashpoint_community_content->bundle() === 'mytype') {
        return TRUE;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $url = Url::fromRoute('<front>');
    $breadcrumb->addLink(Link::fromTextAndUrl('Home', $url));

    $flashpoint_community_content = \Drupal::routeMatch()->getParameter('flashpoint_community_content');

    $gcs = GroupContent::loadByEntity($flashpoint_community_content);
    if (!empty($gcs)) {
      $gc = array_shift($gcs);
      $group = $gc->getGroup();
      $link = $group->toLink($group->label(), 'canonical');
      $breadcrumb->addLink($link);

      $my_url = Url::fromRoute('mymodule.myroute', []);
      $my_link = Link::fromTextAndUrl('MyRoute', $my_url);
      $breadcrumb->addLink(my_link);

      // Add a cache item per route, otherwise all breadcrumbs are identical.
      $breadcrumb->addCacheContexts(['route']);
      return $breadcrumb;
    }
    else {
      // Add a cache item per route, otherwise all breadcrumbs are identical.
      $breadcrumb->addCacheContexts(['route']);
      return $breadcrumb;
    }
  }
}

Help improve this page

Page status: No known problems

You can: