Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| DisplayBuilderSubscriber | |
0.00% |
0 / 27 |
|
0.00% |
0 / 12 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSubscribedEvents | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onSave | |
0.00% |
0 / 15 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
42 | |||
| getEntityViewDisplayEntity | |
0.00% |
0 / 8 |
|
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\EventSubscriber; |
| 6 | |
| 7 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | use Drupal\Core\Plugin\Context\ContextInterface; |
| 9 | use Drupal\display_builder\DisplayBuildableInterface; |
| 10 | use Drupal\display_builder\Event\DisplayBuilderEvent; |
| 11 | use Drupal\display_builder\Event\DisplayBuilderEvents; |
| 12 | use Drupal\display_builder_entity_view\Entity\EntityViewDisplay; |
| 13 | use Drupal\display_builder_entity_view\Field\DisplayBuilderItemList; |
| 14 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 15 | |
| 16 | /** |
| 17 | * The event subscriber for display builder islands. |
| 18 | */ |
| 19 | class DisplayBuilderSubscriber implements EventSubscriberInterface { |
| 20 | |
| 21 | public function __construct( |
| 22 | protected EntityTypeManagerInterface $entityTypeManager, |
| 23 | ) {} |
| 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public static function getSubscribedEvents(): array { |
| 29 | return [ |
| 30 | DisplayBuilderEvents::ON_SAVE => 'onSave', |
| 31 | ]; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Event handler for when a display builder is saved. |
| 36 | * |
| 37 | * @param \Drupal\display_builder\Event\DisplayBuilderEvent $event |
| 38 | * The event object. |
| 39 | */ |
| 40 | public function onSave(DisplayBuilderEvent $event): void { |
| 41 | $builder_id = $event->getBuilderId(); |
| 42 | /** @var \Drupal\display_builder\InstanceInterface $instance */ |
| 43 | $instance = $this->entityTypeManager->getStorage('display_builder_instance')->load($builder_id); |
| 44 | $contexts = $instance->getContexts(); |
| 45 | |
| 46 | // Entity view display overrides. |
| 47 | if ($params = DisplayBuilderItemList::checkInstanceId($builder_id)) { |
| 48 | /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ |
| 49 | $entity = $this->entityTypeManager->getStorage($params['entity_type_id']) |
| 50 | ->load($params['entity_id']); |
| 51 | /** @var \Drupal\display_builder\DisplayBuildableInterface $override */ |
| 52 | $override = $entity->get($params['field_name']); |
| 53 | |
| 54 | if ($override) { |
| 55 | $override->saveSources(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Entity view displays. |
| 60 | elseif (EntityViewDisplay::checkInstanceId($builder_id)) { |
| 61 | if (!$instance->hasSaveContextsRequirement(EntityViewDisplay::getContextRequirement(), $contexts)) { |
| 62 | return; |
| 63 | } |
| 64 | // Entity view display parameters are also in route match. |
| 65 | /** @var \Drupal\display_builder\DisplayBuildableInterface|null $display */ |
| 66 | $display = $this->getEntityViewDisplayEntity($contexts['entity'], $contexts['view_mode']); |
| 67 | |
| 68 | if ($display) { |
| 69 | $display->saveSources(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get entity view display entity. |
| 76 | * |
| 77 | * @param \Drupal\Core\Plugin\Context\ContextInterface $entity_context |
| 78 | * The entity context. |
| 79 | * @param \Drupal\Core\Plugin\Context\ContextInterface $view_mode_context |
| 80 | * The view mode context. |
| 81 | * |
| 82 | * @return \Drupal\display_builder\DisplayBuildableInterface|null |
| 83 | * The entity view display entity or NULL if not found. |
| 84 | */ |
| 85 | protected function getEntityViewDisplayEntity(ContextInterface $entity_context, ContextInterface $view_mode_context): ?DisplayBuildableInterface { |
| 86 | /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ |
| 87 | $entity = $entity_context->getContextValue(); |
| 88 | $entity_type_id = $entity->getEntityTypeId(); |
| 89 | $bundle = $entity->bundle(); |
| 90 | $view_mode = $view_mode_context->getContextValue(); |
| 91 | $display_id = "{$entity_type_id}.{$bundle}.{$view_mode}"; |
| 92 | |
| 93 | /** @var \Drupal\display_builder\DisplayBuildableInterface|null $display */ |
| 94 | $display = $this->entityTypeManager->getStorage('entity_view_display') |
| 95 | ->load($display_id); |
| 96 | |
| 97 | return $display; |
| 98 | } |
| 99 | |
| 100 | } |