Skip to content

Commit 9db0626

Browse files
authored
IBX-9917: Added event for disabling autosave in content edit form (#103)
* Fixed CS * added false positive to baseline * CR remarks
1 parent 7ded185 commit 9db0626

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,12 @@ parameters:
14941494
count: 1
14951495
path: src/lib/FieldType/Mapper/AuthorFormMapper.php
14961496

1497+
-
1498+
message: '#^Property Ibexa\\ContentForms\\Data\\Content\\ContentUpdateData\:\:\$contentDraft \(Ibexa\\Contracts\\Core\\Repository\\Values\\Content\\Content\) in isset\(\) is not nullable\.$#'
1499+
identifier: isset.property
1500+
count: 1
1501+
path: src/lib/FieldType/Mapper/AuthorFormMapper.php
1502+
14971503
-
14981504
message: '#^Method Ibexa\\ContentForms\\FieldType\\Mapper\\BinaryFileFormMapper\:\:mapFieldValueForm\(\) has no return type specified\.$#'
14991505
identifier: missingType.return
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Contracts\ContentForms\Event;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
12+
use Symfony\Contracts\EventDispatcher\Event;
13+
14+
final class AutosaveEnabled extends Event
15+
{
16+
private VersionInfo $versionInfo;
17+
18+
private bool $autosaveEnabled = true;
19+
20+
public function __construct(VersionInfo $versionInfo)
21+
{
22+
$this->versionInfo = $versionInfo;
23+
}
24+
25+
public function getVersionInfo(): VersionInfo
26+
{
27+
return $this->versionInfo;
28+
}
29+
30+
public function isAutosaveEnabled(): bool
31+
{
32+
return $this->autosaveEnabled;
33+
}
34+
35+
public function enableAutosave(): void
36+
{
37+
$this->autosaveEnabled = true;
38+
}
39+
40+
public function disableAutosave(): void
41+
{
42+
$this->autosaveEnabled = false;
43+
}
44+
}

src/lib/Content/View/Filter/ContentEditViewFilter.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Ibexa\ContentForms\Data\Content\ContentUpdateData;
1212
use Ibexa\ContentForms\Data\Mapper\ContentUpdateMapper;
1313
use Ibexa\ContentForms\Form\Type\Content\ContentEditType;
14+
use Ibexa\Contracts\ContentForms\Event\AutosaveEnabled;
1415
use Ibexa\Contracts\Core\Repository\ContentService;
1516
use Ibexa\Contracts\Core\Repository\ContentTypeService;
1617
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException;
@@ -24,41 +25,36 @@
2425
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2526
use Symfony\Component\Form\FormFactoryInterface;
2627
use Symfony\Component\Form\FormInterface;
28+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2729

2830
class ContentEditViewFilter implements EventSubscriberInterface
2931
{
30-
/** @var \Ibexa\Contracts\Core\Repository\ContentService */
31-
private $contentService;
32+
private ContentService $contentService;
3233

33-
/** @var \Ibexa\Contracts\Core\Repository\ContentTypeService */
34-
private $contentTypeService;
34+
private ContentTypeService $contentTypeService;
3535

36-
/** @var \Symfony\Component\Form\FormFactoryInterface */
37-
private $formFactory;
36+
private FormFactoryInterface $formFactory;
3837

39-
/** @var \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
40-
private $languagePreferenceProvider;
38+
private UserLanguagePreferenceProviderInterface $languagePreferenceProvider;
4139

4240
private LocationService $locationService;
4341

44-
/**
45-
* @param \Ibexa\Contracts\Core\Repository\ContentService $contentService
46-
* @param \Ibexa\Contracts\Core\Repository\ContentTypeService $contentTypeService
47-
* @param \Symfony\Component\Form\FormFactoryInterface $formFactory
48-
* @param \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
49-
*/
42+
private EventDispatcherInterface $eventDispatcher;
43+
5044
public function __construct(
5145
ContentService $contentService,
5246
LocationService $locationService,
5347
ContentTypeService $contentTypeService,
5448
FormFactoryInterface $formFactory,
55-
UserLanguagePreferenceProviderInterface $languagePreferenceProvider
49+
UserLanguagePreferenceProviderInterface $languagePreferenceProvider,
50+
EventDispatcherInterface $eventDispatcher
5651
) {
5752
$this->contentService = $contentService;
5853
$this->contentTypeService = $contentTypeService;
5954
$this->formFactory = $formFactory;
6055
$this->languagePreferenceProvider = $languagePreferenceProvider;
6156
$this->locationService = $locationService;
57+
$this->eventDispatcher = $eventDispatcher;
6258
}
6359

6460
public static function getSubscribedEvents()
@@ -111,11 +107,14 @@ public function handleContentEditForm(FilterViewBuilderParametersEvent $event)
111107
$contentType,
112108
$currentFields
113109
);
110+
111+
$autosaveEnabled = $this->eventDispatcher->dispatch(new AutosaveEnabled($contentDraft->getVersionInfo()))->isAutosaveEnabled();
114112
$form = $this->resolveContentEditForm(
115113
$contentUpdate,
116114
$languageCode,
117115
$contentDraft,
118-
$location ?? null
116+
$location ?? null,
117+
$autosaveEnabled
119118
);
120119

121120
$event->getParameters()->add([
@@ -153,7 +152,8 @@ private function resolveContentEditForm(
153152
ContentUpdateData $contentUpdate,
154153
string $languageCode,
155154
Content $content,
156-
?Location $location = null
155+
?Location $location = null,
156+
bool $autosaveEnabled = true
157157
): FormInterface {
158158
return $this->formFactory->create(
159159
ContentEditType::class,
@@ -165,6 +165,7 @@ private function resolveContentEditForm(
165165
'content' => $content,
166166
'contentUpdateStruct' => $contentUpdate,
167167
'drafts_enabled' => true,
168+
'autosave_enabled' => $autosaveEnabled,
168169
]
169170
);
170171
}

0 commit comments

Comments
 (0)