Skip to content

Commit

Permalink
IBX-6279: Simplify actions in create and edit modes (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
tischsoic authored Sep 26, 2023
1 parent d136cd6 commit 267c890
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<target state="new">Publish</target>
<note>key: Publish</note>
</trans-unit>
<trans-unit id="e4edccf256c4bf913a5929b429824155a7613ca3" resname="Publish and edit">
<source>Publish and edit</source>
<target state="new">Publish and edit</target>
<note>key: Publish and edit</note>
</trans-unit>
<trans-unit id="fb063a81c26c9246dd089025bf8e6ff896058ffc" resname="To language">
<source>To language</source>
<target state="new">To language</target>
Expand All @@ -51,6 +56,11 @@
<target state="new">Save draft</target>
<note>key: save_draft</note>
</trans-unit>
<trans-unit id="d57dc5d10311fb7d6d4730bfdee8a5b6e7c2d849" resname="save_draft_and_close">
<source>Save draft and close</source>
<target state="new">Save draft and close</target>
<note>key: save_draft_and_close</note>
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions src/lib/Event/ContentFormEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down
95 changes: 95 additions & 0 deletions src/lib/Form/Processor/ContentFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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],
];
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
7 changes: 6 additions & 1 deletion src/lib/Form/Type/Content/ContentEditType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'],
Expand Down

0 comments on commit 267c890

Please sign in to comment.