Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Added context to ChoicesProvider for enabling dynamic choices #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Form/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ 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);
$options = $this->getOptions($formName, $definition, $formConfig, $context);

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

Expand Down Expand Up @@ -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 = null): FormBuilderInterface
Copy link
Author

@christianbader christianbader Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extend the following functions with context:

 /**
 * @return array<mixed>
 */
public function buildJson(string $name): array
{
    return $this->json($this->buildForm($name));
}

public function buildJsonString(string $name): string
{
    return json_encode($this->buildJson($name), \JSON_THROW_ON_ERROR);
}

public function buildForm(string $name): FormInterface
{
    return $this->build($name)->getForm();
}

{
$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
Loading