Skip to content

Commit

Permalink
Added context to ChoiceOptionProvider for dynamic choices
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbader committed Mar 21, 2024
1 parent e7b3f7f commit f8f3ef7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
18 changes: 9 additions & 9 deletions src/Form/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public function form(string $name, array $config): FormBuilderInterface
*
* @return array{string,array}
*/
public function field(string $formName, array $definition, array $formConfig): array
public function field(string $formName, array $definition, array $formConfig, mixed $context): array
{
$options = $this->getOptions($formName, $definition, $formConfig);
$fieldOptions = $this->getOptions($formName, $definition, $formConfig, $context);

$constraints = $this->getConstraints($definition, $options);
$constraints = $this->getConstraints($definition, $fieldOptions);

if (!empty($constraints)) {
$options['constraints'] = $constraints;
$fieldOptions['constraints'] = $constraints;
}

return [$this->getType($definition['type']), $options];
return [$this->getType($definition['type']), $fieldOptions];
}

protected function getConstraintClass(string $name): string
Expand All @@ -88,7 +88,7 @@ protected function getType(string $name): string
*
* @return array<mixed>
*/
protected function getOptions(string $formName, array $definition, array $formConfig): array
protected function getOptions(string $formName, array $definition, array $formConfig, mixed $context): array
{
$options = $definition['options'];

Expand Down Expand Up @@ -127,10 +127,10 @@ protected function getOptions(string $formName, array $definition, array $formCo
$choices->setFieldConfig($formConfig);
}

$options['choices'] = $choices->choices();
$options['choices'] = $choices->choices($context);
$options['choice_value'] = fn ($a) => $a;
$options['choice_label'] = fn ($choice, $key, $value) => $choices->choiceLabel($choice, $key, $value);
$options['choice_attr'] = fn ($choice, $key, $value) => $choices->choiceAttribute($choice, $key, $value);
$options['choice_label'] = fn ($choice, $key, $value) => $choices->choiceLabel($choice, $key, $value, $context);
$options['choice_attr'] = fn ($choice, $key, $value) => $choices->choiceAttribute($choice, $key, $value, $context);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Form/Type/AbstractChoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ abstract class AbstractChoices implements ChoicesInterface, ConfigAwareInterface
protected array $fieldConfig;
protected string $formName;

public function choiceLabel(mixed $choice, mixed $key, mixed $value): ?string
public function choiceLabel(mixed $choice, mixed $key, mixed $value, mixed $context): ?string
{
return $key;
}

public function choiceAttribute(mixed $choice, mixed $key, mixed $value): array
public function choiceAttribute(mixed $choice, mixed $key, mixed $value, mixed $context): array
{
return [];
}
Expand Down
10 changes: 7 additions & 3 deletions src/Form/Type/ChoicesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@
interface ChoicesInterface
{
/**
* @param mixed $context
*
* @return array<mixed,mixed>
*/
public function choices(): array;
public function choices(mixed $context): array;

/**
* @param mixed $choice
* @param mixed $key
* @param mixed $value
* @param mixed $context
*
* @return string|null
*/
public function choiceLabel(mixed $choice, mixed $key, mixed $value): ?string;
public function choiceLabel(mixed $choice, mixed $key, mixed $value, mixed $context): ?string;

/**
* @param mixed $choice
* @param mixed $key
* @param mixed $value
* @param mixed $context
*
* @return array<string,string|int>
*/
public function choiceAttribute(mixed $choice, mixed $key, mixed $value): array;
public function choiceAttribute(mixed $choice, mixed $key, mixed $value, mixed $context): array;
}
4 changes: 2 additions & 2 deletions src/Service/FormService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function __construct(
$this->liform = $liform;
}

public function build(string $name): FormBuilderInterface
public function build(string $name, mixed $context): FormBuilderInterface
{
$config = $this->getConfig($name);
$form = $this->builder->form($name, $config);

foreach ($config['fields'] as $fieldName => $definition) {
$form->add($fieldName, ...$this->builder->field($name, $definition, $config));
$form->add($fieldName, ...$this->builder->field($name, $definition, $config, $context));
}

if ($form->getOption('csrf_protection') === true) {
Expand Down

0 comments on commit f8f3ef7

Please sign in to comment.