Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 17 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DisplayBuilderEntityViewHook | |
0.00% |
0 / 34 |
|
0.00% |
0 / 17 |
|
0.00% |
0 / 10 |
|
0.00% |
0 / 3 |
90 | |
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 | |||
| 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\display_builder\DisplayBuildableInterface; |
| 13 | use Drupal\display_builder\DisplayBuilderHelpers; |
| 14 | use Drupal\display_builder_entity_view\Entity\EntityViewDisplay; |
| 15 | use Drupal\display_builder_entity_view\Entity\LayoutBuilderEntityViewDisplay; |
| 16 | use Drupal\display_builder_entity_view\Form\EntityViewDisplayForm; |
| 17 | use Drupal\display_builder_entity_view\Form\LayoutBuilderEntityViewDisplayForm; |
| 18 | use Drupal\display_builder_entity_view\Plugin\display_builder\Buildable\EntityViewOverride; |
| 19 | |
| 20 | /** |
| 21 | * Hook implementations for display_builder_entity_view. |
| 22 | */ |
| 23 | class DisplayBuilderEntityViewHook { |
| 24 | |
| 25 | public function __construct( |
| 26 | protected ModuleHandlerInterface $moduleHandler, |
| 27 | protected EntityTypeManagerInterface $entityTypeManager, |
| 28 | ) {} |
| 29 | |
| 30 | /** |
| 31 | * Implements hook_entity_type_alter(). |
| 32 | * |
| 33 | * @param array $entity_types |
| 34 | * An associative array of entity type definitions. |
| 35 | */ |
| 36 | #[Hook('entity_type_alter', order: new OrderAfter(['layout_builder']))] |
| 37 | public function entityTypeAlter(array &$entity_types): void { |
| 38 | /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ |
| 39 | if ($this->moduleHandler->moduleExists('layout_builder')) { |
| 40 | $entity_types['entity_view_display'] |
| 41 | ->setClass(LayoutBuilderEntityViewDisplay::class) |
| 42 | ->setFormClass('edit', LayoutBuilderEntityViewDisplayForm::class); |
| 43 | } |
| 44 | else { |
| 45 | $entity_types['entity_view_display'] |
| 46 | ->setClass(EntityViewDisplay::class) |
| 47 | ->setFormClass('edit', EntityViewDisplayForm::class); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Implements hook_entity_delete(). |
| 53 | * |
| 54 | * If the entity deleted has a display override, need to be deleted as well. |
| 55 | */ |
| 56 | #[Hook('entity_delete')] |
| 57 | public function entityDelete(EntityInterface $entity): void { |
| 58 | $entity_type_id = $entity->getEntityTypeId(); |
| 59 | |
| 60 | if ($entity_type_id === 'display_builder_instance') { |
| 61 | return; |
| 62 | } |
| 63 | $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
| 64 | |
| 65 | // Only entities with display are concerned. |
| 66 | if (!DisplayBuilderHelpers::isDisplayBuilderEntityType($entity_type)) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | $displays = $this->entityTypeManager->getStorage('entity_view_display')->loadByProperties([ |
| 71 | 'targetEntityType' => $entity_type_id, |
| 72 | 'bundle' => $entity->bundle(), |
| 73 | ]); |
| 74 | |
| 75 | $storage = $this->entityTypeManager->getStorage('display_builder_instance'); |
| 76 | |
| 77 | foreach ($displays as $display) { |
| 78 | // @phpstan-ignore-next-line |
| 79 | if (!$display->getDisplayBuilderOverrideField()) { |
| 80 | continue; |
| 81 | } |
| 82 | $field_name = $display->getThirdPartySetting('display_builder', DisplayBuildableInterface::OVERRIDE_FIELD_PROPERTY); |
| 83 | |
| 84 | $instance_id = \sprintf( |
| 85 | '%s%s__%s__%s', |
| 86 | EntityViewOverride::getPrefix(), |
| 87 | $entity_type_id, |
| 88 | $entity->id(), |
| 89 | $field_name, |
| 90 | ); |
| 91 | |
| 92 | $instance = $storage->load($instance_id); |
| 93 | |
| 94 | if (!$instance) { |
| 95 | return; |
| 96 | } |
| 97 | $storage->delete([$instance]); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } |