Use proper option injection for StructuralEntityType generate* functions.

Otherwise we get problems, if a page has multiple types with different options. This fixes issue #39
This commit is contained in:
Jan Böhmer 2020-04-09 13:46:28 +02:00
parent d5ff73b263
commit b53e1f0a66
2 changed files with 38 additions and 36 deletions

View file

@ -84,7 +84,7 @@ class CurrencyEntityType extends StructuralEntityType
});
}
public function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value): string
public function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value, $options): string
{
//Similar to StructuralEntityType, but we use the currency symbol instead if available
@ -93,12 +93,12 @@ class CurrencyEntityType extends StructuralEntityType
}
/** @var AbstractStructuralDBElement|null $parent */
$parent = $this->options['subentities_of'];
$parent = $options['subentities_of'];
/*** @var Currency $choice */
$level = $choice->getLevel();
//If our base entity is not the root level, we need to change the level, to get zero position
if (null !== $this->options['subentities_of']) {
if (null !== $options['subentities_of']) {
$level -= $parent->getLevel() - 1;
}
@ -112,7 +112,7 @@ class CurrencyEntityType extends StructuralEntityType
return $tmp;
}
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value): array
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
{
/** @var Currency $choice */
$tmp = [];
@ -123,7 +123,7 @@ class CurrencyEntityType extends StructuralEntityType
}
//Disable attribute if the choice is marked as not selectable
if ($this->options['disable_not_selectable'] && $choice->isNotSelectable()) {
if ($options['disable_not_selectable'] && $choice->isNotSelectable()) {
$tmp += ['disabled' => 'disabled'];
}

View file

@ -67,7 +67,7 @@ use Symfony\Component\Validator\Constraints\Choice;
class StructuralEntityType extends AbstractType
{
protected $em;
protected $options;
/**
* @var NodesListBuilder
*/
@ -102,11 +102,15 @@ class StructuralEntityType extends AbstractType
return $this->getEntries($options);
});
},
'choice_label' => function ($choice, $key, $value) {
return $this->generateChoiceLabels($choice, $key, $value);
'choice_label' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
return $this->generateChoiceLabels($choice, $key, $value, $options);
};
},
'choice_attr' => function ($choice, $key, $value) {
return $this->generateChoiceAttr($choice, $key, $value);
'choice_attr' => function (Options $options) {
return function ($choice, $key, $value) use ($options) {
return $this->generateChoiceAttr($choice, $key, $value, $options);
};
},
'choice_translation_domain' => false, //Don't translate the entity names
]);
@ -133,8 +137,6 @@ class StructuralEntityType extends AbstractType
*/
public function getEntries(Options $options): array
{
$this->options = $options;
return $this->builder->typeToNodesList($options['class'], null);
}
@ -240,16 +242,16 @@ class StructuralEntityType extends AbstractType
return $this->em->find($options['class'], $value->getID());
}
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value): array
protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
{
$tmp = [];
if ($this->options['show_fullpath_in_subtext'] && null !== $choice->getParent()) {
if ($options['show_fullpath_in_subtext'] && null !== $choice->getParent()) {
$tmp += ['data-subtext' => $choice->getParent()->getFullPath()];
}
//Disable attribute if the choice is marked as not selectable
if ($this->options['disable_not_selectable'] && $choice->isNotSelectable()) {
if ($options['disable_not_selectable'] && $choice->isNotSelectable()) {
$tmp += ['disabled' => 'disabled'];
}
@ -260,15 +262,15 @@ class StructuralEntityType extends AbstractType
return $tmp;
}
protected function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value): string
protected function generateChoiceLabels(AbstractStructuralDBElement $choice, $key, $value, $options): string
{
/** @var AbstractStructuralDBElement|null $parent */
$parent = $this->options['subentities_of'];
$parent = $options['subentities_of'];
/*** @var AbstractStructuralDBElement $choice */
$level = $choice->getLevel();
//If our base entity is not the root level, we need to change the level, to get zero position
if (null !== $this->options['subentities_of']) {
if (null !== $options['subentities_of']) {
$level -= $parent->getLevel() - 1;
}