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