vendor/ibexa/content-forms/src/lib/Content/View/Filter/ContentCreateViewFilter.php line 69

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\ContentForms\Content\View\Filter;
  8. use Ibexa\ContentForms\Data\Content\ContentCreateData;
  9. use Ibexa\ContentForms\Data\Mapper\ContentCreateMapper;
  10. use Ibexa\ContentForms\Form\Type\Content\ContentEditType;
  11. use Ibexa\Contracts\Core\Repository\ContentTypeService;
  12. use Ibexa\Contracts\Core\Repository\LocationService;
  13. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  14. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  15. use Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
  16. use Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
  17. use Ibexa\Core\MVC\Symfony\View\ViewEvents;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\Form\FormInterface;
  21. class ContentCreateViewFilter implements EventSubscriberInterface
  22. {
  23. /** @var \Ibexa\Contracts\Core\Repository\LocationService */
  24. private $locationService;
  25. /** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
  26. private $contentTypeService;
  27. /** @var \Symfony\Component\Form\FormFactoryInterface */
  28. private $formFactory;
  29. /** @var \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
  30. private $languagePreferenceProvider;
  31. /**
  32. * @param \Ibexa\Contracts\Core\Repository\LocationService $locationService
  33. * @param \Ibexa\Contracts\Core\Repository\ContentTypeService $contentTypeService
  34. * @param \Symfony\Component\Form\FormFactoryInterface $formFactory
  35. * @param \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
  36. */
  37. public function __construct(
  38. LocationService $locationService,
  39. ContentTypeService $contentTypeService,
  40. FormFactoryInterface $formFactory,
  41. UserLanguagePreferenceProviderInterface $languagePreferenceProvider
  42. ) {
  43. $this->locationService = $locationService;
  44. $this->contentTypeService = $contentTypeService;
  45. $this->formFactory = $formFactory;
  46. $this->languagePreferenceProvider = $languagePreferenceProvider;
  47. }
  48. public static function getSubscribedEvents()
  49. {
  50. return [ViewEvents::FILTER_BUILDER_PARAMETERS => 'handleContentCreateForm'];
  51. }
  52. /**
  53. * @param \Ibexa\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent $event
  54. *
  55. * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
  56. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
  57. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  58. */
  59. public function handleContentCreateForm(FilterViewBuilderParametersEvent $event)
  60. {
  61. if ('ibexa_content_edit:createWithoutDraftAction' !== $event->getParameters()->get('_controller')) {
  62. return;
  63. }
  64. $request = $event->getRequest();
  65. $languageCode = $request->attributes->get('language');
  66. $contentType = $this->contentTypeService->loadContentTypeByIdentifier(
  67. $request->attributes->get('contentTypeIdentifier'),
  68. $this->languagePreferenceProvider->getPreferredLanguages()
  69. );
  70. $location = $this->locationService->loadLocation(
  71. $request->attributes->getInt('parentLocationId')
  72. );
  73. $contentCreateData = $this->resolveContentCreateData($contentType, $location, $languageCode);
  74. $form = $this->resolveContentCreateForm(
  75. $contentCreateData,
  76. $languageCode,
  77. false
  78. );
  79. $event->getParameters()->add(['form' => $form->handleRequest($request)]);
  80. }
  81. /**
  82. * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
  83. * @param \Ibexa\Contracts\Core\Repository\Values\Content\Location $location
  84. * @param string $languageCode
  85. *
  86. * @return \Ibexa\ContentForms\Data\Content\ContentCreateData
  87. */
  88. private function resolveContentCreateData(
  89. ContentType $contentType,
  90. Location $location,
  91. string $languageCode
  92. ): ContentCreateData {
  93. $contentCreateMapper = new ContentCreateMapper();
  94. return $contentCreateMapper->mapToFormData(
  95. $contentType,
  96. [
  97. 'mainLanguageCode' => $languageCode,
  98. 'parentLocation' => $this->locationService->newLocationCreateStruct($location->id, $contentType),
  99. ]
  100. );
  101. }
  102. /**
  103. * @param \Ibexa\ContentForms\Data\Content\ContentCreateData $contentCreateData
  104. * @param string $languageCode
  105. *
  106. * @return \Symfony\Component\Form\FormInterface
  107. */
  108. private function resolveContentCreateForm(
  109. ContentCreateData $contentCreateData,
  110. string $languageCode,
  111. bool $autosaveEnabled = true
  112. ): FormInterface {
  113. return $this->formFactory->create(ContentEditType::class, $contentCreateData, [
  114. 'languageCode' => $languageCode,
  115. 'mainLanguageCode' => $languageCode,
  116. 'contentCreateStruct' => $contentCreateData,
  117. 'drafts_enabled' => true,
  118. 'autosave_enabled' => $autosaveEnabled,
  119. ]);
  120. }
  121. }
  122. class_alias(ContentCreateViewFilter::class, 'EzSystems\EzPlatformContentForms\Content\View\Filter\ContentCreateViewFilter');