|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the TYPO3 CMS project. |
| 7 | + * |
| 8 | + * It is free software; you can redistribute it and/or modify it under |
| 9 | + * the terms of the GNU General Public License, either version 2 |
| 10 | + * of the License, or any later version. |
| 11 | + * |
| 12 | + * For the full copyright and license information, please read the |
| 13 | + * LICENSE.txt file that was distributed with this source code. |
| 14 | + * |
| 15 | + * The TYPO3 project - inspiring people to share! |
| 16 | + */ |
| 17 | + |
| 18 | +namespace TYPO3\CMS\ContentBlocks\EventListener; |
| 19 | + |
| 20 | +use TYPO3\CMS\Backend\Controller\Event\ModifyNewRecordCreationLinksEvent; |
| 21 | +use TYPO3\CMS\ContentBlocks\Loader\LoadedContentBlock; |
| 22 | +use TYPO3\CMS\ContentBlocks\Registry\ContentBlockRegistry; |
| 23 | +use TYPO3\CMS\ContentBlocks\SiteSet\ContentBlockSiteRegistry; |
| 24 | +use TYPO3\CMS\Core\Attribute\AsEventListener; |
| 25 | +use TYPO3\CMS\Core\Site\Entity\Site; |
| 26 | + |
| 27 | +#[AsEventListener('RestrictContentBlockInNewRecordView')] |
| 28 | +readonly class RestrictContentBlockInNewRecordView |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + protected ContentBlockRegistry $contentBlockRegistry, |
| 32 | + protected ContentBlockSiteRegistry $contentBlockSiteRegistry, |
| 33 | + ) {} |
| 34 | + |
| 35 | + public function __invoke(ModifyNewRecordCreationLinksEvent $event): void |
| 36 | + { |
| 37 | + /** @var Site $site */ |
| 38 | + $site = $event->request->getAttribute('site'); |
| 39 | + foreach ($event->groupedCreationLinks as $groupName => $group) { |
| 40 | + if ($groupName === 'pages' || $groupName === 'content') { |
| 41 | + continue; |
| 42 | + } |
| 43 | + foreach ($group['items'] as $mainType => $item) { |
| 44 | + $contentBlocks = $this->contentBlockSiteRegistry->resolveContentBlocksRegisteredAsSiteSet($site, $mainType); |
| 45 | + if (array_key_exists('types', $item)) { |
| 46 | + // If there is no Content Block registered as Site Set, allow all. |
| 47 | + if ($contentBlocks === []) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + foreach ($item['types'] as $type => $typeItem) { |
| 51 | + $isRecordAllowed = $this->isRecordAllowed($contentBlocks, $mainType, $type); |
| 52 | + if ($isRecordAllowed === false) { |
| 53 | + unset($event->groupedCreationLinks[$groupName]['items'][$mainType]['types'][$type]); |
| 54 | + if ($event->groupedCreationLinks[$groupName]['items'][$mainType]['types'] === []) { |
| 55 | + unset($event->groupedCreationLinks[$groupName]['items'][$mainType]); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + continue; |
| 60 | + } |
| 61 | + $isRecordAllowed = $this->isRecordAllowed($contentBlocks, $mainType); |
| 62 | + if ($isRecordAllowed === false) { |
| 63 | + unset($event->groupedCreationLinks[$groupName]['items'][$mainType]); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param array<LoadedContentBlock> $registeredContentBlocks |
| 71 | + */ |
| 72 | + protected function isRecordAllowed(array $registeredContentBlocks, string $mainType, ?string $subType = null): bool |
| 73 | + { |
| 74 | + $contentBlock = $this->contentBlockRegistry->getByTypeName($mainType, $subType ?? '1'); |
| 75 | + if ($contentBlock === null) { |
| 76 | + return true; |
| 77 | + } |
| 78 | + if ($subType === null) { |
| 79 | + $contentBlockTableNames = array_map(fn(LoadedContentBlock $contentBlock) => $contentBlock->getYaml()['table'], $registeredContentBlocks); |
| 80 | + $isAllowed = in_array($mainType, $contentBlockTableNames, true); |
| 81 | + return $isAllowed; |
| 82 | + } |
| 83 | + foreach ($registeredContentBlocks as $registeredContentBlock) { |
| 84 | + if ($registeredContentBlock->getYaml()['table'] !== $mainType) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + if ($registeredContentBlock->getYaml()['typeName'] !== $subType) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + return true; |
| 91 | + } |
| 92 | + return false; |
| 93 | + } |
| 94 | +} |
0 commit comments