Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
DisplayBuilderSubscriber | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSubscribedEvents | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
onSave | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\display_builder_page_layout\EventSubscriber; |
6 | |
7 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
8 | use Drupal\Core\Plugin\CachedDiscoveryClearerInterface; |
9 | use Drupal\display_builder\Event\DisplayBuilderEvent; |
10 | use Drupal\display_builder\Event\DisplayBuilderEvents; |
11 | use Drupal\display_builder_page_layout\Entity\PageLayout; |
12 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
13 | |
14 | /** |
15 | * The event subscriber for display builder islands. |
16 | */ |
17 | class DisplayBuilderSubscriber implements EventSubscriberInterface { |
18 | |
19 | public function __construct( |
20 | private CachedDiscoveryClearerInterface $pluginCacheClearer, |
21 | private EntityTypeManagerInterface $entityTypeManager, |
22 | ) {} |
23 | |
24 | /** |
25 | * {@inheritdoc} |
26 | */ |
27 | public static function getSubscribedEvents(): array { |
28 | return [ |
29 | DisplayBuilderEvents::ON_SAVE => 'onSave', |
30 | ]; |
31 | } |
32 | |
33 | /** |
34 | * Event handler for when a display builder is saved. |
35 | * |
36 | * @param \Drupal\display_builder\Event\DisplayBuilderEvent $event |
37 | * The event object. |
38 | */ |
39 | public function onSave(DisplayBuilderEvent $event): void { |
40 | $builder_id = $event->getBuilderId(); |
41 | $contexts = $event->getData(); |
42 | $params = PageLayout::checkInstanceId($builder_id); |
43 | |
44 | if (!$params) { |
45 | return; |
46 | } |
47 | |
48 | // Context requirements is set to allow SourcePlugin when editing. We need |
49 | // a precedence when saving. |
50 | // @todo perhaps we need a third context. |
51 | /** @var \Drupal\display_builder\InstanceInterface $instance */ |
52 | $instance = $this->entityTypeManager->getStorage('display_builder_instance')->load($builder_id); |
53 | |
54 | if (!$instance->hasSaveContextsRequirement(PageLayout::getContextRequirement(), $contexts)) { |
55 | return; |
56 | } |
57 | $page_layout_id = $params['page_layout'] ?? NULL; |
58 | |
59 | if (!$page_layout_id) { |
60 | // This must never happen because PageLayout::checkInstanceId() always |
61 | // return a page_layout key. |
62 | return; |
63 | } |
64 | /** @var \Drupal\display_builder_page_layout\PageLayoutInterface $page_layout */ |
65 | $page_layout = $this->entityTypeManager->getStorage('page_layout')->load($page_layout_id); |
66 | $page_layout->saveSources(); |
67 | // Clearing plugin cache seems enough to get the new layout. |
68 | // @todo It looks very costly. Check if it is still needed. |
69 | $this->pluginCacheClearer->clearCachedDefinitions(); |
70 | } |
71 | |
72 | } |