You are here

RuleComponentBreadcrumbBuilder.php in RNG - Events and Registrations 3.x

File

src/Breadcrumb/RuleComponentBreadcrumbBuilder.php
View source
<?php

namespace Drupal\rng\Breadcrumb;

use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\rng\Entity\RuleComponentInterface;
use Drupal\Core\Link;
use Drupal\Core\Breadcrumb\Breadcrumb;

/**
 * Provides a breadcrumb builder for RNG rule components.
 */
class RuleComponentBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    $component = $route_match
      ->getParameter('rng_rule_component');
    return $component instanceof RuleComponentInterface;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $links = [
      Link::createFromRoute($this
        ->t('Home'), '<front>'),
    ];

    /** @var \Drupal\rng\Entity\RuleComponentInterface $component */
    $component = $route_match
      ->getParameter('rng_rule_component');
    if ($rule = $component
      ->getRule()) {
      if ($event = $rule
        ->getEvent()) {
        $links[] = new Link($event
          ->label(), $event
          ->toUrl());
      }
    }
    $breadcrumb = new Breadcrumb();
    return $breadcrumb
      ->setLinks($links)
      ->addCacheContexts([
      'route.name',
    ]);
  }

}

Classes

Namesort descending Description
RuleComponentBreadcrumbBuilder Provides a breadcrumb builder for RNG rule components.