Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Breadcrumb not showing for news article #740

Open
wants to merge 11 commits into
base: 7.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,147 +3,34 @@
namespace Drupal\thunder_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;
use Drupal\taxonomy\TermInterface;

/**
* Class to define the menu_link breadcrumb builder.
*/
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;
}
class ThunderArticleBreadcrumbBuilder extends ThunderTaxonomyTermBreadcrumbBuilderBase {

/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match): bool {
// This breadcrumb apply only for all articles.
$parameters = $route_match->getParameters()->all();
if (($route_match->getRouteName() === 'entity.node.canonical') && is_object($parameters['node'])) {
return $parameters['node']->getType() == 'article';
}
return FALSE;
return ($route_match->getRouteName() === 'entity.node.canonical') && is_object($parameters['node']) && $parameters['node']->getType() === 'article' && !empty($parameters['node']->field_channel->entity);
}

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

// Add all parent forums to breadcrumbs.
protected function getCurrentTerm(RouteMatchInterface $route_match, Breadcrumb $breadcrumb): TermInterface {
/** @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);
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = $node->field_channel->entity;
return $term;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Drupal\thunder_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;
use Drupal\taxonomy\TermInterface;

/**
* Class to define the menu_link breadcrumb builder.
*/
abstract class ThunderTaxonomyTermBreadcrumbBuilderBase implements BreadcrumbBuilderInterface {
use StringTranslationTrait;

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

/**
* 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;
}

/**
* Get the term to produce the breadcrumb for.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Breadcrumb\Breadcrumb $breadcrumb
* The breadcrumb.
*
* @return \Drupal\taxonomy\TermInterface
* The current term.
*/
abstract protected function getCurrentTerm(RouteMatchInterface $route_match, Breadcrumb $breadcrumb): TermInterface;

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

$term = $this->getCurrentTerm($route_match, $breadcrumb);
$breadcrumb->addCacheableDependency($term);

$links = [];
$parents = $this->termStorage->loadAllParents($term->id());
foreach (array_reverse($parents) 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ protected function runAndTestQuery(string $schema): void {

$expectedData = $this->jsonDecode($this->getExpectedResponseFromFile($schema))['data'];

$this->assertEqualsCanonicalizing($expectedData, $responseData);
$this->assertEqualsCanonicalizing(
$expectedData,
$responseData,
'The expected schema for "' . $schema . '" did not match the result.'
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\thunder_news_article\Breadcrumb;

use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\thunder_article\Breadcrumb\ThunderTaxonomyTermBreadcrumbBuilderBase;

/**
* Class to define the menu_link breadcrumb builder.
*/
class ThunderNewsArticleBreadcrumbBuilder extends ThunderTaxonomyTermBreadcrumbBuilderBase {

/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match): bool {
// This breadcrumb apply only for all news articles.
$parameters = $route_match->getParameters()->all();
return ($route_match->getRouteName() === 'entity.node.canonical') && is_object($parameters['node']) && $parameters['node']->getType() === 'news_article' && !empty($parameters['node']->field_channel->entity);
}

/**
* {@inheritdoc}
*/
protected function getCurrentTerm(RouteMatchInterface $route_match, Breadcrumb $breadcrumb): TermInterface {
/** @var \Drupal\node\Entity\Node $node */
$node = $route_match->getParameter('node');
$breadcrumb->addCacheableDependency($node);

/** @var \Drupal\taxonomy\Entity\Term $term */
$term = $node->field_channel->entity;
return $term;
}

}
1 change: 1 addition & 0 deletions modules/thunder_news_article/thunder_news_article.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ core_version_requirement: ^10
package: Thunder
dependencies:
- drupal:node
- thunder:thunder_article
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
thunder_news_article.breadcrumb.default:
class: Drupal\thunder_news_article\Breadcrumb\ThunderNewsArticleBreadcrumbBuilder
arguments: ['@entity_type.manager', '@entity.repository', '@config.factory']
tags:
- { name: breadcrumb_builder, priority: 100 }
Loading