You are here

class WebformAccessBreadcrumbBuilder in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_access/src/Breadcrumb/WebformAccessBreadcrumbBuilder.php \Drupal\webform_access\Breadcrumb\WebformAccessBreadcrumbBuilder

Provides a webform access breadcrumb builder.

Hierarchy

Expanded class hierarchy of WebformAccessBreadcrumbBuilder

1 string reference to 'WebformAccessBreadcrumbBuilder'
webform_access.services.yml in modules/webform_access/webform_access.services.yml
modules/webform_access/webform_access.services.yml
1 service uses WebformAccessBreadcrumbBuilder
webform_access.breadcrumb in modules/webform_access/webform_access.services.yml
Drupal\webform_access\Breadcrumb\WebformAccessBreadcrumbBuilder

File

modules/webform_access/src/Breadcrumb/WebformAccessBreadcrumbBuilder.php, line 16

Namespace

Drupal\webform_access\Breadcrumb
View source
class WebformAccessBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The current route's entity or plugin type.
   *
   * @var string
   */
  protected $type;

  /**
   * Constructs a WebformAccessBreadcrumbBuilder.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(TranslationInterface $string_translation) {
    $this
      ->setStringTranslation($string_translation);
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {
    $route_name = $route_match
      ->getRouteName();

    // All routes must begin or contain 'webform_access'.
    if (strpos($route_name, 'webform_access') === FALSE) {
      return FALSE;
    }
    if (!$route_match
      ->getRouteObject()) {
      return FALSE;
    }
    $path = Url::fromRouteMatch($route_match)
      ->toString();
    if (strpos($path, 'admin/structure/webform/access/') === FALSE) {
      return FALSE;
    }
    if (strpos($path, 'admin/structure/webform/access/group/manage/') !== FALSE) {
      $this->type = 'webform_access_group';
    }
    elseif (strpos($path, 'admin/structure/webform/access/type/manage/') !== FALSE) {
      $this->type = 'webform_access_type';
    }
    else {
      $this->type = 'webform_access';
    }
    return $this->type ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Home'), '<front>'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Administration'), 'system.admin'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Structure'), 'system.admin_structure'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Webforms'), 'entity.webform.collection'));
    $breadcrumb
      ->addLink(Link::createFromRoute($this
      ->t('Access'), 'entity.webform_access_group.collection'));
    switch ($this->type) {
      case 'webform_access_group':
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Groups'), 'entity.webform_access_group.collection'));
        break;
      case 'webform_access_type':
        $breadcrumb
          ->addLink(Link::createFromRoute($this
          ->t('Types'), 'entity.webform_access_type.collection'));
        break;
    }

    // This breadcrumb builder is based on a route parameter, and hence it
    // depends on the 'route' cache context.
    $breadcrumb
      ->addCacheContexts([
      'route',
    ]);
    return $breadcrumb;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformAccessBreadcrumbBuilder::$type protected property The current route's entity or plugin type.
WebformAccessBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
WebformAccessBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
WebformAccessBreadcrumbBuilder::__construct public function Constructs a WebformAccessBreadcrumbBuilder.