You are here

class ThunderArticleBreadcrumbBuilder in Thunder 8.5

Same name and namespace in other branches
  1. 8.2 modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder
  2. 8.3 modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder
  3. 8.4 modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder
  4. 6.2.x modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder
  5. 6.0.x modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder
  6. 6.1.x modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php \Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder

Class to define the menu_link breadcrumb builder.

Hierarchy

Expanded class hierarchy of ThunderArticleBreadcrumbBuilder

1 string reference to 'ThunderArticleBreadcrumbBuilder'
thunder_article.services.yml in modules/thunder_article/thunder_article.services.yml
modules/thunder_article/thunder_article.services.yml
1 service uses ThunderArticleBreadcrumbBuilder
thunder_article.breadcrumb.default in modules/thunder_article/thunder_article.services.yml
Drupal\thunder_article\Breadcrumb\ThunderArticleBreadcrumbBuilder

File

modules/thunder_article/src/Breadcrumb/ThunderArticleBreadcrumbBuilder.php, line 22

Namespace

Drupal\thunder_article\Breadcrumb
View source
class ThunderArticleBreadcrumbBuilder implements BreadcrumbBuilderInterface {
  use StringTranslationTrait;

  /**
   * The router request context.
   *
   * @var \Drupal\Core\Routing\RequestContext
   */
  protected $context;

  /**
   * The menu link access service.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * The dynamic router service.
   *
   * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface
   */
  protected $router;

  /**
   * The dynamic router service.
   *
   * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
   */
  protected $pathProcessor;

  /**
   * Site configFactory object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The title resolver.
   *
   * @var \Drupal\Core\Controller\TitleResolverInterface
   */
  protected $titleResolver;

  /**
   * The current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The entity repository service.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * The taxonomy storage.
   *
   * @var \Drupal\Taxonomy\TermStorageInterface
   */
  protected $termStorage;

  /**
   * Constructs the ThunderArticleBreadcrumbBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
   *   The entity repository service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityRepositoryInterface $entityRepository, ConfigFactoryInterface $configFactory) {
    $this->entityRepository = $entityRepository;
    $this->termStorage = $entityTypeManager
      ->getStorage('taxonomy_term');
    $this->configFactory = $configFactory;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $route_match) {

    // This breadcrumb apply only for all articles.
    $parameters = $route_match
      ->getParameters()
      ->all();
    if (isset($parameters['node']) && is_object($parameters['node'])) {
      return $parameters['node']
        ->getType() == 'article';
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
    $breadcrumb = new Breadcrumb();
    $breadcrumb
      ->addCacheContexts([
      'route',
    ]);

    // Add all parent forums to breadcrumbs.

    /** @var Node $node */
    $node = $route_match
      ->getParameter('node');
    $breadcrumb
      ->addCacheableDependency($node);

    // Add all parent forums to breadcrumbs.

    /** @var \Drupal\Taxonomy\TermInterface $term */
    $term = !empty($node->field_channel) ? $node->field_channel->entity : '';
    $links = [];
    if ($term) {
      $breadcrumb
        ->addCacheableDependency($term);
      $channels = $this->termStorage
        ->loadAllParents($term
        ->id());
      foreach (array_reverse($channels) as $term) {
        $term = $this->entityRepository
          ->getTranslationFromContext($term);
        $breadcrumb
          ->addCacheableDependency($term);
        $links[] = Link::createFromRoute($term
          ->getName(), 'entity.taxonomy_term.canonical', [
          'taxonomy_term' => $term
            ->id(),
        ]);
      }
    }
    if (!$links || '/' . $links[0]
      ->getUrl()
      ->getInternalPath() != $this->configFactory
      ->get('system.site')
      ->get('page.front')) {
      array_unshift($links, Link::createFromRoute($this
        ->t('Home'), '<front>'));
    }
    return $breadcrumb
      ->setLinks($links);
  }

  /**
   * Matches a path in the router.
   *
   * @param string $path
   *   The request path with a leading slash.
   * @param array $exclude
   *   An array of paths or system paths to skip.
   *
   * @return \Symfony\Component\HttpFoundation\Request
   *   A populated request object or NULL if the path couldn't be matched.
   */
  protected function getRequestForPath($path, array $exclude) {
    if (!empty($exclude[$path])) {
      return NULL;
    }

    // @todo Use the RequestHelper once https://www.drupal.org/node/2090293 is
    //   fixed.
    $request = Request::create($path);

    // Performance optimization: set a short accept header to reduce overhead in
    // AcceptHeaderMatcher when matching the request.
    $request->headers
      ->set('Accept', 'text/html');

    // Find the system path by resolving aliases, language prefix, etc.
    $processed = $this->pathProcessor
      ->processInbound($path, $request);
    if (empty($processed) || !empty($exclude[$processed])) {

      // This resolves to the front page, which we already add.
      return NULL;
    }
    $this->currentPath
      ->setPath($processed, $request);

    // Attempt to match this path to provide a fully built request.
    try {
      $request->attributes
        ->add($this->router
        ->matchRequest($request));
      return $request;
    } catch (ParamNotConvertedException $e) {
      return NULL;
    } catch (ResourceNotFoundException $e) {
      return NULL;
    } catch (MethodNotAllowedException $e) {
      return NULL;
    } catch (AccessDeniedHttpException $e) {
      return NULL;
    }
  }

}

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.
ThunderArticleBreadcrumbBuilder::$accessManager protected property The menu link access service.
ThunderArticleBreadcrumbBuilder::$configFactory protected property Site configFactory object.
ThunderArticleBreadcrumbBuilder::$context protected property The router request context.
ThunderArticleBreadcrumbBuilder::$currentUser protected property The current user object.
ThunderArticleBreadcrumbBuilder::$entityRepository protected property The entity repository service.
ThunderArticleBreadcrumbBuilder::$pathProcessor protected property The dynamic router service.
ThunderArticleBreadcrumbBuilder::$router protected property The dynamic router service.
ThunderArticleBreadcrumbBuilder::$termStorage protected property The taxonomy storage.
ThunderArticleBreadcrumbBuilder::$titleResolver protected property The title resolver.
ThunderArticleBreadcrumbBuilder::applies public function Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface::applies
ThunderArticleBreadcrumbBuilder::build public function Builds the breadcrumb. Overrides BreadcrumbBuilderInterface::build
ThunderArticleBreadcrumbBuilder::getRequestForPath protected function Matches a path in the router.
ThunderArticleBreadcrumbBuilder::__construct public function Constructs the ThunderArticleBreadcrumbBuilder.