Skip to content

Commit

Permalink
feat: Filter node references by their defaultValued node-types (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Feb 10, 2025
1 parent 25d06cb commit 5a7b113
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
25 changes: 23 additions & 2 deletions src/Field/NodesFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): s
->addComment($this->getFieldSourcesName().' NodesSources direct field buffer.')
->addComment('@var '.$this->getRepositoryClass().'[]|null');

$nodeSourceClasses = [];
if (!empty($this->field->getDefaultValues())) {
$defaultValuesParsed = $this->field->getDefaultValuesAsArray();
$nodeTypes = array_map(
fn ($nodeTypeName) => $this->nodeTypeResolver->get($nodeTypeName),
$defaultValuesParsed
);
$nodeSourceClasses = array_map(
fn ($nodeType) => '\\'.$nodeType->getSourceEntityFullQualifiedClassName().'::class',
array_filter($nodeTypes)
);
}
$repositoryClass = $this->getRepositoryClass().'::class';
if (1 === count($nodeSourceClasses)) {
$repositoryClass = array_shift($nodeSourceClasses);
$nodeSourceClasses = '';
} else {
$nodeSourceClasses = implode(', ', $nodeSourceClasses);
}

$this->addFieldAutodoc($property);
$this->addFieldAttributes($property, $namespace, $this->isExcludingFieldFromJmsSerialization());

Expand All @@ -109,10 +129,11 @@ public function addFieldGetter(ClassType $classType, PhpNamespace $namespace): s
if (null === \$this->{$this->getFieldSourcesName()}) {
if (null !== \$this->objectManager) {
\$this->{$this->getFieldSourcesName()} = \$this->objectManager
->getRepository({$this->getRepositoryClass()}::class)
->getRepository({$repositoryClass})
->findByNodesSourcesAndFieldNameAndTranslation(
\$this,
'{$this->field->getName()}'
'{$this->field->getName()}',
[{$nodeSourceClasses}]
);
} else {
\$this->{$this->getFieldSourcesName()} = [];
Expand Down
12 changes: 9 additions & 3 deletions tests/Mocks/GeneratedNodesSources/NSMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ class NSMock extends NodesSources
* @var \mock\Entity\NodesSources[]|null
* ForBar hidden nodes field.
* Maecenas sed diam eget risus varius blandit sit amet non magna.
* Default values:
* - Mock
* - MockTwo
*/
#[JMS\Exclude]
private ?array $fooBarHiddenSources = null;
Expand Down Expand Up @@ -792,7 +795,8 @@ public function getFooBarSources(): array
->getRepository(\mock\Entity\NodesSources::class)
->findByNodesSourcesAndFieldNameAndTranslation(
$this,
'foo_bar'
'foo_bar',
[]
);
} else {
$this->fooBarSources = [];
Expand Down Expand Up @@ -827,7 +831,8 @@ public function getFooBarHiddenSources(): array
->getRepository(\mock\Entity\NodesSources::class)
->findByNodesSourcesAndFieldNameAndTranslation(
$this,
'foo_bar_hidden'
'foo_bar_hidden',
[\tests\mocks\GeneratedNodesSources\NSMock::class, \tests\mocks\GeneratedNodesSources\NSMockTwo::class]
);
} else {
$this->fooBarHiddenSources = [];
Expand Down Expand Up @@ -862,7 +867,8 @@ public function getFooBarTypedSources(): array
->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class)
->findByNodesSourcesAndFieldNameAndTranslation(
$this,
'foo_bar_typed'
'foo_bar_typed',
[]
);
} else {
$this->fooBarTypedSources = [];
Expand Down
12 changes: 9 additions & 3 deletions tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ class NSMock extends NodesSources
* @var \mock\Entity\NodesSources[]|null
* ForBar hidden nodes field.
* Maecenas sed diam eget risus varius blandit sit amet non magna.
* Default values:
* - Mock
* - MockTwo
*/
#[JMS\Exclude]
private ?array $fooBarHiddenSources = null;
Expand Down Expand Up @@ -792,7 +795,8 @@ public function getFooBarSources(): array
->getRepository(\mock\Entity\NodesSources::class)
->findByNodesSourcesAndFieldNameAndTranslation(
$this,
'foo_bar'
'foo_bar',
[]
);
} else {
$this->fooBarSources = [];
Expand Down Expand Up @@ -827,7 +831,8 @@ public function getFooBarHiddenSources(): array
->getRepository(\mock\Entity\NodesSources::class)
->findByNodesSourcesAndFieldNameAndTranslation(
$this,
'foo_bar_hidden'
'foo_bar_hidden',
[\tests\mocks\GeneratedNodesSources\NSMock::class, \tests\mocks\GeneratedNodesSources\NSMockTwo::class]
);
} else {
$this->fooBarHiddenSources = [];
Expand Down Expand Up @@ -862,7 +867,8 @@ public function getFooBarTypedSources(): array
->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class)
->findByNodesSourcesAndFieldNameAndTranslation(
$this,
'foo_bar_typed'
'foo_bar_typed',
[]
);
} else {
$this->fooBarTypedSources = [];
Expand Down
4 changes: 4 additions & 0 deletions tests/NodeTypeAwareTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ protected function getMockNodeType(): NodeTypeInterface
->setExcludedFromSerialization(true)
->setLabel('ForBar hidden nodes field')
->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna')
->setDefaultValues(Yaml::dump([
'Mock',
'MockTwo',
]))
->setIndexed(false),
(new SimpleNodeTypeField())
->setName('foo_bar_typed')
Expand Down

0 comments on commit 5a7b113

Please sign in to comment.