class Breadcrumb

  1. 8.x-3.x src/Plugin/Preprocess/Breadcrumb.php Breadcrumb
  2. 8.x-3.x src/Plugin/Setting/Components/Breadcrumbs/Breadcrumb.php Breadcrumb

Pre-processes variables for the "breadcrumb" theme hook.

Plugin annotation

@BootstrapPreprocess("breadcrumb")

Hierarchy

Expanded class hierarchy of Breadcrumb

Related topics

Namespace

Drupal\bootstrap\Plugin\Preprocess
Source
class Breadcrumb extends PreprocessBase implements PreprocessInterface {

  /**
   * {@inheritdoc}
   */
  public function preprocessVariables(Variables $variables) {
    $breadcrumb = &$variables['breadcrumb'];

    // Determine if breadcrumbs should be displayed.
    $breadcrumb_visibility = $this->theme->getSetting('breadcrumb');
    if (($breadcrumb_visibility == 0 || ($breadcrumb_visibility == 2 && \Drupal::service('router.admin_context')->isAdminRoute())) || empty($breadcrumb)) {
      $breadcrumb = [];
      return;
    }

    // Remove first occurrence of the "Home" <front> link, provided by core.
    if (!$this->theme->getSetting('breadcrumb_home')) {
      $front = Url::fromRoute('<front>')->toString();
      foreach ($breadcrumb as $key => $link) {
        if (isset($link['url']) && $link['url'] === $front) {
          unset($breadcrumb[$key]);
          break;
        }
      }
    }

    if ($this->theme->getSetting('breadcrumb_title') && !empty($breadcrumb)) {
      $request = \Drupal::request();
      $route_match = \Drupal::routeMatch();
      $page_title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
      if (!empty($page_title)) {
        $breadcrumb[] = [
          'text' => $page_title,
          'attributes' => new Attribute(['class' => ['active']]),
        ];
      }
    }

    // Add cache context based on url.
    $variables->addCacheContexts(['url']);
  }

}

Members

Contains filters are case sensitive
Name Modifiers Type Description
Breadcrumb::preprocessVariables public function Preprocess the variables array. Overrides PreprocessBase::preprocessVariables
PluginBase::$theme protected property The currently set theme object.
PluginBase::__construct public function
PreprocessBase::$hook protected property The theme hook invoked.
PreprocessBase::$info protected property The theme hook info array from the theme registry.
PreprocessBase::$variables protected property The Variables object.
PreprocessBase::preprocess public function Preprocess theme hook variables. Overrides PreprocessInterface::preprocess
PreprocessBase::preprocessAttributes protected function Ensures all attributes have been converted to an Attribute object.
PreprocessBase::preprocessDescription protected function Converts any set description variable into a traversable array.
PreprocessBase::preprocessElement protected function Preprocess the variables array if an element is present.