Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PageVariantSubscriber | |
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 / 5 |
|
0.00% |
0 / 1 |
2 | |||
onSelectPageDisplayVariant | |
0.00% |
0 / 12 |
|
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\Render\PageDisplayVariantSelectionEvent; |
9 | use Drupal\Core\Render\RenderEvents; |
10 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11 | |
12 | /** |
13 | * Drupal should return a display builder page for any non admin route. |
14 | * |
15 | * @code |
16 | * route.name: |
17 | * ... |
18 | * options: |
19 | * _admin_route: false |
20 | * _display_builder_route: true |
21 | * |
22 | * @endcode |
23 | */ |
24 | class PageVariantSubscriber implements EventSubscriberInterface { |
25 | |
26 | public function __construct( |
27 | private EntityTypeManagerInterface $entityTypeManager, |
28 | ) {} |
29 | |
30 | /** |
31 | * {@inheritdoc} |
32 | */ |
33 | public static function getSubscribedEvents(): array { |
34 | return [ |
35 | RenderEvents::SELECT_PAGE_DISPLAY_VARIANT => [ |
36 | ['onSelectPageDisplayVariant', -100], |
37 | ], |
38 | ]; |
39 | } |
40 | |
41 | /** |
42 | * Selects the page display variant. |
43 | * |
44 | * @param \Drupal\Core\Render\PageDisplayVariantSelectionEvent $event |
45 | * The event to process. |
46 | */ |
47 | public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $event): void { |
48 | $route = $event->getRouteMatch()->getRouteObject(); |
49 | $options = $route->getOptions(); |
50 | |
51 | // In admin pages, we want the page.html.twig from the admin theme. |
52 | // For now we don't build pages for Admin routes, but this will be possible |
53 | // in the future. |
54 | if ($options['_admin_route'] ?? FALSE) { |
55 | return; |
56 | } |
57 | |
58 | // When we use Display Builder to build the full page, we don't want to have |
59 | // neither the theme's page.html.twig nor the page managed by Display |
60 | // Builder. We want a simple blank page. |
61 | // Example: entity.page_layout.display_builder. |
62 | if ($options['_display_builder_route'] ?? FALSE) { |
63 | $event->setPluginId('simple_page'); |
64 | |
65 | return; |
66 | } |
67 | |
68 | // Fallback to Block Layout if there is no suitable Page Layout entities. |
69 | /** @var \Drupal\display_builder_page_layout\AccessControlHandler $access_control */ |
70 | $access_control = $this->entityTypeManager->getAccessControlHandler('page_layout'); |
71 | $page_layout = $access_control->loadCurrentPageLayout(); |
72 | |
73 | if (!$page_layout) { |
74 | return; |
75 | } |
76 | |
77 | // Every other pages must be managed by Display Builder. |
78 | $event->setPluginId('display_builder'); |
79 | } |
80 | |
81 | } |