Skip to content

Commit

Permalink
[BUGFIX] Generate types inside for loop correctly
Browse files Browse the repository at this point in the history
The concrete view helper is instantiated on the first iteration. On consecutive iterations
the same model is used as reference, so the last iteration overrides all the others before.

Resolves: #52
  • Loading branch information
brotkrueml committed May 26, 2020
1 parent 207f19d commit 18d6186
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Classes/Core/ViewHelpers/AbstractTypeViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ abstract class AbstractTypeViewHelper extends ViewHelper\AbstractViewHelper
private $isMainEntityOfWebPage = false;
private $parentPropertyName = '';

/** @var TypeInterface */
private $modelTemplate;

/** @var TypeInterface */
private $model;

Expand All @@ -40,7 +43,7 @@ public function __construct(TypeStack $typeStack = null, SchemaManager $schemaMa
{
$this->stack = $typeStack ?? GeneralUtility::makeInstance(TypeStack::class);
$this->schemaManager = $schemaManager ?? GeneralUtility::makeInstance(SchemaManager::class);
$this->model = TypeFactory::createType($this->getType());
$this->modelTemplate = TypeFactory::createType($this->getType());
}

public function initializeArguments()
Expand All @@ -52,13 +55,15 @@ public function initializeArguments()
$this->registerArgument(static::ARGUMENT_IS_MAIN_ENTITY_OF_WEBPAGE, 'bool', 'Set to true, if the type is the primary content of the web page', false, false);
$this->registerArgument(static::ARGUMENT_SPECIFIC_TYPE, 'string', 'A specific type of the chosen type. Only the properties of the chosen type are valid');

foreach ($this->model->getPropertyNames() as $property) {
foreach ($this->modelTemplate->getPropertyNames() as $property) {
$this->registerArgument($property, 'mixed', 'Property ' . $property);
}
}

public function render()
{
$this->model = clone $this->modelTemplate;

$this->checkSpecificTypeAttribute();
$this->checkAsAttribute();
$this->checkIsMainEntityOfWebPage();
Expand Down
7 changes: 7 additions & 0 deletions Tests/Unit/ViewHelpers/Type/ThingViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public function fluidTemplatesProvider(): iterable
'<script type="application/ld+json">{"@context":"http://schema.org","@type":"Organization","contactPoint":[{"@type":"ContactPoint","areaServed":"DE","contactType":"sales","telephone":"+49 30 123456789"},{"@type":"ContactPoint","areaServed":"PL","contactType":"sales","telephone":"+48 22 123456789"},{"@type":"ContactPoint","areaServed":"TR","contactType":"sales","telephone":"+90 212 123456789"}],"logo":"https://www.example.org/logo.png","name":"Acme Ltd.","url":"https://www.example.org/"}</script>',
];

yield 'Type inside "for" loop' => [
'<f:for each="{0: \'foo\', 1: \'bar\', 2: \'qux\'}" as="item" iteration="iterator">
<schema:type.thing name="{item}" identifier="{iterator.cycle}"/>
</f:for>',
'<script type="application/ld+json">{"@context":"http://schema.org","@graph":[{"@type":"Thing","identifier":"1","name":"foo"},{"@type":"Thing","identifier":"2","name":"bar"},{"@type":"Thing","identifier":"3","name":"qux"}]}</script>',
];

yield 'Type with -isMainEntityOfWebPage set to true without a WebPage' => [
'<schema:type.thing
-id="parentThing"
Expand Down

0 comments on commit 18d6186

Please sign in to comment.