Skip to content

Commit 0e365cc

Browse files
committed
IBX-10936: Add expression parser configuration
1 parent a8fb412 commit 0e365cc

File tree

7 files changed

+97
-6
lines changed

7 files changed

+97
-6
lines changed

src/bundle/Controller/ContentTypeFieldsByExpressionController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public function loadFieldDefinitionsFromExpression(Request $request): FieldDefin
4040
);
4141

4242
try {
43-
$fieldDefinitions = $this->fieldsByExpressionService->getFieldsFromExpression($input->expression);
43+
$fieldDefinitions = $this->fieldsByExpressionService->getFieldsFromExpression(
44+
$input->expression,
45+
$input->configuration,
46+
);
4447
} catch (FieldTypeExpressionParserException | LogicException $e) {
4548
throw new BadRequestException($e->getMessage(), $e->getCode(), $e);
4649
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Bundle\AdminUi\DependencyInjection\Configuration\Parser;
10+
11+
use Ibexa\Bundle\Core\DependencyInjection\Configuration\AbstractParser;
12+
use Ibexa\Bundle\Core\DependencyInjection\Configuration\SiteAccessAware\ContextualizerInterface;
13+
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
14+
15+
/**
16+
* Configuration parser for mapping content type fields expressions.
17+
*
18+
* Example configuration:
19+
*
20+
* ```yaml
21+
* ibexa:
22+
* system:
23+
* default:
24+
* content_type_fields_by_expression:
25+
* configurations:
26+
* text_fields: [ezstring, eztext]
27+
* ```
28+
*/
29+
final class ContentTypeFieldsByExpression extends AbstractParser
30+
{
31+
public function addSemanticConfig(NodeBuilder $nodeBuilder): void
32+
{
33+
$nodeBuilder
34+
->arrayNode('content_type_fields_by_expression')
35+
->addDefaultsIfNotSet()
36+
->children()
37+
->arrayNode('configurations')
38+
->useAttributeAsKey('name')
39+
->arrayPrototype()
40+
->scalarPrototype()->end()
41+
->end()
42+
->defaultValue([])
43+
->end()
44+
->end()
45+
->end();
46+
}
47+
48+
/**
49+
* @param array<string,mixed> $scopeSettings
50+
*/
51+
public function mapConfig(array &$scopeSettings, $currentScope, ContextualizerInterface $contextualizer): void
52+
{
53+
if (!isset($scopeSettings['content_type_fields_by_expression'])) {
54+
return;
55+
}
56+
57+
$configurations = $scopeSettings['content_type_fields_by_expression']['configurations'] ?? [];
58+
foreach ($configurations as $name => $config) {
59+
$contextualizer->setContextualParameter(
60+
"content_type_fields_by_expression.configurations.$name",
61+
$currentScope,
62+
$config
63+
);
64+
}
65+
}
66+
}

src/bundle/IbexaAdminUiBundle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private function getConfigParsers(): array
7171
new Parser\AdminUiForms(),
7272
new Parser\ContentType(),
7373
new Parser\ContentTypeGroup(),
74+
new Parser\ContentTypeFieldsByExpression(),
7475
new Parser\SubtreePath(),
7576
new Parser\LimitationValueTemplates(),
7677
new Parser\Assets(),

src/contracts/ContentType/ContentTypeFieldsByExpressionServiceInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ContentTypeFieldsByExpressionServiceInterface
1919
* @throws \Ibexa\AdminUi\Exception\FieldTypeExpressionParserException
2020
* @throws \LogicException
2121
*/
22-
public function getFieldsFromExpression(string $expression): array;
22+
public function getFieldsFromExpression(string $expression, ?string $configuration = null): array;
2323

2424
/**
2525
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException

src/lib/ContentType/ContentTypeFieldsByExpressionService.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as ContentTypeHandler;
1515
use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
1616
use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition;
17+
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
1718
use Ibexa\Core\FieldType\FieldTypeRegistry;
1819
use Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper;
1920

@@ -25,32 +26,49 @@ final class ContentTypeFieldsByExpressionService implements ContentTypeFieldsByE
2526

2627
private ContentTypeDomainMapper $contentTypeDomainMapper;
2728

29+
private ConfigResolverInterface $configResolver;
30+
2831
public function __construct(
2932
ContentTypeFieldsExtractorInterface $fieldsExtractor,
3033
ContentTypeHandler $contentTypeHandler,
3134
ContentLanguageHandler $contentLanguageHandler,
32-
FieldTypeRegistry $fieldTypeRegistry
35+
FieldTypeRegistry $fieldTypeRegistry,
36+
ConfigResolverInterface $configResolver
3337
) {
3438
$this->fieldsExtractor = $fieldsExtractor;
3539
$this->contentTypeHandler = $contentTypeHandler;
3640
// Building ContentTypeDomainMapper manually to avoid circular dependency.
41+
//TODO handle after core merge
3742
$this->contentTypeDomainMapper = new ContentTypeDomainMapper(
3843
$contentTypeHandler,
3944
$contentLanguageHandler,
4045
$fieldTypeRegistry,
4146
);
47+
$this->configResolver = $configResolver;
4248
}
4349

44-
public function getFieldsFromExpression(string $expression): array
50+
public function getFieldsFromExpression(string $expression, ?string $configuration = null): array
4551
{
4652
$contentTypeFieldIds = $this->fieldsExtractor->extractFieldsFromExpression($expression);
4753

54+
$configuration = $configuration !== null
55+
? $this->configResolver->getParameter("content_type_fields_by_expression.configurations.$configuration")
56+
: null;
57+
4858
$contentTypeFieldDefinitions = [];
4959
foreach ($contentTypeFieldIds as $contentTypeFieldId) {
5060
$persistenceFieldDefinition = $this->contentTypeHandler->getFieldDefinition(
5161
$contentTypeFieldId,
5262
ContentType::STATUS_DEFINED,
5363
);
64+
65+
if (
66+
$configuration !== null
67+
&& !in_array($persistenceFieldDefinition->fieldType, $configuration, true)
68+
) {
69+
continue;
70+
}
71+
5472
$apiFieldDefinition = $this->contentTypeDomainMapper->buildFieldDefinitionDomainObject(
5573
$persistenceFieldDefinition,
5674
$persistenceFieldDefinition->mainLanguageCode,

src/lib/REST/Input/Parser/ContentType/FieldDefinitionExpression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function parse(array $data, ParsingDispatcher $parsingDispatcher): FieldD
2323
);
2424
}
2525

26-
return new FieldDefinitionExpressionValue($data['expression']);
26+
return new FieldDefinitionExpressionValue($data['expression'], $data['configuration'] ?? null);
2727
}
2828
}

src/lib/REST/Value/ContentType/FieldDefinitionExpression.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ final class FieldDefinitionExpression extends RestValue
1414
{
1515
public string $expression;
1616

17-
public function __construct(string $expression)
17+
public ?string $configuration = null;
18+
19+
public function __construct(string $expression, ?string $configuration = null)
1820
{
1921
$this->expression = $expression;
22+
$this->configuration = $configuration;
2023
}
2124
}

0 commit comments

Comments
 (0)