Skip to content
Snippets Groups Projects

#3251306 Breadcrumbs are translated incorrectly.

5 files
+ 241
25
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 32
13
@@ -10,6 +10,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Language\LanguageInterface;
@@ -28,10 +29,6 @@ use Symfony\Component\HttpFoundation\RequestStack;
/**
* Breadcrumb builder class for generating breadcrumbs.
*
* Builds breadcrumbs within the context of the abv_app Drupal package.
*
* @package Drupal\abv_app
*/
class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
@@ -112,6 +109,12 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected $fileUrlGenerator;
/**
* EntityRepository service.
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* BreadcrumbBuilder constructor.
*
@@ -133,10 +136,22 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
* Path matcher.
* @param \Drupal\Core\Routing\AdminContext $routerAdminContext
* Router admin context.
* @param \Drupal\Core\File\FileUrlGeneratorInterface|null $fileUrlGenerator
* @param \Drupal\Core\File\FileUrlGeneratorInterface $fileUrlGenerator
* FileUrlGenerator service.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
* EntityRepository service.
*/
public function __construct(ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager, LanguageManagerInterface $languageManager, RequestStack $requestStack, TitleResolverInterface $titleResolver, Token $token, AliasManagerInterface $aliasManager, PathMatcherInterface $pathMatcher, AdminContext $routerAdminContext, FileUrlGeneratorInterface $fileUrlGenerator) {
public function __construct(ConfigFactoryInterface $configFactory,
EntityTypeManagerInterface $entityTypeManager,
LanguageManagerInterface $languageManager,
RequestStack $requestStack,
TitleResolverInterface $titleResolver,
Token $token,
AliasManagerInterface $aliasManager,
PathMatcherInterface $pathMatcher,
AdminContext $routerAdminContext,
FileUrlGeneratorInterface $fileUrlGenerator,
EntityRepositoryInterface $entityRepository) {
$this->entityTypeManager = $entityTypeManager;
$this->languageManager = $languageManager;
$this->token = $token;
@@ -148,6 +163,7 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
$this->pathMatcher = $pathMatcher;
$this->routerAdminContext = $routerAdminContext;
$this->fileUrlGenerator = $fileUrlGenerator;
$this->entityRepository = $entityRepository;
}
/**
@@ -239,7 +255,7 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
* Route match.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException|\Drupal\Core\Entity\EntityMalformedException
*/
protected function applyContentEntityBreadcrumb(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match) {
// Prepare all route parameters.
@@ -277,7 +293,7 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
* Entity.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException|\Drupal\Core\Entity\EntityMalformedException
*/
protected function applyBreadcrumb(Breadcrumb &$breadcrumb, CustomBreadcrumbs $customBreadcrumbs, $entity) {
$paths = $customBreadcrumbs->getMultiValues('breadcrumbPaths');
@@ -312,8 +328,11 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
if ($entity->hasField($field_name)) {
$term = $entity->get($field_name)->entity;
if ($term instanceof TermInterface) {
$langcode = $entity->language()->getId();
$parents = $this->getAllParents($term->id());
/** @var \Drupal\taxonomy\TermInterface $parent */
foreach (array_reverse($parents) as $parent) {
$parent = $this->entityRepository->getTranslationFromContext($parent, $langcode);
$link = $parent->toLink($this->prepareTitle($parent->label()));
$breadcrumb->addLink($link);
$breadcrumb->addCacheableDependency($parent);
@@ -346,13 +365,13 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
* @param int $tid
* Term id.
*
* @return mixed
* @return array
* List of entities.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getAllParents($tid) {
protected function getAllParents(int $tid): array {
return $this->entityTypeManager->getStorage("taxonomy_term")
->loadAllParents($tid);
}
@@ -366,8 +385,8 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
* @return bool
* True or false.
*/
protected function checkHierarchyToken($href) {
if (strpos($href, '<term_hierarchy:') !== FALSE) {
protected function checkHierarchyToken(string $href): bool {
if (str_contains($href, '<term_hierarchy:')) {
return TRUE;
}
@@ -482,7 +501,7 @@ class BreadcrumbBuilder implements BreadcrumbBuilderInterface {
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function matchEntity(RouteMatchInterface $route_match) {
protected function matchEntity(RouteMatchInterface $route_match): bool {
$params = $route_match->getParameters()->all();
$entityTypeIds = array_keys($params);
Loading