Skip to content

Commit

Permalink
IBX-7935: Handled User-related structs in FieldCollectionType dispa…
Browse files Browse the repository at this point in the history
…tcher (#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
  • Loading branch information
barw4 authored Jun 17, 2024
1 parent 2caf048 commit 04dd868
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 141 deletions.
13 changes: 11 additions & 2 deletions src/bundle/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Ibexa\ContentForms\User\View\UserCreateView;
use Ibexa\ContentForms\User\View\UserUpdateView;
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\ContentTypeService;
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException;
use Ibexa\Contracts\Core\Repository\LanguageService;
Expand Down Expand Up @@ -53,6 +54,8 @@ class UserController extends Controller
/** @var \Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface */
private $groupedContentFormFieldsProvider;

private ContentService $contentService;

public function __construct(
ContentTypeService $contentTypeService,
UserService $userService,
Expand All @@ -61,7 +64,8 @@ public function __construct(
ActionDispatcherInterface $userActionDispatcher,
PermissionResolver $permissionResolver,
UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider,
GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider
GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider,
ContentService $contentService
) {
$this->contentTypeService = $contentTypeService;
$this->userService = $userService;
Expand All @@ -71,6 +75,7 @@ public function __construct(
$this->permissionResolver = $permissionResolver;
$this->userLanguagePreferenceProvider = $userLanguagePreferenceProvider;
$this->groupedContentFormFieldsProvider = $groupedContentFormFieldsProvider;
$this->contentService = $contentService;
}

/**
Expand Down Expand Up @@ -114,6 +119,7 @@ public function createAction(
$form = $this->createForm(UserCreateType::class, $data, [
'languageCode' => $language->languageCode,
'mainLanguageCode' => $language->languageCode,
'struct' => $data,
]);
$form->handleRequest($request);

Expand Down Expand Up @@ -152,6 +158,7 @@ public function createAction(
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @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\InvalidArgumentType
* @throws \Ibexa\Core\Base\Exceptions\UnauthorizedException
*/
Expand Down Expand Up @@ -192,9 +199,11 @@ public function editAction(
$userUpdate,
[
'location' => $location,
'content' => $this->contentService->loadContent($contentId),
'languageCode' => $language,
'mainLanguageCode' => $user->contentInfo->mainLanguageCode,
]
'struct' => $userUpdate,
],
);
$form->handleRequest($request);

Expand Down
17 changes: 9 additions & 8 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ services:

Ibexa\Bundle\ContentForms\Controller\UserController:
arguments:
- '@ibexa.api.service.content_type'
- '@ibexa.api.service.user'
- '@ibexa.api.service.location'
- '@ibexa.api.service.language'
- '@Ibexa\ContentForms\Form\ActionDispatcher\UserDispatcher'
- '@Ibexa\Contracts\Core\Repository\PermissionResolver'
- '@Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProvider'
- '@Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider'
$contentTypeService: '@ibexa.api.service.content_type'
$userService: '@ibexa.api.service.user'
$locationService: '@ibexa.api.service.location'
$languageService: '@ibexa.api.service.language'
$userActionDispatcher: '@Ibexa\ContentForms\Form\ActionDispatcher\UserDispatcher'
$permissionResolver: '@Ibexa\Contracts\Core\Repository\PermissionResolver'
$userLanguagePreferenceProvider: '@Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProvider'
$groupedContentFormFieldsProvider: '@Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider'
$contentService: '@ibexa.api.service.content'
parent: Ibexa\Core\MVC\Symfony\Controller\Controller
tags:
- { name: controller.service_arguments }
Expand Down
47 changes: 3 additions & 44 deletions src/lib/Event/ContentCreateFieldOptionsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,27 @@
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\EventDispatcher\Event;

final class ContentCreateFieldOptionsEvent extends Event
final class ContentCreateFieldOptionsEvent extends StructFieldOptionsEvent
{
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct */
private $contentCreateStruct;

/** @var \Symfony\Component\Form\FormInterface */
private $parentForm;

/** @var \Ibexa\Contracts\ContentForms\Data\Content\FieldData */
private $fieldData;

/** @var array */
private $options;

public function __construct(
ContentCreateStruct $contentCreateStruct,
FormInterface $parentForm,
FieldData $fieldData,
array $options
) {
$this->contentCreateStruct = $contentCreateStruct;
$this->parentForm = $parentForm;
$this->fieldData = $fieldData;
$this->options = $options;

parent::__construct($parentForm, $fieldData, $options);
}

public function getContentCreateStruct(): ContentCreateStruct
{
return $this->contentCreateStruct;
}

public function getParentForm(): FormInterface
{
return $this->parentForm;
}

public function getFieldData(): FieldData
{
return $this->fieldData;
}

public function getOptions(): array
{
return $this->options;
}

public function setOptions(array $options): void
{
$this->options = $options;
}

public function setOption(string $option, $value): void
{
$this->options[$option] = $value;
}

public function getOption(string $option)
{
return $this->options[$option] ?? null;
}
}

class_alias(ContentCreateFieldOptionsEvent::class, 'EzSystems\EzPlatformContentForms\Event\ContentCreateFieldOptionsEvent');
10 changes: 10 additions & 0 deletions src/lib/Event/ContentFormEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ final class ContentFormEvents
* Triggered when resolving Field Type options for content create form.
*/
public const CONTENT_CREATE_FIELD_OPTIONS = 'content.create.field.options';

/**
* Triggered when resolving Field Type options for user edit form.
*/
public const USER_EDIT_FIELD_OPTIONS = 'user.edit.field.options';

/**
* Triggered when resolving Field Type options for user create form.
*/
public const USER_CREATE_FIELD_OPTIONS = 'user.create.field.options';
}

class_alias(ContentFormEvents::class, 'EzSystems\EzPlatformContentForms\Event\ContentFormEvents');
47 changes: 3 additions & 44 deletions src/lib/Event/ContentUpdateFieldOptionsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,15 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\EventDispatcher\Event;

final class ContentUpdateFieldOptionsEvent extends Event
final class ContentUpdateFieldOptionsEvent extends StructFieldOptionsEvent
{
/** @var \Ibexa\Contracts\Core\Repository\Values\Content\Content */
private $content;

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

/** @var \Symfony\Component\Form\FormInterface */
private $parentForm;

/** @var \Ibexa\Contracts\ContentForms\Data\Content\FieldData */
private $fieldData;

/** @var array */
private $options;

public function __construct(
Content $content,
ContentUpdateStruct $contentUpdateStruct,
Expand All @@ -40,9 +30,8 @@ public function __construct(
) {
$this->content = $content;
$this->contentUpdateStruct = $contentUpdateStruct;
$this->parentForm = $parentForm;
$this->fieldData = $fieldData;
$this->options = $options;

parent::__construct($parentForm, $fieldData, $options);
}

public function getContent(): Content
Expand All @@ -54,36 +43,6 @@ public function getContentUpdateStruct(): ContentUpdateStruct
{
return $this->contentUpdateStruct;
}

public function getParentForm(): FormInterface
{
return $this->parentForm;
}

public function getFieldData(): FieldData
{
return $this->fieldData;
}

public function getOptions(): array
{
return $this->options;
}

public function setOptions(array $options): void
{
$this->options = $options;
}

public function setOption(string $option, $value): void
{
$this->options[$option] = $value;
}

public function getOption(string $option)
{
return $this->options[$option] ?? null;
}
}

class_alias(ContentUpdateFieldOptionsEvent::class, 'EzSystems\EzPlatformContentForms\Event\ContentUpdateFieldOptionsEvent');
75 changes: 75 additions & 0 deletions src/lib/Event/StructFieldOptionsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\ContentForms\Event;

use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Symfony\Component\Form\FormInterface;
use Symfony\Contracts\EventDispatcher\Event;

abstract class StructFieldOptionsEvent extends Event
{
protected FormInterface $parentForm;

protected FieldData $fieldData;

/** @var array<string, mixed> */
protected array $options;

public function __construct(
FormInterface $parentForm,
FieldData $fieldData,
array $options
) {
$this->parentForm = $parentForm;
$this->fieldData = $fieldData;
$this->options = $options;
}

public function getParentForm(): FormInterface
{
return $this->parentForm;
}

public function getFieldData(): FieldData
{
return $this->fieldData;
}

/**
* @return array<string, mixed>
*/
public function getOptions(): array
{
return $this->options;
}

/**
* @param array<string, mixed> $options
*/
public function setOptions(array $options): void
{
$this->options = $options;
}

/**
* @param mixed $value
*/
public function setOption(string $option, $value): void
{
$this->options[$option] = $value;
}

/**
* @return mixed|null
*/
public function getOption(string $option)
{
return $this->options[$option] ?? null;
}
}
34 changes: 34 additions & 0 deletions src/lib/Event/UserCreateFieldOptionsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\ContentForms\Event;

use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\User\UserCreateStruct;
use Symfony\Component\Form\FormInterface;

final class UserCreateFieldOptionsEvent extends StructFieldOptionsEvent
{
private UserCreateStruct $userCreateStruct;

public function __construct(
UserCreateStruct $userCreateStruct,
FormInterface $parentForm,
FieldData $fieldData,
array $options
) {
$this->userCreateStruct = $userCreateStruct;

parent::__construct($parentForm, $fieldData, $options);
}

public function getUserCreateStruct(): UserCreateStruct
{
return $this->userCreateStruct;
}
}
Loading

0 comments on commit 04dd868

Please sign in to comment.