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