Skip to content

Commit dcccc72

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

File tree

5 files changed

+99
-13
lines changed

5 files changed

+99
-13
lines changed

src/bundle/Resources/config/services.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,24 @@ 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+
- 'customer_group'
182+
- 'sales_rep'
183+
- 'contact'
184+
- 'default_address'
185+
- 'billing_address'
186+
- 'address_book'
187+
- 'members'
188+
$modifiedOptions:
189+
disable_remove: true
190+
$contentTypeSpecification: '@Ibexa\AdminUi\Specification\ContentType\ContentTypeIsCompany'
191+
tags:
192+
- { name: form.type_extension }

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

Lines changed: 24 additions & 6 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;
@@ -18,18 +19,26 @@
1819
*/
1920
final class ModifyFieldDefinitionFieldsSubscriber implements EventSubscriberInterface
2021
{
21-
private string $fieldTypeIdentifier;
22+
/** @var string[] */
23+
private array $fieldTypeIdentifier;
2224

2325
/** @var array<string, mixed> */
2426
private array $modifiedOptions;
2527

28+
private ?SpecificationInterface $contentTypeSpecification;
29+
2630
/**
31+
* @param string[]|string $fieldTypeIdentifier
2732
* @param array<string, mixed> $modifiedOptions
2833
*/
29-
public function __construct(string $fieldTypeIdentifier, array $modifiedOptions)
30-
{
31-
$this->fieldTypeIdentifier = $fieldTypeIdentifier;
34+
public function __construct(
35+
$fieldTypeIdentifier,
36+
array $modifiedOptions,
37+
?SpecificationInterface $contentTypeSpecification = null
38+
) {
39+
$this->fieldTypeIdentifier = is_array($fieldTypeIdentifier) ? $fieldTypeIdentifier : [$fieldTypeIdentifier];
3240
$this->modifiedOptions = $modifiedOptions;
41+
$this->contentTypeSpecification = $contentTypeSpecification;
3342
}
3443

3544
public static function getSubscribedEvents(): array
@@ -45,12 +54,21 @@ public function onPreSetData(FormEvent $event): void
4554
$data = $event->getData();
4655
$form = $event->getForm();
4756

48-
if (null === $data) {
57+
if ($data === null || $data === []) {
4958
return;
5059
}
5160

61+
$firstField = reset($data);
62+
$contentType = $firstField->contentTypeData->contentTypeDraft ?? null;
63+
64+
if (
65+
$this->contentTypeSpecification !== null &&
66+
!$this->contentTypeSpecification->isSatisfiedBy($contentType)
67+
) {
68+
return;
69+
}
5270
foreach ($data as $fieldTypeIdentifier => $fieldTypeData) {
53-
if ($this->fieldTypeIdentifier !== $fieldTypeData->fieldDefinition->fieldTypeIdentifier) {
71+
if (!in_array($fieldTypeIdentifier, $this->fieldTypeIdentifier, true)) {
5472
continue;
5573
}
5674

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

Lines changed: 18 additions & 6 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

@@ -20,24 +21,35 @@
2021
*/
2122
final class ModifyFieldDefinitionsCollectionTypeExtension extends AbstractTypeExtension
2223
{
23-
private string $fieldTypeIdentifier;
24+
/** @var string[] */
25+
private array $fieldTypeIdentifier;
2426

2527
/** @var array<string, mixed> */
2628
private array $modifiedOptions;
2729

30+
private ?SpecificationInterface $contentTypeSpecification;
31+
2832
/**
29-
* @param string $fieldTypeIdentifier
33+
* @param string|string[] $fieldTypeIdentifier
3034
* @param array<string, mixed> $modifiedOptions
3135
*/
32-
public function __construct(string $fieldTypeIdentifier, array $modifiedOptions)
33-
{
34-
$this->fieldTypeIdentifier = $fieldTypeIdentifier;
36+
public function __construct(
37+
$fieldTypeIdentifier,
38+
array $modifiedOptions,
39+
?SpecificationInterface $contentTypeSpecification = null
40+
) {
41+
$this->fieldTypeIdentifier = is_array($fieldTypeIdentifier) ? $fieldTypeIdentifier : [$fieldTypeIdentifier];
3542
$this->modifiedOptions = $modifiedOptions;
43+
$this->contentTypeSpecification = $contentTypeSpecification;
3644
}
3745

3846
public function buildForm(FormBuilderInterface $builder, array $options): void
3947
{
40-
$subscriber = new ModifyFieldDefinitionFieldsSubscriber($this->fieldTypeIdentifier, $this->modifiedOptions);
48+
$subscriber = new ModifyFieldDefinitionFieldsSubscriber(
49+
$this->fieldTypeIdentifier,
50+
$this->modifiedOptions,
51+
$this->contentTypeSpecification
52+
);
4153

4254
foreach ($builder->all() as $fieldTypeGroup) {
4355
$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+
}

tests/lib/Form/Type/Extension/EventSubscriber/ModifyFieldDefinitionFieldsSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function setUp(): void
4949

5050
public function testDisableFields(): void
5151
{
52-
$fieldIdentifier = 'field_123456789';
52+
$fieldIdentifier = self::FIELD_TYPE_IDENTIFIER;
5353
$data = $this->getFormData($fieldIdentifier);
5454
$event = new FormEvent($this->form, $data);
5555
$options = [

0 commit comments

Comments
 (0)