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 27c6ac8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
34 changes: 18 additions & 16 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 $options = []): array

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::field() has parameter $options with no value type specified in iterable type array.

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::field() has parameter $options with no value type specified in iterable type array.

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::field() has parameter $options with no value type specified in iterable type array.

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::field() has parameter $options with no value type specified in iterable type array.

Check failure on line 54 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig
{
$options = $this->getOptions($formName, $definition, $formConfig);
$fieldOptions = $this->getOptions($formName, $definition, $options);

$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,16 +88,17 @@ 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 $options = []): array

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::getOptions() has parameter $options with no value type specified in iterable type array.

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::getOptions() has parameter $options with no value type specified in iterable type array.

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::getOptions() has parameter $options with no value type specified in iterable type array.

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

Method Valantic\PimcoreFormsBundle\Form\Builder::getOptions() has parameter $options with no value type specified in iterable type array.

Check failure on line 91 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

PHPDoc tag @param references unknown parameter: $formConfig
{
$options = $definition['options'];
$fieldOptions = $definition['options'];
$formConfig = $options['config'];

Check failure on line 94 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

Offset 'config' does not exist on array|null.

Check failure on line 94 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.1 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

Offset 'config' does not exist on array|null.

Check failure on line 94 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-lowest) on ubuntu-latest)

Offset 'config' does not exist on array|null.

Check failure on line 94 in src/Form/Builder.php

View workflow job for this annotation

GitHub Actions / phpstan (PHP 8.2 with Pimcore ^11.0 (prefer-stable) on ubuntu-latest)

Offset 'config' does not exist on array|null.

if ($formConfig['translate']['field_labels'] && !empty($options['label'])) {
$options['label'] = $this->translator->trans($options['label']);
if ($formConfig['translate']['field_labels'] && !empty($fieldOptions['label'])) {
$fieldOptions['label'] = $this->translator->trans($fieldOptions['label']);
}

if (in_array($this->getType($definition['type']), [DateType::class, TimeType::class], true)) {
$options['widget'] ??= 'single_text';
$fieldOptions['widget'] ??= 'single_text';
}
if ($this->getType($definition['type']) === ChoiceType::class) {
if (
Expand All @@ -111,7 +112,7 @@ protected function getOptions(string $formName, array $definition, array $formCo
continue;
}

$options[$key] = array_combine(
$fieldOptions[$key] = array_combine(
array_map(
fn (string $key): string => $this->translator->trans($key),
array_keys($definition['options'][$key])
Expand All @@ -127,14 +128,15 @@ protected function getOptions(string $formName, array $definition, array $formCo
$choices->setFieldConfig($formConfig);
}

$options['choices'] = $choices->choices();
$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);
$context = $options['context'] ?? null;
$fieldOptions['choices'] = $choices->choices($context);
$fieldOptions['choice_value'] = fn ($a) => $a;
$fieldOptions['choice_label'] = fn ($choice, $key, $value) => $choices->choiceLabel($choice, $key, $value, $context);
$fieldOptions['choice_attr'] = fn ($choice, $key, $value) => $choices->choiceAttribute($choice, $key, $value, $context);
}
}

return $options;
return $fieldOptions;
}

/**
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;
}
6 changes: 4 additions & 2 deletions src/Service/FormService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ public function __construct(
$this->liform = $liform;
}

public function build(string $name): FormBuilderInterface
public function build(string $name, mixed $context): FormBuilderInterface
{
$config = $this->getConfig($name);
$options['config'] = $config;
$options['context'] = $context;
$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, $options));
}

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

0 comments on commit 27c6ac8

Please sign in to comment.