vendor/ibexa/taxonomy/src/lib/UniversalDiscovery/Event/Subscriber/ContentCreateSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. declare(strict_types=1);
  7. namespace Ibexa\Taxonomy\UniversalDiscovery\Event\Subscriber;
  8. use Ibexa\AdminUi\UniversalDiscovery\Event\ConfigResolveEvent;
  9. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  10. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  11. use Ibexa\Taxonomy\Service\TaxonomyConfiguration;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. final class ContentCreateSubscriber implements EventSubscriberInterface
  14. {
  15. private ContentTypeService $contentTypeService;
  16. private TaxonomyConfiguration $taxonomyConfiguration;
  17. public function __construct(
  18. ContentTypeService $contentTypeService,
  19. TaxonomyConfiguration $taxonomyConfiguration
  20. ) {
  21. $this->contentTypeService = $contentTypeService;
  22. $this->taxonomyConfiguration = $taxonomyConfiguration;
  23. }
  24. public static function getSubscribedEvents(): array
  25. {
  26. return [
  27. ConfigResolveEvent::NAME => ['onUdwConfigResolve', -10],
  28. ];
  29. }
  30. public function onUdwConfigResolve(ConfigResolveEvent $event): void
  31. {
  32. if ($event->getConfigName() !== 'create') {
  33. return;
  34. }
  35. $config = $event->getConfig();
  36. $allowedContentTypes = $config['allowed_content_types'];
  37. $eligibleContentTypes = [];
  38. $contentTypeGroups = $this->contentTypeService->loadContentTypeGroups();
  39. foreach ($contentTypeGroups as $contentTypeGroup) {
  40. $contentTypes = $this->contentTypeService->loadContentTypes($contentTypeGroup);
  41. if ($contentTypes instanceof \Traversable) {
  42. $contentTypes = iterator_to_array($contentTypes);
  43. }
  44. $contentTypes = array_filter(
  45. $contentTypes,
  46. fn (ContentType $contentType): bool => !$this
  47. ->taxonomyConfiguration
  48. ->isContentTypeAssociatedWithTaxonomy($contentType)
  49. );
  50. $eligibleContentTypes = array_merge(
  51. $eligibleContentTypes,
  52. array_column($contentTypes, 'identifier')
  53. );
  54. }
  55. $config['allowed_content_types'] = $allowedContentTypes !== null
  56. ? array_intersect($allowedContentTypes, $eligibleContentTypes)
  57. : $eligibleContentTypes;
  58. $event->setConfig($config);
  59. }
  60. }