From 267c890de64f7ac10de1cb77a591aaafb382ab9e Mon Sep 17 00:00:00 2001 From: Jakub Brzegowski Date: Tue, 26 Sep 2023 15:48:00 +0200 Subject: [PATCH] IBX-6279: Simplify actions in create and edit modes (#50) --- .../ibexa_content_forms_content.en.xliff | 10 ++ src/lib/Event/ContentFormEvents.php | 10 ++ .../Form/Processor/ContentFormProcessor.php | 95 +++++++++++++++++++ src/lib/Form/Type/Content/ContentEditType.php | 7 +- 4 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/bundle/Resources/translations/ibexa_content_forms_content.en.xliff b/src/bundle/Resources/translations/ibexa_content_forms_content.en.xliff index c22904ad..ac72ee80 100644 --- a/src/bundle/Resources/translations/ibexa_content_forms_content.en.xliff +++ b/src/bundle/Resources/translations/ibexa_content_forms_content.en.xliff @@ -31,6 +31,11 @@ Publish key: Publish + + Publish and edit + Publish and edit + key: Publish and edit + To language To language @@ -51,6 +56,11 @@ Save draft key: save_draft + + Save draft and close + Save draft and close + key: save_draft_and_close + diff --git a/src/lib/Event/ContentFormEvents.php b/src/lib/Event/ContentFormEvents.php index e25d4560..62226a2a 100644 --- a/src/lib/Event/ContentFormEvents.php +++ b/src/lib/Event/ContentFormEvents.php @@ -20,6 +20,11 @@ final class ContentFormEvents */ public const CONTENT_SAVE_DRAFT = 'content.edit.saveDraft'; + /** + * Triggered when saving a content draft and closing edit. + */ + public const CONTENT_SAVE_DRAFT_AND_CLOSE = 'content.edit.saveDraftAndClose'; + /** * Triggered when creating a content draft. */ @@ -30,6 +35,11 @@ final class ContentFormEvents */ public const CONTENT_PUBLISH = 'content.edit.publish'; + /** + * Triggered when publishing a content and opening new edit. + */ + public const CONTENT_PUBLISH_AND_EDIT = 'content.edit.publishAndEdit'; + /** * Triggered when canceling a content edition. */ diff --git a/src/lib/Form/Processor/ContentFormProcessor.php b/src/lib/Form/Processor/ContentFormProcessor.php index 306a7f4d..8e6bd5d2 100644 --- a/src/lib/Form/Processor/ContentFormProcessor.php +++ b/src/lib/Form/Processor/ContentFormProcessor.php @@ -18,6 +18,7 @@ use Ibexa\Contracts\Core\Repository\Values\Content\Content; use Ibexa\Contracts\Core\Repository\Values\Content\ContentStruct; use Ibexa\Contracts\Core\Repository\Values\Content\Location; +use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; use Ibexa\Core\Base\Exceptions\InvalidArgumentException; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -61,8 +62,10 @@ public static function getSubscribedEvents(): array { return [ ContentFormEvents::CONTENT_PUBLISH => ['processPublish', 10], + ContentFormEvents::CONTENT_PUBLISH_AND_EDIT => ['processPublishAndEdit', 10], ContentFormEvents::CONTENT_CANCEL => ['processCancel', 10], ContentFormEvents::CONTENT_SAVE_DRAFT => ['processSaveDraft', 10], + ContentFormEvents::CONTENT_SAVE_DRAFT_AND_CLOSE => ['processSaveDraftAndClose', 10], ContentFormEvents::CONTENT_CREATE_DRAFT => ['processCreateDraft', 10], ]; } @@ -102,6 +105,60 @@ public function processSaveDraft(FormActionEvent $event) $event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl)); } + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException + * @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException + */ + public function processSaveDraftAndClose(FormActionEvent $event): void + { + /** @var \Ibexa\ContentForms\Data\Content\ContentCreateData|\Ibexa\ContentForms\Data\Content\ContentUpdateData $data */ + $data = $event->getData(); + $form = $event->getForm(); + + $formConfig = $form->getConfig(); + $languageCode = $formConfig->getOption('languageCode'); + $draft = $this->saveDraft($data, $languageCode, []); + $referrerLocation = $event->getOption('referrerLocation'); + + if ($referrerLocation === null) { + $versionInfo = $data->contentDraft->getVersionInfo(); + $contentInfo = $versionInfo->getContentInfo(); + + $currentVersion = $this->contentService->loadContentByContentInfo($contentInfo); + + if ($currentVersion->getVersionInfo()->status === VersionInfo::STATUS_PUBLISHED) { + $publishedContentInfo = $currentVersion->getVersionInfo()->getContentInfo(); + $redirectionLocationId = $publishedContentInfo->mainLocationId; + $redirectionContentId = $publishedContentInfo->getId(); + } else { + $parentLocation = $this->locationService->loadParentLocationsForDraftContent($versionInfo)[0]; + $redirectionLocationId = $parentLocation->id; + $redirectionContentId = $parentLocation->contentId; + } + } else { + $redirectionLocationId = $referrerLocation->contentId; + $redirectionContentId = $referrerLocation->id; + } + + $event->setPayload('content', $draft); + $event->setPayload('is_new', $draft->contentInfo->isDraft()); + + $defaultUrl = $this->router->generate( + 'ibexa.content.view', + [ + 'contentId' => $redirectionContentId, + 'locationId' => $redirectionLocationId, + ] + ); + + $event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl)); + } + /** * @param \Ibexa\ContentForms\Event\FormActionEvent $event * @@ -141,6 +198,44 @@ public function processPublish(FormActionEvent $event) $event->setResponse(new RedirectResponse($redirectUrl)); } + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException + * @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentException + */ + public function processPublishAndEdit(FormActionEvent $event) + { + /** @var \Ibexa\ContentForms\Data\Content\ContentCreateData|\Ibexa\ContentForms\Data\Content\ContentUpdateData $data */ + $data = $event->getData(); + $form = $event->getForm(); + $referrerLocation = $event->getOption('referrerLocation'); + + $formConfig = $form->getConfig(); + $languageCode = $formConfig->getOption('languageCode'); + $draft = $this->saveDraft($data, $languageCode); + $versionInfo = $draft->versionInfo; + $content = $this->contentService->publishVersion($versionInfo, [$versionInfo->initialLanguageCode]); + + $contentInfo = $content->contentInfo; + $contentVersionInfo = $content->getVersionInfo(); + $newDraft = $this->contentService->createContentDraft($contentInfo, $contentVersionInfo); + + $event->setPayload('content', $newDraft); + $event->setPayload('is_new', $newDraft->contentInfo->isDraft()); + + $redirectUrl = $this->router->generate('ibexa.content.draft.edit', [ + 'contentId' => $newDraft->id, + 'versionNo' => $newDraft->getVersionInfo()->versionNo, + 'language' => $newDraft->contentInfo->mainLanguageCode, + 'locationId' => null !== $referrerLocation ? $referrerLocation->id : null, + ]); + + $event->setResponse(new RedirectResponse($redirectUrl)); + } + /** * @param \Ibexa\ContentForms\Event\FormActionEvent $event * diff --git a/src/lib/Form/Type/Content/ContentEditType.php b/src/lib/Form/Type/Content/ContentEditType.php index 01951769..f0ed24d3 100644 --- a/src/lib/Form/Type/Content/ContentEditType.php +++ b/src/lib/Form/Type/Content/ContentEditType.php @@ -41,7 +41,8 @@ public function getParent() public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('publish', SubmitType::class, ['label' => 'Publish']); + ->add('publish', SubmitType::class, ['label' => 'Publish']) + ->add('publishAndEdit', SubmitType::class, ['label' => 'Publish and edit']); if (!$options['drafts_enabled']) { return; @@ -52,6 +53,10 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'label' => /** @Desc("Save draft") */ 'save_draft', 'attr' => ['formnovalidate' => 'formnovalidate'], ]) + ->add('saveDraftAndClose', SubmitType::class, [ + 'label' => /** @Desc("Save draft and close") */ 'save_draft_and_close', + 'attr' => ['formnovalidate' => 'formnovalidate'], + ]) ->add('cancel', SubmitType::class, [ 'label' => /** @Desc("Cancel") */ 'cancel', 'attr' => ['formnovalidate' => 'formnovalidate'],