diff --git a/bundle/Controller/ContentEditController.php b/bundle/Controller/ContentEditController.php index 05d9ee72d..bdf75b56a 100644 --- a/bundle/Controller/ContentEditController.php +++ b/bundle/Controller/ContentEditController.php @@ -11,6 +11,7 @@ use eZ\Bundle\EzPublishCoreBundle\Controller; use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\ContentTypeService; +use eZ\Publish\API\Repository\LanguageService; use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\Values\Content\VersionInfo; use eZ\Publish\Core\Base\Exceptions\BadStateException; @@ -27,24 +28,19 @@ class ContentEditController extends Controller { - /** - * @var ContentTypeService - */ + /** @var ContentTypeService */ private $contentTypeService; - /** - * @var ContentService - */ + /** @var ContentService */ private $contentService; - /** - * @var LocationService - */ + /** @var LocationService */ private $locationService; - /** - * @var ActionDispatcherInterface - */ + /** @var LanguageService */ + private $languageService; + + /** @var ActionDispatcherInterface */ private $contentActionDispatcher; /** @@ -58,10 +54,12 @@ public function __construct( ContentTypeService $contentTypeService, ContentService $contentService, LocationService $locationService, + LanguageService $languageService, ActionDispatcherInterface $contentActionDispatcher ) { $this->contentTypeService = $contentTypeService; $this->locationService = $locationService; + $this->languageService = $languageService; $this->contentActionDispatcher = $contentActionDispatcher; $this->contentService = $contentService; } @@ -82,14 +80,16 @@ public function __construct( */ public function createWithoutDraftAction($contentTypeIdentifier, $language, $parentLocationId, Request $request) { + $language = $this->languageService->loadLanguage($language); $parentLocation = $this->locationService->loadLocation($parentLocationId); $contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier); $data = (new ContentCreateMapper())->mapToFormData($contentType, [ - 'mainLanguageCode' => $language, + 'mainLanguageCode' => $language->languageCode, 'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocationId), ]); $form = $this->createForm(ContentEditType::class, $data, [ - 'languageCode' => $language, + 'languageCode' => $language->languageCode, + 'mainLanguageCode' => $language->languageCode, 'drafts_enabled' => true, ]); $form->handleRequest($request); @@ -103,7 +103,7 @@ public function createWithoutDraftAction($contentTypeIdentifier, $language, $par return new ContentCreateView(null, [ 'form' => $form->createView(), - 'languageCode' => $language, + 'language' => $language, 'contentType' => $contentType, 'parentLocation' => $parentLocation, ]); @@ -190,12 +190,14 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r throw new BadStateException('Version status', 'status is not draft'); } + $language = $language ?: $draft->getVersionInfo()->getContentInfo()->mainLanguageCode; + $language = $this->languageService->loadLanguage($language); $contentType = $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId); $contentUpdate = (new ContentUpdateMapper())->mapToFormData( $draft, [ - 'languageCode' => $language, + 'languageCode' => $language->languageCode, 'contentType' => $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId), ] ); @@ -203,7 +205,8 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r ContentEditType::class, $contentUpdate, [ - 'languageCode' => $language, + 'languageCode' => $language->languageCode, + 'mainLanguageCode' => $draft->contentInfo->mainLanguageCode, 'drafts_enabled' => true, ] ); @@ -218,7 +221,7 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r return new ContentEditView(null, [ 'form' => $form->createView(), - 'languageCode' => $language, + 'language' => $language, 'content' => $draft, 'contentType' => $contentType, ]); diff --git a/bundle/Resources/config/services.yml b/bundle/Resources/config/services.yml index e1e317249..6ec5f64a3 100644 --- a/bundle/Resources/config/services.yml +++ b/bundle/Resources/config/services.yml @@ -223,6 +223,7 @@ services: - "@ezpublish.api.service.content_type" - "@ezpublish.api.service.content" - "@ezpublish.api.service.location" + - "@ezpublish.api.service.language" - "@ezrepoforms.action_dispatcher.content" parent: ezpublish.controller.base calls: diff --git a/lib/Data/Content/ContentUpdateData.php b/lib/Data/Content/ContentUpdateData.php index d75412d48..c03cc2105 100644 --- a/lib/Data/Content/ContentUpdateData.php +++ b/lib/Data/Content/ContentUpdateData.php @@ -13,7 +13,7 @@ /** * @property-read \EzSystems\RepositoryForms\Data\Content\FieldData[] $fieldsData - * @property-read \eZ\Publish\API\Repository\Values\Content\Content[] $contentDraft + * @property-read \eZ\Publish\API\Repository\Values\Content\Content $contentDraft */ class ContentUpdateData extends ContentUpdateStruct implements NewnessCheckable { diff --git a/lib/Form/Processor/ContentFormProcessor.php b/lib/Form/Processor/ContentFormProcessor.php index 2ed1269cc..1093af31b 100644 --- a/lib/Form/Processor/ContentFormProcessor.php +++ b/lib/Form/Processor/ContentFormProcessor.php @@ -12,6 +12,9 @@ use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\Values\Content\ContentStruct; use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter; +use EzSystems\RepositoryForms\Data\Content\ContentCreateData; +use EzSystems\RepositoryForms\Data\Content\ContentUpdateData; +use EzSystems\RepositoryForms\Data\NewnessChecker; use EzSystems\RepositoryForms\Event\FormActionEvent; use EzSystems\RepositoryForms\Event\RepositoryFormEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -153,7 +156,12 @@ public function processCreateDraft(FormActionEvent $event) */ private function saveDraft(ContentStruct $data, $languageCode) { + $mainLanuageCode = $this->resolveMainLanguageCode($data); foreach ($data->fieldsData as $fieldDefIdentifier => $fieldData) { + if ($mainLanuageCode && !$fieldData->fieldDefinition->isTranslatable) { + continue; + } + $data->setField($fieldDefIdentifier, $fieldData->value, $languageCode); } @@ -165,4 +173,16 @@ private function saveDraft(ContentStruct $data, $languageCode) return $contentDraft; } + + /** + * @param ContentUpdateData|ContentCreateData|NewnessChecker $data + * + * @return string + */ + private function resolveMainLanguageCode($data): string + { + return $data->isNew() + ? $data->mainLanguageCode + : $data->contentDraft->getVersionInfo()->getContentInfo()->mainLanguageCode; + } } diff --git a/lib/Form/Type/Content/ContentEditType.php b/lib/Form/Type/Content/ContentEditType.php index 4f19f6aec..6621c12d8 100644 --- a/lib/Form/Type/Content/ContentEditType.php +++ b/lib/Form/Type/Content/ContentEditType.php @@ -14,6 +14,8 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -39,7 +41,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) ->add('fieldsData', CollectionType::class, [ 'entry_type' => ContentFieldType::class, 'label' => 'ezrepoforms.content.fields', - 'entry_options' => ['languageCode' => $options['languageCode']], + 'entry_options' => [ + 'languageCode' => $options['languageCode'], + 'mainLanguageCode' => $options['mainLanguageCode'], + ], ]) ->add('redirectUrlAfterPublish', HiddenType::class, ['required' => false, 'mapped' => false]) ->add('publish', SubmitType::class, ['label' => 'content.publish_button']); @@ -55,6 +60,12 @@ public function buildForm(FormBuilderInterface $builder, array $options) } } + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['languageCode'] = $options['languageCode']; + $view->vars['mainLanguageCode'] = $options['mainLanguageCode']; + } + public function suppressValidationOnCancel(FormEvent $event) { $form = $event->getForm(); @@ -72,6 +83,6 @@ public function configureOptions(OptionsResolver $resolver) 'data_class' => '\eZ\Publish\API\Repository\Values\Content\ContentStruct', 'translation_domain' => 'ezrepoforms_content', ]) - ->setRequired(['languageCode']); + ->setRequired(['languageCode', 'mainLanguageCode']); } } diff --git a/lib/Form/Type/Content/ContentFieldType.php b/lib/Form/Type/Content/ContentFieldType.php index 6186f21a2..bd049c769 100644 --- a/lib/Form/Type/Content/ContentFieldType.php +++ b/lib/Form/Type/Content/ContentFieldType.php @@ -12,6 +12,8 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; class ContentFieldType extends AbstractType @@ -43,7 +45,13 @@ public function configureOptions(OptionsResolver $resolver) 'data_class' => '\EzSystems\RepositoryForms\Data\Content\FieldData', 'translation_domain' => 'ezrepoforms_content', ]) - ->setRequired(['languageCode']); + ->setRequired(['languageCode', 'mainLanguageCode']); + } + + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['languageCode'] = $options['languageCode']; + $view->vars['mainLanguageCode'] = $options['mainLanguageCode']; } public function buildForm(FormBuilderInterface $builder, array $options)