Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 9 |
|
0.00% |
0 / 6 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PageVariantSubscriber | |
0.00% |
0 / 20 |
|
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 / 5 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| onSelectPageDisplayVariant | |
0.00% |
0 / 14 |
|
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\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 | // In DisplayBuilderPageVariant we add PageLayout::getCacheTags() to the |
| 74 | // page renderable but it works only for the pages already managed by |
| 75 | // Display Builder. |
| 76 | // So let's add a custom tag for the others. |
| 77 | /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */ |
| 78 | $entity_type = $this->entityTypeManager->getDefinition('page_layout'); |
| 79 | $event->addCacheTags([$entity_type->getConfigPrefix()]); |
| 80 | |
| 81 | if (!$page_layout) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // Every other pages must be managed by Display Builder. |
| 86 | $event->setPluginId('display_builder'); |
| 87 | } |
| 88 | |
| 89 | } |