Skip to content

Commit

Permalink
IBX-6279: add two new events CONTENT_SAVE_DRAFT_AND_CLOSE and CONTENT…
Browse files Browse the repository at this point in the history
…_PUBLISH_AND_EDIT
  • Loading branch information
tischsoic committed Sep 9, 2023
1 parent d136cd6 commit 437b838
Show file tree
Hide file tree
Showing 4 changed files with 106 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
79 changes: 79 additions & 0 deletions src/lib/Form/Processor/ContentFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,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 +104,43 @@ public function processSaveDraft(FormActionEvent $event)
$event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl));
}

/**
* @param \Ibexa\ContentForms\Event\FormActionEvent $event
*
* @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)
{
/** @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');
// $contentLocation = $this->resolveLocation($draft, $referrerLocation, $data);

$event->setPayload('content', $draft);
$event->setPayload('is_new', $draft->contentInfo->isDraft());

$defaultUrl = $this->router->generate(
'ibexa.content.view',
[
'contentId' => $referrerLocation->contentId,
'locationId' => $referrerLocation->id,
]
);

$event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl));
}

/**
* @param \Ibexa\ContentForms\Event\FormActionEvent $event
*
Expand Down Expand Up @@ -141,6 +180,46 @@ public function processPublish(FormActionEvent $event)
$event->setResponse(new RedirectResponse($redirectUrl));
}

/**
* @param \Ibexa\ContentForms\Event\FormActionEvent $event
*
* @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
8 changes: 7 additions & 1 deletion src/lib/Form/Type/Content/ContentEditType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Ibexa\Contracts\Core\Repository\Values\Content\ContentStruct;
use JMS\TranslationBundle\Annotation\Desc;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -41,7 +42,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 +54,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 437b838

Please sign in to comment.