Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 24 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| DisplayBuilderEntityViewHook | |
0.00% |
0 / 59 |
|
0.00% |
0 / 24 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| entityTypeAlter | |
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| entityOperationAlter | |
0.00% |
0 / 25 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
| entityDelete | |
0.00% |
0 / 26 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\display_builder_entity_view\Hook; |
| 6 | |
| 7 | use Drupal\Core\Entity\EntityInterface; |
| 8 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 9 | use Drupal\Core\Extension\ModuleHandlerInterface; |
| 10 | use Drupal\Core\Hook\Attribute\Hook; |
| 11 | use Drupal\Core\Hook\Order\OrderAfter; |
| 12 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 13 | use Drupal\display_builder\DisplayBuildableInterface; |
| 14 | use Drupal\display_builder\DisplayBuilderHelpers; |
| 15 | use Drupal\display_builder\InstanceInterface; |
| 16 | use Drupal\display_builder_entity_view\Entity\EntityViewDisplay; |
| 17 | use Drupal\display_builder_entity_view\Entity\LayoutBuilderEntityViewDisplay; |
| 18 | use Drupal\display_builder_entity_view\Form\EntityViewDisplayForm; |
| 19 | use Drupal\display_builder_entity_view\Form\LayoutBuilderEntityViewDisplayForm; |
| 20 | use Drupal\display_builder_entity_view\Plugin\display_builder\Buildable\EntityView; |
| 21 | use Drupal\display_builder_entity_view\Plugin\display_builder\Buildable\EntityViewOverride; |
| 22 | |
| 23 | /** |
| 24 | * Hook implementations for display_builder_entity_view. |
| 25 | */ |
| 26 | class DisplayBuilderEntityViewHook { |
| 27 | |
| 28 | public function __construct( |
| 29 | protected ModuleHandlerInterface $moduleHandler, |
| 30 | protected EntityTypeManagerInterface $entityTypeManager, |
| 31 | ) {} |
| 32 | |
| 33 | /** |
| 34 | * Implements hook_entity_type_alter(). |
| 35 | * |
| 36 | * @param array $entity_types |
| 37 | * An associative array of entity type definitions. |
| 38 | */ |
| 39 | #[Hook('entity_type_alter', order: new OrderAfter(['layout_builder']))] |
| 40 | public function entityTypeAlter(array &$entity_types): void { |
| 41 | /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ |
| 42 | if ($this->moduleHandler->moduleExists('layout_builder')) { |
| 43 | $entity_types['entity_view_display'] |
| 44 | ->setClass(LayoutBuilderEntityViewDisplay::class) |
| 45 | ->setFormClass('edit', LayoutBuilderEntityViewDisplayForm::class); |
| 46 | } |
| 47 | else { |
| 48 | $entity_types['entity_view_display'] |
| 49 | ->setClass(EntityViewDisplay::class) |
| 50 | ->setFormClass('edit', EntityViewDisplayForm::class); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Implements hook_entity_operation_alter(). |
| 56 | * |
| 57 | * @param array $operations |
| 58 | * An associative array of operations. |
| 59 | * @param \Drupal\Core\Entity\EntityInterface $entity |
| 60 | * The entity for which the operations are being altered. |
| 61 | */ |
| 62 | #[Hook('entity_operation_alter')] |
| 63 | public function entityOperationAlter(array &$operations, EntityInterface $entity): void { |
| 64 | if (!$entity instanceof InstanceInterface) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $id = (string) $entity->id(); |
| 69 | |
| 70 | if (\str_starts_with($id, EntityView::getPrefix())) { |
| 71 | $operations['build'] = [ |
| 72 | 'title' => new TranslatableMarkup('Build display'), |
| 73 | 'url' => EntityView::getUrlFromInstanceId($id), |
| 74 | 'weight' => -1, |
| 75 | ]; |
| 76 | $operations['edit'] = [ |
| 77 | 'title' => new TranslatableMarkup('Edit display'), |
| 78 | 'url' => EntityView::getDisplayUrlFromInstanceId($id), |
| 79 | 'weight' => 10, |
| 80 | ]; |
| 81 | } |
| 82 | elseif (\str_starts_with($id, EntityViewOverride::getPrefix())) { |
| 83 | $operations['build'] = [ |
| 84 | 'title' => new TranslatableMarkup('Build display'), |
| 85 | 'url' => EntityViewOverride::getUrlFromInstanceId($id), |
| 86 | 'weight' => -1, |
| 87 | ]; |
| 88 | $operations['edit'] = [ |
| 89 | 'title' => new TranslatableMarkup('Edit display'), |
| 90 | 'url' => EntityViewOverride::getUrlFromInstanceId($id), |
| 91 | 'weight' => 10, |
| 92 | ]; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Implements hook_entity_delete(). |
| 98 | * |
| 99 | * If the entity deleted has a display override, need to be deleted as well. |
| 100 | */ |
| 101 | #[Hook('entity_delete')] |
| 102 | public function entityDelete(EntityInterface $entity): void { |
| 103 | $entity_type_id = $entity->getEntityTypeId(); |
| 104 | |
| 105 | if ($entity_type_id === 'display_builder_instance') { |
| 106 | return; |
| 107 | } |
| 108 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 109 | |
| 110 | // Only entities with display are concerned. |
| 111 | if (!DisplayBuilderHelpers::isDisplayBuilderEntityType($entity_type)) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $displays = $this->entityTypeManager->getStorage('entity_view_display')->loadByProperties([ |
| 116 | 'targetEntityType' => $entity_type_id, |
| 117 | 'bundle' => $entity->bundle(), |
| 118 | ]); |
| 119 | |
| 120 | $storage = $this->entityTypeManager->getStorage('display_builder_instance'); |
| 121 | |
| 122 | foreach ($displays as $display) { |
| 123 | // @phpstan-ignore-next-line |
| 124 | if (!$display->getDisplayBuilderOverrideField()) { |
| 125 | continue; |
| 126 | } |
| 127 | $field_name = $display->getThirdPartySetting('display_builder', DisplayBuildableInterface::OVERRIDE_FIELD_PROPERTY); |
| 128 | |
| 129 | $instance_id = \sprintf( |
| 130 | '%s%s__%s__%s', |
| 131 | EntityViewOverride::getPrefix(), |
| 132 | $entity_type_id, |
| 133 | $entity->id(), |
| 134 | $field_name, |
| 135 | ); |
| 136 | |
| 137 | $instance = $storage->load($instance_id); |
| 138 | |
| 139 | if (!$instance) { |
| 140 | return; |
| 141 | } |
| 142 | $storage->delete([$instance]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | } |
Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not
necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once.
Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 29 | protected ModuleHandlerInterface $moduleHandler, |
| 30 | protected EntityTypeManagerInterface $entityTypeManager, |
| 31 | ) {} |
| 102 | public function entityDelete(EntityInterface $entity): void { |
| 103 | $entity_type_id = $entity->getEntityTypeId(); |
| 104 | |
| 105 | if ($entity_type_id === 'display_builder_instance') { |
| 106 | return; |
| 108 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 109 | |
| 110 | // Only entities with display are concerned. |
| 111 | if (!DisplayBuilderHelpers::isDisplayBuilderEntityType($entity_type)) { |
| 112 | return; |
| 115 | $displays = $this->entityTypeManager->getStorage('entity_view_display')->loadByProperties([ |
| 116 | 'targetEntityType' => $entity_type_id, |
| 117 | 'bundle' => $entity->bundle(), |
| 118 | ]); |
| 119 | |
| 120 | $storage = $this->entityTypeManager->getStorage('display_builder_instance'); |
| 121 | |
| 122 | foreach ($displays as $display) { |
| 122 | foreach ($displays as $display) { |
| 124 | if (!$display->getDisplayBuilderOverrideField()) { |
| 125 | continue; |
| 127 | $field_name = $display->getThirdPartySetting('display_builder', DisplayBuildableInterface::OVERRIDE_FIELD_PROPERTY); |
| 128 | |
| 129 | $instance_id = \sprintf( |
| 130 | '%s%s__%s__%s', |
| 131 | EntityViewOverride::getPrefix(), |
| 132 | $entity_type_id, |
| 133 | $entity->id(), |
| 134 | $field_name, |
| 135 | ); |
| 136 | |
| 137 | $instance = $storage->load($instance_id); |
| 138 | |
| 139 | if (!$instance) { |
| 140 | return; |
| 122 | foreach ($displays as $display) { |
| 123 | // @phpstan-ignore-next-line |
| 124 | if (!$display->getDisplayBuilderOverrideField()) { |
| 125 | continue; |
| 126 | } |
| 127 | $field_name = $display->getThirdPartySetting('display_builder', DisplayBuildableInterface::OVERRIDE_FIELD_PROPERTY); |
| 128 | |
| 129 | $instance_id = \sprintf( |
| 130 | '%s%s__%s__%s', |
| 131 | EntityViewOverride::getPrefix(), |
| 132 | $entity_type_id, |
| 133 | $entity->id(), |
| 134 | $field_name, |
| 135 | ); |
| 136 | |
| 137 | $instance = $storage->load($instance_id); |
| 138 | |
| 139 | if (!$instance) { |
| 140 | return; |
| 141 | } |
| 142 | $storage->delete([$instance]); |
| 122 | foreach ($displays as $display) { |
| 123 | // @phpstan-ignore-next-line |
| 124 | if (!$display->getDisplayBuilderOverrideField()) { |
| 125 | continue; |
| 126 | } |
| 127 | $field_name = $display->getThirdPartySetting('display_builder', DisplayBuildableInterface::OVERRIDE_FIELD_PROPERTY); |
| 128 | |
| 129 | $instance_id = \sprintf( |
| 130 | '%s%s__%s__%s', |
| 131 | EntityViewOverride::getPrefix(), |
| 132 | $entity_type_id, |
| 133 | $entity->id(), |
| 134 | $field_name, |
| 135 | ); |
| 136 | |
| 137 | $instance = $storage->load($instance_id); |
| 138 | |
| 139 | if (!$instance) { |
| 140 | return; |
| 141 | } |
| 142 | $storage->delete([$instance]); |
| 143 | } |
| 144 | } |
| 63 | public function entityOperationAlter(array &$operations, EntityInterface $entity): void { |
| 64 | if (!$entity instanceof InstanceInterface) { |
| 65 | return; |
| 68 | $id = (string) $entity->id(); |
| 69 | |
| 70 | if (\str_starts_with($id, EntityView::getPrefix())) { |
| 70 | if (\str_starts_with($id, EntityView::getPrefix())) { |
| 71 | $operations['build'] = [ |
| 72 | 'title' => new TranslatableMarkup('Build display'), |
| 82 | elseif (\str_starts_with($id, EntityViewOverride::getPrefix())) { |
| 84 | 'title' => new TranslatableMarkup('Build display'), |
| 85 | 'url' => EntityViewOverride::getUrlFromInstanceId($id), |
| 86 | 'weight' => -1, |
| 87 | ]; |
| 88 | $operations['edit'] = [ |
| 89 | 'title' => new TranslatableMarkup('Edit display'), |
| 90 | 'url' => EntityViewOverride::getUrlFromInstanceId($id), |
| 91 | 'weight' => 10, |
| 92 | ]; |
| 93 | } |
| 94 | } |
| 94 | } |
| 40 | public function entityTypeAlter(array &$entity_types): void { |
| 41 | /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ |
| 42 | if ($this->moduleHandler->moduleExists('layout_builder')) { |
| 42 | if ($this->moduleHandler->moduleExists('layout_builder')) { |
| 43 | $entity_types['entity_view_display'] |
| 48 | $entity_types['entity_view_display'] |
| 49 | ->setClass(EntityViewDisplay::class) |
| 50 | ->setFormClass('edit', EntityViewDisplayForm::class); |
| 51 | } |
| 52 | } |
| 52 | } |