Skip to content

Commit 04dd868

Browse files
authored
IBX-7935: Handled User-related structs in FieldCollectionType dispatcher (#64)
* IBX-7935: Handled User-related structs in `FieldCollectionType` dispatcher * IBX-7935: Refactored code * IBX-7935: Further code refactor * IBX-7935: Applied review remarks * IBX-7935: Fixup * IBX-7935: Applied review remarks * IBX-7935: Refactored options * IBX-7935: Refactored options * IBX-7935: Allowed BC * IBX-7935: Refactored default value for the `struct` option * IBX-7935: Refactored `FieldCollectionType` * IBX-7935: Fixed translation domain * IBX-7935: Applied review remark * IBX-7935: Removed unnecessary annotations * IBX-7935: Removed unnecessary annotations * IBX-7935: Fixed `data` option not being passed to `struct` * IBX-7935: Allow `struct` to be `null` option
1 parent 2caf048 commit 04dd868

13 files changed

+417
-141
lines changed

src/bundle/Controller/UserController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Ibexa\ContentForms\User\View\UserCreateView;
1818
use Ibexa\ContentForms\User\View\UserUpdateView;
1919
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
20+
use Ibexa\Contracts\Core\Repository\ContentService;
2021
use Ibexa\Contracts\Core\Repository\ContentTypeService;
2122
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
2223
use Ibexa\Contracts\Core\Repository\LanguageService;
@@ -53,6 +54,8 @@ class UserController extends Controller
5354
/** @var \Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface */
5455
private $groupedContentFormFieldsProvider;
5556

57+
private ContentService $contentService;
58+
5659
public function __construct(
5760
ContentTypeService $contentTypeService,
5861
UserService $userService,
@@ -61,7 +64,8 @@ public function __construct(
6164
ActionDispatcherInterface $userActionDispatcher,
6265
PermissionResolver $permissionResolver,
6366
UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
64-
GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider
67+
GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider,
68+
ContentService $contentService
6569
) {
6670
$this->contentTypeService = $contentTypeService;
6771
$this->userService = $userService;
@@ -71,6 +75,7 @@ public function __construct(
7175
$this->permissionResolver = $permissionResolver;
7276
$this->userLanguagePreferenceProvider = $userLanguagePreferenceProvider;
7377
$this->groupedContentFormFieldsProvider = $groupedContentFormFieldsProvider;
78+
$this->contentService = $contentService;
7479
}
7580

7681
/**
@@ -114,6 +119,7 @@ public function createAction(
114119
$form = $this->createForm(UserCreateType::class, $data, [
115120
'languageCode' => $language->languageCode,
116121
'mainLanguageCode' => $language->languageCode,
122+
'struct' => $data,
117123
]);
118124
$form->handleRequest($request);
119125

@@ -152,6 +158,7 @@ public function createAction(
152158
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
153159
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
154160
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
161+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
155162
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
156163
* @throws \Ibexa\Core\Base\Exceptions\UnauthorizedException
157164
*/
@@ -192,9 +199,11 @@ public function editAction(
192199
$userUpdate,
193200
[
194201
'location' => $location,
202+
'content' => $this->contentService->loadContent($contentId),
195203
'languageCode' => $language,
196204
'mainLanguageCode' => $user->contentInfo->mainLanguageCode,
197-
]
205+
'struct' => $userUpdate,
206+
],
198207
);
199208
$form->handleRequest($request);
200209

src/bundle/Resources/config/services.yaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ services:
9999

100100
Ibexa\Bundle\ContentForms\Controller\UserController:
101101
arguments:
102-
- '@ibexa.api.service.content_type'
103-
- '@ibexa.api.service.user'
104-
- '@ibexa.api.service.location'
105-
- '@ibexa.api.service.language'
106-
- '@Ibexa\ContentForms\Form\ActionDispatcher\UserDispatcher'
107-
- '@Ibexa\Contracts\Core\Repository\PermissionResolver'
108-
- '@Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProvider'
109-
- '@Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider'
102+
$contentTypeService: '@ibexa.api.service.content_type'
103+
$userService: '@ibexa.api.service.user'
104+
$locationService: '@ibexa.api.service.location'
105+
$languageService: '@ibexa.api.service.language'
106+
$userActionDispatcher: '@Ibexa\ContentForms\Form\ActionDispatcher\UserDispatcher'
107+
$permissionResolver: '@Ibexa\Contracts\Core\Repository\PermissionResolver'
108+
$userLanguagePreferenceProvider: '@Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProvider'
109+
$groupedContentFormFieldsProvider: '@Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider'
110+
$contentService: '@ibexa.api.service.content'
110111
parent: Ibexa\Core\MVC\Symfony\Controller\Controller
111112
tags:
112113
- { name: controller.service_arguments }

src/lib/Event/ContentCreateFieldOptionsEvent.php

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,27 @@
1111
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
1212
use Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct;
1313
use Symfony\Component\Form\FormInterface;
14-
use Symfony\Contracts\EventDispatcher\Event;
1514

16-
final class ContentCreateFieldOptionsEvent extends Event
15+
final class ContentCreateFieldOptionsEvent extends StructFieldOptionsEvent
1716
{
1817
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct */
1918
private $contentCreateStruct;
2019

21-
/** @var \Symfony\Component\Form\FormInterface */
22-
private $parentForm;
23-
24-
/** @var \Ibexa\Contracts\ContentForms\Data\Content\FieldData */
25-
private $fieldData;
26-
27-
/** @var array */
28-
private $options;
29-
3020
public function __construct(
3121
ContentCreateStruct $contentCreateStruct,
3222
FormInterface $parentForm,
3323
FieldData $fieldData,
3424
array $options
3525
) {
3626
$this->contentCreateStruct = $contentCreateStruct;
37-
$this->parentForm = $parentForm;
38-
$this->fieldData = $fieldData;
39-
$this->options = $options;
27+
28+
parent::__construct($parentForm, $fieldData, $options);
4029
}
4130

4231
public function getContentCreateStruct(): ContentCreateStruct
4332
{
4433
return $this->contentCreateStruct;
4534
}
46-
47-
public function getParentForm(): FormInterface
48-
{
49-
return $this->parentForm;
50-
}
51-
52-
public function getFieldData(): FieldData
53-
{
54-
return $this->fieldData;
55-
}
56-
57-
public function getOptions(): array
58-
{
59-
return $this->options;
60-
}
61-
62-
public function setOptions(array $options): void
63-
{
64-
$this->options = $options;
65-
}
66-
67-
public function setOption(string $option, $value): void
68-
{
69-
$this->options[$option] = $value;
70-
}
71-
72-
public function getOption(string $option)
73-
{
74-
return $this->options[$option] ?? null;
75-
}
7635
}
7736

7837
class_alias(ContentCreateFieldOptionsEvent::class, 'EzSystems\EzPlatformContentForms\Event\ContentCreateFieldOptionsEvent');

src/lib/Event/ContentFormEvents.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ final class ContentFormEvents
7474
* Triggered when resolving Field Type options for content create form.
7575
*/
7676
public const CONTENT_CREATE_FIELD_OPTIONS = 'content.create.field.options';
77+
78+
/**
79+
* Triggered when resolving Field Type options for user edit form.
80+
*/
81+
public const USER_EDIT_FIELD_OPTIONS = 'user.edit.field.options';
82+
83+
/**
84+
* Triggered when resolving Field Type options for user create form.
85+
*/
86+
public const USER_CREATE_FIELD_OPTIONS = 'user.create.field.options';
7787
}
7888

7989
class_alias(ContentFormEvents::class, 'EzSystems\EzPlatformContentForms\Event\ContentFormEvents');

src/lib/Event/ContentUpdateFieldOptionsEvent.php

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,15 @@
1212
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
1313
use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct;
1414
use Symfony\Component\Form\FormInterface;
15-
use Symfony\Contracts\EventDispatcher\Event;
1615

17-
final class ContentUpdateFieldOptionsEvent extends Event
16+
final class ContentUpdateFieldOptionsEvent extends StructFieldOptionsEvent
1817
{
1918
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content */
2019
private $content;
2120

2221
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct */
2322
private $contentUpdateStruct;
2423

25-
/** @var \Symfony\Component\Form\FormInterface */
26-
private $parentForm;
27-
28-
/** @var \Ibexa\Contracts\ContentForms\Data\Content\FieldData */
29-
private $fieldData;
30-
31-
/** @var array */
32-
private $options;
33-
3424
public function __construct(
3525
Content $content,
3626
ContentUpdateStruct $contentUpdateStruct,
@@ -40,9 +30,8 @@ public function __construct(
4030
) {
4131
$this->content = $content;
4232
$this->contentUpdateStruct = $contentUpdateStruct;
43-
$this->parentForm = $parentForm;
44-
$this->fieldData = $fieldData;
45-
$this->options = $options;
33+
34+
parent::__construct($parentForm, $fieldData, $options);
4635
}
4736

4837
public function getContent(): Content
@@ -54,36 +43,6 @@ public function getContentUpdateStruct(): ContentUpdateStruct
5443
{
5544
return $this->contentUpdateStruct;
5645
}
57-
58-
public function getParentForm(): FormInterface
59-
{
60-
return $this->parentForm;
61-
}
62-
63-
public function getFieldData(): FieldData
64-
{
65-
return $this->fieldData;
66-
}
67-
68-
public function getOptions(): array
69-
{
70-
return $this->options;
71-
}
72-
73-
public function setOptions(array $options): void
74-
{
75-
$this->options = $options;
76-
}
77-
78-
public function setOption(string $option, $value): void
79-
{
80-
$this->options[$option] = $value;
81-
}
82-
83-
public function getOption(string $option)
84-
{
85-
return $this->options[$option] ?? null;
86-
}
8746
}
8847

8948
class_alias(ContentUpdateFieldOptionsEvent::class, 'EzSystems\EzPlatformContentForms\Event\ContentUpdateFieldOptionsEvent');
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\ContentForms\Event;
10+
11+
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
12+
use Symfony\Component\Form\FormInterface;
13+
use Symfony\Contracts\EventDispatcher\Event;
14+
15+
abstract class StructFieldOptionsEvent extends Event
16+
{
17+
protected FormInterface $parentForm;
18+
19+
protected FieldData $fieldData;
20+
21+
/** @var array<string, mixed> */
22+
protected array $options;
23+
24+
public function __construct(
25+
FormInterface $parentForm,
26+
FieldData $fieldData,
27+
array $options
28+
) {
29+
$this->parentForm = $parentForm;
30+
$this->fieldData = $fieldData;
31+
$this->options = $options;
32+
}
33+
34+
public function getParentForm(): FormInterface
35+
{
36+
return $this->parentForm;
37+
}
38+
39+
public function getFieldData(): FieldData
40+
{
41+
return $this->fieldData;
42+
}
43+
44+
/**
45+
* @return array<string, mixed>
46+
*/
47+
public function getOptions(): array
48+
{
49+
return $this->options;
50+
}
51+
52+
/**
53+
* @param array<string, mixed> $options
54+
*/
55+
public function setOptions(array $options): void
56+
{
57+
$this->options = $options;
58+
}
59+
60+
/**
61+
* @param mixed $value
62+
*/
63+
public function setOption(string $option, $value): void
64+
{
65+
$this->options[$option] = $value;
66+
}
67+
68+
/**
69+
* @return mixed|null
70+
*/
71+
public function getOption(string $option)
72+
{
73+
return $this->options[$option] ?? null;
74+
}
75+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\ContentForms\Event;
10+
11+
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
12+
use Ibexa\Contracts\Core\Repository\Values\User\UserCreateStruct;
13+
use Symfony\Component\Form\FormInterface;
14+
15+
final class UserCreateFieldOptionsEvent extends StructFieldOptionsEvent
16+
{
17+
private UserCreateStruct $userCreateStruct;
18+
19+
public function __construct(
20+
UserCreateStruct $userCreateStruct,
21+
FormInterface $parentForm,
22+
FieldData $fieldData,
23+
array $options
24+
) {
25+
$this->userCreateStruct = $userCreateStruct;
26+
27+
parent::__construct($parentForm, $fieldData, $options);
28+
}
29+
30+
public function getUserCreateStruct(): UserCreateStruct
31+
{
32+
return $this->userCreateStruct;
33+
}
34+
}

0 commit comments

Comments
 (0)