Skip to content

Commit

Permalink
[Autocomplete] Add extra options support in AutocompleteChoiceTypeExt…
Browse files Browse the repository at this point in the history
…ension
  • Loading branch information
jakubtobiasz committed Dec 3, 2023
1 parent 39b9a6c commit 90f57b3
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\Autocomplete\Calculator\ChecksumCalculatorInterface;

/**
* Initializes the autocomplete Stimulus controller for any fields with the "autocomplete" option.
Expand All @@ -27,8 +28,10 @@
*/
final class AutocompleteChoiceTypeExtension extends AbstractTypeExtension
{
public function __construct(private ?TranslatorInterface $translator = null)
{
public function __construct(
private readonly ChecksumCalculatorInterface $checksumCalculator,
private readonly ?TranslatorInterface $translator = null,
) {
}

public static function getExtendedTypes(): iterable
Expand Down Expand Up @@ -79,6 +82,19 @@ public function finishView(FormView $view, FormInterface $form, array $options):
$values['min-characters'] = $options['min_characters'];
}

// TODO: This statement should be definitely refactored
if ($options['extra_options']) {
$this->validateExtraOptions($options['extra_options']);
$options['extra_options']['@checksum'] = $this->checksumCalculator->calculateForArray($options['extra_options']);
$extraOptions = base64_encode(json_encode($options['extra_options']));

$values['url'] = sprintf(
'%s?%s',
$values['url'],
http_build_query(['extra_options' => $extraOptions]),
);
}

$values['no-results-found-text'] = $this->trans($options['no_results_found_text']);
$values['no-more-results-text'] = $this->trans($options['no_more_results_text']);
$values['preload'] = $options['preload'];
Expand All @@ -91,6 +107,19 @@ public function finishView(FormView $view, FormInterface $form, array $options):
$view->vars['attr'] = $attr;
}

private function validateExtraOptions(array $extraOptions): void
{
foreach ($extraOptions as $option) {
if (!\is_scalar($option) && !\is_array($option) && null !== $option) {
throw new \InvalidArgumentException(sprintf('Extra option "%s" must be a scalar value, an array or null.', $option));
}

if (\is_array($option)) {
$this->validateExtraOptions($option);
}
}
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
Expand All @@ -104,6 +133,7 @@ public function configureOptions(OptionsResolver $resolver): void
'min_characters' => null,
'max_results' => 10,
'preload' => 'focus',
'extra_options' => [],
]);

// if autocomplete_url is passed, then HTML options are already supported
Expand Down

0 comments on commit 90f57b3

Please sign in to comment.