Skip to content

Commit 9a1427b

Browse files
committed
IBX-8753: Added ContentTypeIsCompany specification and improved ModifyFieldDefinitions logic
1 parent 1bdc60c commit 9a1427b

File tree

4 files changed

+112
-14
lines changed

4 files changed

+112
-14
lines changed

src/bundle/Resources/config/services.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,25 @@ services:
169169
- {name: kernel.event_subscriber}
170170

171171
Ibexa\AdminUi\Permission\Mapper\UsersWithPermissionInfoMapper: ~
172+
173+
Ibexa\AdminUi\Specification\ContentType\ContentTypeIsCompany:
174+
arguments:
175+
$companyContentTypeIdentifier: 'company'
176+
177+
Ibexa\AdminUi\Form\Type\Extension\ModifyFieldDefinitionsCollectionTypeExtension.corporate_fields:
178+
class: Ibexa\AdminUi\Form\Type\Extension\ModifyFieldDefinitionsCollectionTypeExtension
179+
arguments:
180+
$fieldTypeIdentifier: ''
181+
$fieldIdentifiers:
182+
- 'customer_group'
183+
- 'sales_rep'
184+
- 'contact'
185+
- 'default_address'
186+
- 'billing_address'
187+
- 'address_book'
188+
- 'members'
189+
$modifiedOptions:
190+
disable_remove: true
191+
$contentTypeSpecification: '@Ibexa\AdminUi\Specification\ContentType\ContentTypeIsCompany'
192+
tags:
193+
- { name: form.type_extension }

src/lib/Form/Type/Extension/EventSubscriber/ModifyFieldDefinitionFieldsSubscriber.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Ibexa\AdminUi\Form\Type\Extension\EventSubscriber;
1010

