vendor/contao/core-bundle/src/EventListener/FilterPageTypeListener.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Contao\CoreBundle\Event\FilterPageTypeEvent;
  12. use Doctrine\DBAL\Connection;
  13. /**
  14.  * @internal
  15.  */
  16. class FilterPageTypeListener
  17. {
  18.     /**
  19.      * @var Connection
  20.      */
  21.     private $connection;
  22.     public function __construct(Connection $connection)
  23.     {
  24.         $this->connection $connection;
  25.     }
  26.     public function __invoke(FilterPageTypeEvent $event): void
  27.     {
  28.         $dc $event->getDataContainer();
  29.         if (!$dc->activeRecord) {
  30.             return;
  31.         }
  32.         // The first level can only have root pages (see #6360)
  33.         if (!$dc->activeRecord->pid) {
  34.             $event->setOptions(['root']);
  35.             return;
  36.         }
  37.         $event->removeOption('root');
  38.         $parentType $this->connection->fetchOne('SELECT type FROM tl_page WHERE id=?', [$dc->activeRecord->pid]);
  39.         // Error pages can only be placed directly inside root pages
  40.         if ('root' !== $parentType) {
  41.             $event->removeOption('error_401');
  42.             $event->removeOption('error_403');
  43.             $event->removeOption('error_404');
  44.             return;
  45.         }
  46.         $siblingTypes $this->connection->fetchFirstColumn(
  47.             'SELECT DISTINCT(type) FROM tl_page WHERE pid=? AND id!=?',
  48.             [$dc->activeRecord->pid$dc->activeRecord->id]
  49.         );
  50.         foreach (array_intersect(['error_401''error_403''error_404'], $siblingTypes) as $type) {
  51.             $event->removeOption($type);
  52.         }
  53.     }
  54. }