Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Navigation
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 4
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 entityTypeAlter
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 isDisplayBuilderEntityType
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getEntityTypeViewModesIds
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_entity_view\Hook;
6
7use Drupal\Core\Config\ConfigFactoryInterface;
8use Drupal\Core\Entity\EntityTypeInterface;
9use Drupal\Core\Entity\FieldableEntityInterface;
10use Drupal\Core\Hook\Attribute\Hook;
11
12/**
13 * Hook implementations for Navigation module support.
14 */
15class Navigation {
16
17  public function __construct(
18    protected ConfigFactoryInterface $configFactory,
19  ) {}
20
21  /**
22   * Implements hook_entity_type_alter().
23   *
24   * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
25   *   An associative array of entity type definitions.
26   */
27  #[Hook('entity_type_alter')]
28  public function entityTypeAlter(array &$entity_types): void {
29    foreach ($entity_types as $entityTypeId => $entityType) {
30      if (!$this->isDisplayBuilderEntityType($entityType)) {
31        continue;
32      }
33
34      $viewModeIds = $this->getEntityTypeViewModesIds($entityTypeId);
35
36      if (!$viewModeIds) {
37        continue;
38      }
39
40      $canonical = $entityType->getLinkTemplate('canonical');
41
42      foreach ($viewModeIds as $viewModeId) {
43        $entityType->setLinkTemplate("display_builder_override.{$viewModeId}", \sprintf('%s/display/%s', $canonical, $viewModeId));
44      }
45    }
46  }
47
48  /**
49   * Determines if a given entity type is display builder relevant or not.
50   *
51   * @param \Drupal\Core\Entity\EntityTypeInterface $entityType
52   *   The entity type.
53   *
54   * @return bool
55   *   Whether this entity type is a display builder candidate or not.
56   *
57   * @see \Drupal\navigation\EntityRouteHelper::isLayoutBuilderEntityType()
58   * @see \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::getEntityTypes()
59   */
60  protected function isDisplayBuilderEntityType(EntityTypeInterface $entityType): bool {
61    return $entityType->entityClassImplements(FieldableEntityInterface::class)
62      && $entityType->hasViewBuilderClass()
63      && $entityType->hasLinkTemplate('canonical');
64  }
65
66  /**
67   * Get the view mode machine name for this entity type.
68   *
69   * @param string $entityTypeId
70   *   The entity type ID.
71   *
72   * @return array
73   *   The list of view mode IDs.
74   */
75  protected function getEntityTypeViewModesIds(string $entityTypeId): array {
76    $viewModeIds = [];
77    $viewModesList = $this->configFactory->listAll("core.entity_view_mode.{$entityTypeId}.");
78
79    if ($viewModesList) {
80      $viewModes = $this->configFactory->loadMultiple($viewModesList);
81
82      foreach ($viewModes as $viewMode => $viewModeConfig) {
83        $viewModeIds[] = \str_replace("core.entity_view_mode.{$entityTypeId}.", '', $viewMode);
84      }
85    }
86
87    return $viewModeIds;
88  }
89
90}