Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TemplateOverride
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 4
132
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
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 entityView
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 suggestionsAlter
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 themeRegistryAlter
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder_entity_view\Hook;
6
7use Drupal\Core\Entity\ContentEntityTypeInterface;
8use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
9use Drupal\Core\Entity\EntityInterface;
10use Drupal\Core\Entity\EntityTypeManagerInterface;
11use Drupal\Core\Extension\ModuleExtensionList;
12use Drupal\Core\Hook\Attribute\Hook;
13use Drupal\Core\Hook\Order\Order;
14use Drupal\display_builder_entity_view\Entity\DisplayBuilderEntityDisplayInterface;
15
16/**
17 * Hook implementations to override entity template.
18 */
19class 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}

Paths

Below are the source code lines that represent each code path as identified by Xdebug. Please note a path is not necessarily coterminous with a line, a line may contain multiple paths and therefore show up more than once. Please also be aware that some paths may include implicit rather than explicit branches, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

TemplateOverride->__construct
26  public function __construct(
27    protected ModuleExtensionList $moduleExtensionList,
28    protected EntityTypeManagerInterface $entityTypeManager,
29  ) {}
TemplateOverride->entityView
46  public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, string $view_mode): void {
47    if (!$display instanceof DisplayBuilderEntityDisplayInterface) {
 
48      return;
46  public function entityView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, string $view_mode): void {
47    if (!$display instanceof DisplayBuilderEntityDisplayInterface) {
 
52    $build['#' . $this::KEY] = $display->isDisplayBuilderEnabled();
53  }
TemplateOverride->suggestionsAlter
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])) {
 
69    if (!isset($variables['elements'][$key]) || empty($variables['elements'][$key])) {
 
70      return;
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])) {
 
69    if (!isset($variables['elements'][$key]) || empty($variables['elements'][$key])) {
 
73    if (isset($variables['theme_hook_original'])) {
 
74      $suggestions[] = $variables['theme_hook_original'] . '__display_builder';
75    }
76  }
 
76  }
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])) {
 
69    if (!isset($variables['elements'][$key]) || empty($variables['elements'][$key])) {
 
73    if (isset($variables['theme_hook_original'])) {
 
76  }
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;
TemplateOverride->themeRegistryAlter
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) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
90      if (!$entityType instanceof ContentEntityTypeInterface) {
 
91        continue;
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
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  }
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) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
90      if (!$entityType instanceof ContentEntityTypeInterface) {
 
94      if (!isset($theme_registry[$entityTypeId])) {
 
95        continue;
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
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  }
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) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
90      if (!$entityType instanceof ContentEntityTypeInterface) {
 
94      if (!isset($theme_registry[$entityTypeId])) {
 
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'] = [
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
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  }
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) {
 
89    foreach ($entityDefinitions as $entityTypeId => $entityType) {
 
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  }
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) {
 
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  }