Skip to content

Commit

Permalink
Merge pull request #35 from Elao/placeholder
Browse files Browse the repository at this point in the history
Support nested keys + default support for placeholder
  • Loading branch information
Thomas Jarrand authored Dec 14, 2020
2 parents 293ca4a + 11c5f61 commit 3be5ff1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function getConfigTreeBuilder()
[
'label' => 'label',
'help' => 'help',
'[attr][placeholder]' => 'placeholder',
]
)
)
Expand All @@ -64,6 +65,7 @@ public function getConfigTreeBuilder()
$this->addKeysConfig(
'choice',
[
'placeholder' => 'placeholder',
'empty_value' => 'empty_value',
]
)
Expand Down
38 changes: 36 additions & 2 deletions Form/Extension/TreeAwareExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

/**
* Tree Aware Extension
Expand Down Expand Up @@ -56,6 +58,11 @@ abstract class TreeAwareExtension extends AbstractTypeExtension
*/
protected $defaultTranslationDomain;

/**
* @var PropertyAccessor
*/
protected $propertyAccessor;

/**
* Enable or disable automatic generation of missing labels
*
Expand Down Expand Up @@ -113,7 +120,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
{
if ($this->treeBuilder && $this->keyBuilder) {
foreach ($this->keys as $key => $value) {
if (isset($options[$key]) && $options[$key] === true) {
if ($this->optionEquals($options, $key, true)) {
$this->generateKey($view, $key, $value);
}
}
Expand All @@ -133,6 +140,33 @@ protected function generateKey(FormView &$view, $key, $value)
$view->vars['tree'] = $this->treeBuilder->getTree($view);
}

$view->vars[$key] = $this->keyBuilder->buildKeyFromTree($view->vars['tree'], $value);
$this->setVar($view->vars, $key, $this->keyBuilder->buildKeyFromTree($view->vars['tree'], $value));
}

protected function setVar(array &$vars, string $key, $value): void
{
if ($this->getPropertyAccessor()->isWritable($vars, $key)) {
$this->getPropertyAccessor()->setValue($vars, $key, $value);
} else {
$vars[$key] = $value;
}
}

protected function optionEquals(array $options, string $key, $value): bool
{
if ($this->getPropertyAccessor()->isReadable($options, $key)) {
return $this->getPropertyAccessor()->getValue($options, $key) === $value;
}

return isset($options[$key]) ? $options[$key] === $value : false;
}

protected function getPropertyAccessor(): PropertyAccessor
{
if (!$this->propertyAccessor) {
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}

return $this->propertyAccessor;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"require": {
"php": "^7.1.0",
"symfony/framework-bundle": "~2.8|~3.0|~4.0|~5.0",
"symfony/form": "~2.8|~3.0|~4.0|~5.0"
"symfony/form": "~2.8|~3.0|~4.0|~5.0",
"symfony/property-access": "~2.8|~3.0|~4.0|~5.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0",
Expand Down

0 comments on commit 3be5ff1

Please sign in to comment.