1111
use Ibexa\AdminUi\Form\Type\FieldDefinition\FieldDefinitionType;
12+
use Ibexa\Contracts\Core\Specification\SpecificationInterface;
1213
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1314
use Symfony\Component\Form\FormEvent;
1415
use Symfony\Component\Form\FormEvents;
@@ -20,16 +21,28 @@ final class ModifyFieldDefinitionFieldsSubscriber implements EventSubscriberInte
2021
{
2122
private string $fieldTypeIdentifier;
2223

24+
/** @var string[] */
25+
private array $fieldIdentifiers;
26+
2327
/** @var array<string, mixed> */
2428
private array $modifiedOptions;
2529

30+
private ?SpecificationInterface $contentTypeSpecification;
31+
2632
/**
33+
* @param string[]|string $fieldIdentifiers
2734
* @param array<string, mixed> $modifiedOptions
2835
*/
29-
public function __construct(string $fieldTypeIdentifier, array $modifiedOptions)
30-
{
36+
public function __construct(
37+
string $fieldTypeIdentifier,
38+
array $modifiedOptions,
39+
$fieldIdentifiers = [],
40+
?SpecificationInterface $contentTypeSpecification = null
41+
) {
3142
$this->fieldTypeIdentifier = $fieldTypeIdentifier;
3243
$this->modifiedOptions = $modifiedOptions;
44+
$this->fieldIdentifiers = is_array($fieldIdentifiers) ? $fieldIdentifiers : [$fieldIdentifiers];
45+
$this->contentTypeSpecification = $contentTypeSpecification;
3346
}
3447

3548
public static function getSubscribedEvents(): array
@@ -45,29 +58,40 @@ public function onPreSetData(FormEvent $event): void
4558
$data = $event->getData();
4659
$form = $event->getForm();
4760

48-
if (null === $data) {
61+
if (empty($data)) {
4962
return;
5063
}
5164

52-
foreach ($data as $fieldTypeIdentifier => $fieldTypeData) {
53-
if ($this->fieldTypeIdentifier !== $fieldTypeData->fieldDefinition->fieldTypeIdentifier) {
65+
$firstField = reset($data);
66+
$contentTypeDraft = $firstField->contentTypeData->contentTypeDraft ?? null;
67+
68+
if (
69+
$this->contentTypeSpecification !== null &&
70+
!$this->contentTypeSpecification->isSatisfiedBy($contentTypeDraft)
71+
) {
72+
return;
73+
}
74+
foreach ($data as $fieldIdentifier => $fieldTypeData) {
75+
$matchesType = $this->fieldTypeIdentifier === $fieldTypeData->fieldDefinition->fieldTypeIdentifier;
76+
$matchesId = in_array($fieldIdentifier, $this->fieldIdentifiers, true);
77+
78+
if (!($matchesType || $matchesId)) {
5479
continue;
5580
}
5681

57-
if (!$form->has($fieldTypeIdentifier)) {
58-
return;
82+
if (!$form->has($fieldIdentifier)) {
83+
continue;
5984
}
6085

61-
$baseFieldForm = $form->get($fieldTypeIdentifier);
86+
$baseFieldForm = $form->get($fieldIdentifier);
6287
$baseFieldFormName = $baseFieldForm->getName();
6388

64-
$form->remove($baseFieldFormName);
65-
6689
$options = array_merge(
6790
$baseFieldForm->getConfig()->getOptions(),
6891
$this->modifiedOptions
6992
);
7093

94+
$form->remove($baseFieldFormName);
7195
$form->add($baseFieldFormName, FieldDefinitionType::class, $options);
7296
}
7397
}

src/lib/Form/Type/Extension/ModifyFieldDefinitionsCollectionTypeExtension.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Ibexa\AdminUi\Form\Type\ContentType\FieldDefinitionsCollectionType;
1212
use Ibexa\AdminUi\Form\Type\Extension\EventSubscriber\ModifyFieldDefinitionFieldsSubscriber;
13+
use Ibexa\Contracts\Core\Specification\SpecificationInterface;
1314
use Symfony\Component\Form\AbstractTypeExtension;
1415
use Symfony\Component\Form\FormBuilderInterface;
1516

@@ -22,22 +23,38 @@ final class ModifyFieldDefinitionsCollectionTypeExtension extends AbstractTypeEx
2223
{
2324
private string $fieldTypeIdentifier;
2425

26+
/** @var string[] */
27+
private array $fieldIdentifiers;
28+
2529
/** @var array<string, mixed> */
2630
private array $modifiedOptions;
2731

32+
private ?SpecificationInterface $contentTypeSpecification;
33+
2834
/**
29-
* @param string $fieldTypeIdentifier
35+
* @param string|string[] $fieldIdentifiers
3036
* @param array<string, mixed> $modifiedOptions
3137
*/
32-
public function __construct(string $fieldTypeIdentifier, array $modifiedOptions)
33-
{
38+
public function __construct(
39+
string $fieldTypeIdentifier,
40+
array $modifiedOptions,
41+
$fieldIdentifiers = [],
42+
?SpecificationInterface $contentTypeSpecification = null
43+
) {
3444
$this->fieldTypeIdentifier = $fieldTypeIdentifier;
45+
$this->fieldIdentifiers = is_array($fieldIdentifiers) ? $fieldIdentifiers : [$fieldIdentifiers];
3546
$this->modifiedOptions = $modifiedOptions;
47+
$this->contentTypeSpecification = $contentTypeSpecification;
3648
}
3749

3850
public function buildForm(FormBuilderInterface $builder, array $options): void
3951
{
40-
$subscriber = new ModifyFieldDefinitionFieldsSubscriber($this->fieldTypeIdentifier, $this->modifiedOptions);
52+
$subscriber = new ModifyFieldDefinitionFieldsSubscriber(
53+
$this->fieldTypeIdentifier,
54+
$this->modifiedOptions,
55+
$this->fieldIdentifiers,
56+
$this->contentTypeSpecification
57+
);
4158

4259
foreach ($builder->all() as $fieldTypeGroup) {
4360
$fieldTypeGroup->addEventSubscriber($subscriber);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\AdminUi\Specification\ContentType;
10+
11+
use Ibexa\AdminUi\Exception\InvalidArgumentException;
12+
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
13+
use Ibexa\Contracts\Core\Specification\AbstractSpecification;
14+
15+
final class ContentTypeIsCompany extends AbstractSpecification
16+
{
17+
private string $companyContentTypeIdentifier;
18+
19+
public function __construct(string $companyContentTypeIdentifier)
20+
{
21+
$this->companyContentTypeIdentifier = $companyContentTypeIdentifier;
22+
}
23+
24+
public function isSatisfiedBy($contentType): bool
25+
{
26+
if (!$contentType instanceof ContentType) {
27+
throw new InvalidArgumentException(
28+
'$contentType',
29+
sprintf('Must be an instance of %s', ContentType::class)
30+
);
31+
}
32+
33+
return $contentType->identifier === $this->companyContentTypeIdentifier;
34+
}
35+
}

0 commit comments

Comments
 (0)