-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Breadcrumb not showing for news article
- Loading branch information
1 parent
0f80c3b
commit 281e3fc
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
modules/thunder_news_article/src/Breadcrumb/ThunderNewsArticleBreadcrumbBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
<?php | ||
|
||
namespace Drupal\thunder_news_article\Breadcrumb; | ||
|
||
use Drupal\Core\Breadcrumb\Breadcrumb; | ||
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Entity\EntityRepositoryInterface; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Link; | ||
use Drupal\Core\Routing\RouteMatchInterface; | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
|
||
/** | ||
* Class to define the menu_link breadcrumb builder. | ||
*/ | ||
class ThunderNewsArticleBreadcrumbBuilder 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 ThunderNewsArticleBreadcrumbBuilder. | ||
* | ||
* @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): bool { | ||
// This breadcrumb apply only for all news articles. | ||
$parameters = $route_match->getParameters()->all(); | ||
if (($route_match->getRouteName() === 'entity.node.canonical') && is_object($parameters['node'])) { | ||
return $parameters['node']->getType() === 'news_article'; | ||
} | ||
return FALSE; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function build(RouteMatchInterface $route_match): Breadcrumb { | ||
$breadcrumb = new Breadcrumb(); | ||
$breadcrumb->addCacheContexts(['route']); | ||
|
||
// Add all parent forums to breadcrumbs. | ||
/** @var \Drupal\node\Entity\Node $node */ | ||
$node = $route_match->getParameter('node'); | ||
$breadcrumb->addCacheableDependency($node); | ||
|
||
// Add all parent forums to breadcrumbs. | ||
/** @var \Drupal\taxonomy\TermInterface|NULL $term */ | ||
$term = !empty($node->field_channel) ? $node->field_channel->entity : NULL; | ||
|
||
$links = []; | ||
if ($term) { | ||
$breadcrumb->addCacheableDependency($term); | ||
|
||
$channels = $this->termStorage->loadAllParents($term->id()); | ||
foreach (array_reverse($channels) as $term) { | ||
/** @var \Drupal\taxonomy\TermInterface $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); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
modules/thunder_news_article/thunder_news_article.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
thunder_article.breadcrumb.default: | ||
class: Drupal\thunder_news_article\Breadcrumb\ThunderNewsArticleBreadcrumbBuilder | ||
arguments: ['@entity_type.manager', '@entity.repository', '@config.factory'] | ||
tags: | ||
- { name: breadcrumb_builder, priority: 100 } |