You are here

public function ContextBreadcrumbBuilder::build in Context Breadcrumb 8

Builds the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Breadcrumb\Breadcrumb A breadcrumb.

Overrides BreadcrumbBuilderInterface::build

File

src/Breadcrumb/ContextBreadcrumbBuilder.php, line 220

Class

ContextBreadcrumbBuilder
Class ConextBreadcrumbBuilder.

Namespace

Drupal\context_breadcrumb\Breadcrumb

Code

public function build(RouteMatchInterface $route_match) {
  $breadcrumb = new Breadcrumb();
  if (!empty($this->contextReactions)) {
    $language = \Drupal::languageManager()
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
    foreach ($this->contextReactions as $reaction) {
      if (empty($reaction) || !$reaction instanceof ContextBreadcrumb) {
        continue;
      }

      /** @var \Drupal\context\ContextReactionInterface $reaction */
      $contextBreadcrumbs = $reaction
        ->execute();
      foreach ($contextBreadcrumbs as $contextBreadcrumb) {
        try {
          if (!empty($contextBreadcrumb['title'])) {
            if ($contextBreadcrumb['url'] == '<front>') {
              $contextBreadcrumb['url'] = '/';
            }
            if (!empty($contextBreadcrumb['token'])) {
              $token_data = [];
              $params = $route_match
                ->getParameters();
              foreach ($params
                ->keys() as $key) {
                $param_object = $params
                  ->get($key);
                if ($key == 'node' && !is_object($param_object)) {
                  $param_object = Node::load($param_object);
                }
                if ($key == 'user' && !is_object($param_object)) {
                  $param_object = User::load($param_object);
                }
                if ($key == 'node_revision' && !is_object($param_object)) {
                  $param_object = \Drupal::entityTypeManager()
                    ->getStorage('node')
                    ->loadRevision($param_object);
                }
                $token_data[$key] = $param_object;
              }
              $contextBreadcrumb['title'] = $this
                ->renderData($contextBreadcrumb['title'], $contextBreadcrumb['token'], $token_data);
              if ($contextBreadcrumb['url'] === '<nolink>') {
                $contextBreadcrumb['url'] = Url::fromRoute($contextBreadcrumb['url']);
              }
              else {
                $contextBreadcrumb['url'] = $this
                  ->renderData($contextBreadcrumb['url'], $contextBreadcrumb['token'], $token_data);
              }
              if ($contextBreadcrumb['url'] instanceof Url) {
                $breadcrumb
                  ->addLink(Link::fromTextAndUrl($this
                  ->trans($contextBreadcrumb['title']), $contextBreadcrumb['url']));
              }
              if (is_string($contextBreadcrumb['url'])) {
                if (strpos($contextBreadcrumb['url'], '://') !== FALSE) {
                  $breadcrumb
                    ->addLink(Link::fromTextAndUrl($this
                    ->t($contextBreadcrumb['title']), Url::fromUri($contextBreadcrumb['url'])));
                }
                else {
                  $breadcrumb
                    ->addLink(Link::fromTextAndUrl($this
                    ->trans($contextBreadcrumb['title']), Url::fromUserInput($contextBreadcrumb['url'])));
                }
              }
            }
            else {
              $url = $contextBreadcrumb['url'] === '<nolink>' ? Url::fromRoute($contextBreadcrumb['url']) : Url::fromUserInput($contextBreadcrumb['url'], [
                'language' => $language,
              ]);
              $breadcrumb
                ->addLink(Link::fromTextAndUrl($this
                ->trans($contextBreadcrumb['title']), $url));
            }
          }
        } catch (\Exception $e) {
          $this->logger
            ->get('context_breadcrumb')
            ->error($e
            ->getMessage());
        }
      }
    }
  }
  $params = $route_match
    ->getParameters()
    ->all();
  foreach ($params as $param) {
    if ($param instanceof CacheableDependencyInterface) {
      $breadcrumb
        ->addCacheableDependency($param);
    }
  }
  $breadcrumb
    ->addCacheContexts([
    'url',
  ]);
  $breadcrumb
    ->addCacheTags([
    'context:breadcrumb',
  ]);
  return $breadcrumb;
}