Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 18 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TemplateOverride | |
0.00% |
0 / 26 |
|
0.00% |
0 / 18 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| entityView | |
0.00% |
0 / 3 |
|
0.00% |
0 / 3 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| suggestionsAlter | |
0.00% |
0 / 5 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| themeRegistryAlter | |
0.00% |
0 / 17 |
|
0.00% |
0 / 8 |
|
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder_entity_view\Hook; |
| 6 | |
| 7 | use Drupal\Core\Entity\ContentEntityTypeInterface; |
| 8 | use Drupal\Core\Entity\Display\EntityViewDisplayInterface; |
| 9 | use Drupal\Core\Entity\EntityInterface; |
| 10 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 11 | use Drupal\Core\Extension\ModuleExtensionList; |
| 12 | use Drupal\Core\Hook\Attribute\Hook; |
| 13 | use Drupal\Core\Hook\Order\Order; |
| 14 | use Drupal\display_builder_entity_view\Entity\DisplayBuilderEntityDisplayInterface; |
| 15 | |
| 16 | /** |
| 17 | * Hook implementations to override entity template. |
| 18 | */ |
| 19 | class TemplateOverride { |
| 20 | |
| 21 | /** |
| 22 | * Key to store info for template override. |
| 23 | */ |
| 24 | public const KEY = 'display_builder_entity_view'; |
| 25 | |
| 26 | public function __construct( |
| 27 | protected ModuleExtensionList $moduleExtensionList, |
| 28 | protected EntityTypeManagerInterface $entityTypeManager, |
| 29 | ) {} |
| 30 | |
| 31 | /** |
| 32 | * Implements hook_entity_view(). |
| 33 | * |
| 34 | * @param array $build |
| 35 | * The renderable array representing the entity content. |
| 36 | * @param \Drupal\Core\Entity\EntityInterface $entity |
| 37 | * The entity being viewed. |
| 38 | * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display |
| 39 | * The display configuration entity to be used to build the entity. |
| 40 | * @param string $view_mode |
| 41 | * The view mode in which the entity is being viewed. |
| 42 | * |
| 43 | * phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 44 | */ |
| 45 | #[Hook('entity_view')] |
| 46 | public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, string $view_mode): void { |
| 47 | if (!$display instanceof DisplayBuilderEntityDisplayInterface) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // @todo Is isDisplayBuilderEnabled enough? |
| 52 | $build['#' . $this::KEY] = $display->isDisplayBuilderEnabled(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Implements hook_theme_suggestions_alter(). |
| 57 | * |
| 58 | * @param array $suggestions |
| 59 | * An array of theme hook suggestions. |
| 60 | * @param array $variables |
| 61 | * An array of variables to pass to the theme function or template. |
| 62 | * @param string $hook |
| 63 | * The name of the theme hook being invoked. |
| 64 | */ |
| 65 | #[Hook('theme_suggestions_alter', order: Order::Last)] |
| 66 | public function suggestionsAlter(array &$suggestions, array $variables, string $hook): void { |
| 67 | $key = '#' . $this::KEY; |
| 68 | |
| 69 | if (!isset($variables['elements'][$key]) || empty($variables['elements'][$key])) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (isset($variables['theme_hook_original'])) { |
| 74 | $suggestions[] = $variables['theme_hook_original'] . '__display_builder'; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Implements hook_theme_registry_alter(). |
| 80 | * |
| 81 | * Add entry for display builder suggestion. |
| 82 | */ |
| 83 | #[Hook('theme_registry_alter')] |
| 84 | public function themeRegistryAlter(array &$theme_registry): void { |
| 85 | $modulePath = $this->moduleExtensionList->getPath('display_builder_entity_view'); |
| 86 | $templatePath = $modulePath . '/templates'; |
| 87 | $entityDefinitions = $this->entityTypeManager->getDefinitions(); |
| 88 | |
| 89 | foreach ($entityDefinitions as $entityTypeId => $entityType) { |
| 90 | if (!$entityType instanceof ContentEntityTypeInterface) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | if (!isset($theme_registry[$entityTypeId])) { |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | $theme_registry[$entityTypeId . '__display_builder'] = [ |
| 99 | // @todo Does the base hook always match the entity type ID? |
| 100 | 'base hook' => $entityTypeId, |
| 101 | 'path' => $templatePath, |
| 102 | 'preprocess functions' => ['display_builder_entity_view_preprocess_entity'], |
| 103 | 'render element' => 'elements', |
| 104 | 'template' => 'entity', |
| 105 | 'theme path' => $modulePath, |
| 106 | 'type' => 'base_theme_engine', |
| 107 | ]; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | } |