forked from contao/contao
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate page type options from providers
- Loading branch information
Showing
5 changed files
with
197 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Event; | ||
|
||
use Contao\DataContainer; | ||
|
||
class FilterPageTypeEvent | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $options; | ||
/** | ||
* @var DataContainer | ||
*/ | ||
private $dataContainer; | ||
|
||
public function __construct(array $options, DataContainer $dataContainer) | ||
{ | ||
$this->options = $options; | ||
$this->dataContainer = $dataContainer; | ||
} | ||
|
||
public function getDataContainer(): DataContainer | ||
{ | ||
return $this->dataContainer; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
public function setOptions(array $options): self | ||
{ | ||
$this->options = $options; | ||
|
||
return $this; | ||
} | ||
|
||
public function addOption(string $option): self | ||
{ | ||
if (!\in_array($option, $this->options, true)) { | ||
$this->options[] = $option; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function removeOption(string $option): self | ||
{ | ||
$key = array_search($option, $this->options, true); | ||
|
||
if (false !== $key) { | ||
unset($this->options[$key]); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
core-bundle/src/EventListener/DataContainer/PageTypeOptionsListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\EventListener\DataContainer; | ||
|
||
use Contao\CoreBundle\Event\FilterPageTypeEvent; | ||
use Contao\CoreBundle\ServiceAnnotation\Callback; | ||
use Contao\DataContainer; | ||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
use Terminal42\ServiceAnnotationBundle\ServiceAnnotationInterface; | ||
|
||
/** | ||
* @Callback(table="tl_page", target="fields.type.options") | ||
*/ | ||
class PageTypeOptionsListener implements ServiceAnnotationInterface | ||
{ | ||
/** | ||
* @var ServiceLocator | ||
*/ | ||
private $contentTypes; | ||
|
||
/** | ||
* @var Security | ||
*/ | ||
private $security; | ||
|
||
/** | ||
* @var EventDispatcherInterface|null | ||
*/ | ||
private $eventDispatcher; | ||
|
||
public function __construct(ServiceLocator $contentTypes, Security $security, EventDispatcherInterface $eventDispatcher = null) | ||
{ | ||
$this->contentTypes = $contentTypes; | ||
$this->security = $security; | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
public function __invoke(DataContainer $dc) | ||
{ | ||
$pageTypes = array_keys($GLOBALS['TL_PTY']); | ||
$contentTypes = array_keys($this->contentTypes->getProvidedServices()); | ||
|
||
$options = array_unique(array_merge($pageTypes, $contentTypes)); | ||
|
||
if (null !== $this->eventDispatcher) { | ||
$options = $this->eventDispatcher | ||
->dispatch(new FilterPageTypeEvent($options, $dc)) | ||
->getOptions() | ||
; | ||
} | ||
|
||
// Allow the currently selected option and anything the user has access to | ||
foreach ($options as $k => $pageType) { | ||
if ($pageType !== $dc->value && !$this->security->isGranted('contao_user.alpty', $pageType)) { | ||
unset($options[$k]); | ||
} | ||
} | ||
|
||
return array_values($options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\EventListener; | ||
|
||
use Contao\CoreBundle\Event\FilterPageTypeEvent; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class FilterPageTypeListener | ||
{ | ||
public function __invoke(FilterPageTypeEvent $event): void | ||
{ | ||
$dc = $event->getDataContainer(); | ||
|
||
if (!$dc->activeRecord) { | ||
return; | ||
} | ||
|
||
// Root pages are allowed on the first level only (see #6360) | ||
if ($dc->activeRecord->pid > 0) { | ||
$event->removeOption('root'); | ||
} else { | ||
$event->setOptions(['root']); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters