|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\thunder_news_article\Breadcrumb; |
| 4 | + |
| 5 | +use Drupal\Core\Breadcrumb\Breadcrumb; |
| 6 | +use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; |
| 7 | +use Drupal\Core\Config\ConfigFactoryInterface; |
| 8 | +use Drupal\Core\Entity\EntityRepositoryInterface; |
| 9 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 10 | +use Drupal\Core\Link; |
| 11 | +use Drupal\Core\Routing\RouteMatchInterface; |
| 12 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class to define the menu_link breadcrumb builder. |
| 16 | + */ |
| 17 | +class ThunderNewsArticleBreadcrumbBuilder implements BreadcrumbBuilderInterface { |
| 18 | + use StringTranslationTrait; |
| 19 | + |
| 20 | + /** |
| 21 | + * The router request context. |
| 22 | + * |
| 23 | + * @var \Drupal\Core\Routing\RequestContext |
| 24 | + */ |
| 25 | + protected $context; |
| 26 | + |
| 27 | + /** |
| 28 | + * The menu link access service. |
| 29 | + * |
| 30 | + * @var \Drupal\Core\Access\AccessManagerInterface |
| 31 | + */ |
| 32 | + protected $accessManager; |
| 33 | + |
| 34 | + /** |
| 35 | + * The dynamic router service. |
| 36 | + * |
| 37 | + * @var \Symfony\Component\Routing\Matcher\RequestMatcherInterface |
| 38 | + */ |
| 39 | + protected $router; |
| 40 | + |
| 41 | + /** |
| 42 | + * The dynamic router service. |
| 43 | + * |
| 44 | + * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface |
| 45 | + */ |
| 46 | + protected $pathProcessor; |
| 47 | + |
| 48 | + /** |
| 49 | + * Site configFactory object. |
| 50 | + * |
| 51 | + * @var \Drupal\Core\Config\ConfigFactoryInterface |
| 52 | + */ |
| 53 | + protected $configFactory; |
| 54 | + |
| 55 | + /** |
| 56 | + * The title resolver. |
| 57 | + * |
| 58 | + * @var \Drupal\Core\Controller\TitleResolverInterface |
| 59 | + */ |
| 60 | + protected $titleResolver; |
| 61 | + |
| 62 | + /** |
| 63 | + * The current user object. |
| 64 | + * |
| 65 | + * @var \Drupal\Core\Session\AccountInterface |
| 66 | + */ |
| 67 | + protected $currentUser; |
| 68 | + |
| 69 | + /** |
| 70 | + * The entity repository service. |
| 71 | + * |
| 72 | + * @var \Drupal\Core\Entity\EntityRepositoryInterface |
| 73 | + */ |
| 74 | + protected $entityRepository; |
| 75 | + |
| 76 | + /** |
| 77 | + * The taxonomy storage. |
| 78 | + * |
| 79 | + * @var \Drupal\taxonomy\TermStorageInterface |
| 80 | + */ |
| 81 | + protected $termStorage; |
| 82 | + |
| 83 | + /** |
| 84 | + * Constructs the ThunderNewsArticleBreadcrumbBuilder. |
| 85 | + * |
| 86 | + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
| 87 | + * The entity type manager service. |
| 88 | + * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository |
| 89 | + * The entity repository service. |
| 90 | + * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory |
| 91 | + * The config factory. |
| 92 | + * |
| 93 | + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
| 94 | + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
| 95 | + */ |
| 96 | + public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityRepositoryInterface $entityRepository, ConfigFactoryInterface $configFactory) { |
| 97 | + $this->entityRepository = $entityRepository; |
| 98 | + $this->termStorage = $entityTypeManager->getStorage('taxonomy_term'); |
| 99 | + $this->configFactory = $configFactory; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * {@inheritdoc} |
| 104 | + */ |
| 105 | + public function applies(RouteMatchInterface $route_match): bool { |
| 106 | + // This breadcrumb apply only for all news articles. |
| 107 | + $parameters = $route_match->getParameters()->all(); |
| 108 | + if (($route_match->getRouteName() === 'entity.node.canonical') && is_object($parameters['node'])) { |
| 109 | + return $parameters['node']->getType() === 'news_article'; |
| 110 | + } |
| 111 | + return FALSE; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * {@inheritdoc} |
| 116 | + */ |
| 117 | + public function build(RouteMatchInterface $route_match): Breadcrumb { |
| 118 | + $breadcrumb = new Breadcrumb(); |
| 119 | + $breadcrumb->addCacheContexts(['route']); |
| 120 | + |
| 121 | + // Add all parent forums to breadcrumbs. |
| 122 | + /** @var \Drupal\node\Entity\Node $node */ |
| 123 | + $node = $route_match->getParameter('node'); |
| 124 | + $breadcrumb->addCacheableDependency($node); |
| 125 | + |
| 126 | + // Add all parent forums to breadcrumbs. |
| 127 | + /** @var \Drupal\taxonomy\TermInterface|NULL $term */ |
| 128 | + $term = !empty($node->field_channel) ? $node->field_channel->entity : NULL; |
| 129 | + |
| 130 | + $links = []; |
| 131 | + if ($term) { |
| 132 | + $breadcrumb->addCacheableDependency($term); |
| 133 | + |
| 134 | + $channels = $this->termStorage->loadAllParents($term->id()); |
| 135 | + foreach (array_reverse($channels) as $term) { |
| 136 | + /** @var \Drupal\taxonomy\TermInterface $term */ |
| 137 | + $term = $this->entityRepository->getTranslationFromContext($term); |
| 138 | + $breadcrumb->addCacheableDependency($term); |
| 139 | + $links[] = Link::createFromRoute($term->getName(), 'entity.taxonomy_term.canonical', ['taxonomy_term' => $term->id()]); |
| 140 | + } |
| 141 | + } |
| 142 | + if (!$links || '/' . $links[0]->getUrl()->getInternalPath() != $this->configFactory->get('system.site')->get('page.front')) { |
| 143 | + array_unshift($links, Link::createFromRoute($this->t('Home'), '<front>')); |
| 144 | + } |
| 145 | + |
| 146 | + return $breadcrumb->setLinks($links); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments