Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DisplayBuilderEntityViewHook | |
0.00% |
0 / 46 |
|
0.00% |
0 / 14 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fieldInfoAlter | |
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 | |||
| displayBuilderProviderInfo | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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\Extension\ModuleHandlerInterface; |
| 9 | use Drupal\Core\Hook\Attribute\Hook; |
| 10 | use Drupal\Core\Hook\Order\OrderAfter; |
| 11 | use Drupal\Core\StringTranslation\TranslatableMarkup; |
| 12 | use Drupal\display_builder\InstanceInterface; |
| 13 | use Drupal\display_builder_entity_view\Entity\EntityViewDisplay; |
| 14 | use Drupal\display_builder_entity_view\Entity\LayoutBuilderEntityViewDisplay; |
| 15 | use Drupal\display_builder_entity_view\Field\DisplayBuilderItemList; |
| 16 | use Drupal\display_builder_entity_view\Form\EntityViewDisplayForm; |
| 17 | use Drupal\display_builder_entity_view\Form\LayoutBuilderEntityViewDisplayForm; |
| 18 | |
| 19 | /** |
| 20 | * Hook implementations for display_builder_entity_view. |
| 21 | */ |
| 22 | class DisplayBuilderEntityViewHook { |
| 23 | |
| 24 | public function __construct( |
| 25 | protected ModuleHandlerInterface $moduleHandler, |
| 26 | ) {} |
| 27 | |
| 28 | /** |
| 29 | * Implements hook_entity_field_type_alter(). |
| 30 | * |
| 31 | * @param array $info |
| 32 | * The field types to alter. |
| 33 | */ |
| 34 | #[Hook('field_info_alter')] |
| 35 | public function fieldInfoAlter(array &$info): void { |
| 36 | $info['ui_patterns_source']['list_class'] = DisplayBuilderItemList::class; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Implements hook_entity_type_alter(). |
| 41 | * |
| 42 | * @param array $entity_types |
| 43 | * An associative array of entity type definitions. |
| 44 | */ |
| 45 | #[Hook('entity_type_alter', order: new OrderAfter(['layout_builder']))] |
| 46 | public function entityTypeAlter(array &$entity_types): void { |
| 47 | /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */ |
| 48 | if ($this->moduleHandler->moduleExists('layout_builder')) { |
| 49 | $entity_types['entity_view_display'] |
| 50 | ->setClass(LayoutBuilderEntityViewDisplay::class) |
| 51 | ->setFormClass('edit', LayoutBuilderEntityViewDisplayForm::class); |
| 52 | } |
| 53 | else { |
| 54 | $entity_types['entity_view_display'] |
| 55 | ->setClass(EntityViewDisplay::class) |
| 56 | ->setFormClass('edit', EntityViewDisplayForm::class); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Implements hook_entity_operation_alter(). |
| 62 | * |
| 63 | * @param array $operations |
| 64 | * An associative array of operations. |
| 65 | * @param \Drupal\Core\Entity\EntityInterface $entity |
| 66 | * The entity for which the operations are being altered. |
| 67 | */ |
| 68 | #[Hook('entity_operation_alter')] |
| 69 | public function entityOperationAlter(array &$operations, EntityInterface $entity): void { |
| 70 | if (!$entity instanceof InstanceInterface) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $id = (string) $entity->id(); |
| 75 | |
| 76 | if (\str_starts_with($id, EntityViewDisplay::getPrefix())) { |
| 77 | $operations['build'] = [ |
| 78 | 'title' => new TranslatableMarkup('Build display'), |
| 79 | 'url' => EntityViewDisplay::getUrlFromInstanceId($id), |
| 80 | 'weight' => -1, |
| 81 | ]; |
| 82 | $operations['edit'] = [ |
| 83 | 'title' => new TranslatableMarkup('Edit display'), |
| 84 | 'url' => EntityViewDisplay::getDisplayUrlFromInstanceId($id), |
| 85 | 'weight' => 10, |
| 86 | ]; |
| 87 | } |
| 88 | elseif (\str_starts_with($id, DisplayBuilderItemList::getPrefix())) { |
| 89 | $operations['build'] = [ |
| 90 | 'title' => new TranslatableMarkup('Build display'), |
| 91 | 'url' => DisplayBuilderItemList::getUrlFromInstanceId($id), |
| 92 | 'weight' => -1, |
| 93 | ]; |
| 94 | $operations['edit'] = [ |
| 95 | 'title' => new TranslatableMarkup('Edit display'), |
| 96 | 'url' => DisplayBuilderItemList::getUrlFromInstanceId($id), |
| 97 | 'weight' => 10, |
| 98 | ]; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Implements hook_display_builder_provider_info(). |
| 104 | * |
| 105 | * @return array |
| 106 | * An associative array of display builder providers. |
| 107 | */ |
| 108 | #[Hook('display_builder_provider_info')] |
| 109 | public function displayBuilderProviderInfo(): array { |
| 110 | return [ |
| 111 | 'entity_view' => [ |
| 112 | 'label' => new TranslatableMarkup('Entity view'), |
| 113 | 'class' => EntityViewDisplay::class, |
| 114 | 'prefix' => EntityViewDisplay::getPrefix(), |
| 115 | ], |
| 116 | 'entity_view_override' => [ |
| 117 | 'label' => new TranslatableMarkup('Entity view override'), |
| 118 | 'class' => DisplayBuilderItemList::class, |
| 119 | 'prefix' => DisplayBuilderItemList::getPrefix(), |
| 120 | ], |
| 121 | ]; |
| 122 | } |
| 123 | |
| 124 | } |