Skip to content

Commit eeecf18

Browse files
committed
feature #608 [Autocomplete] Distinct autocomplete field from normal field in templates (AlexandreGerault)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Autocomplete] Distinct autocomplete field from normal field in templates | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Tickets | Fix #581 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT As discussed in the corresponding issue, it should allow to easily distinct autocomplete fields from non autocomplete fields with a variable added to the view. Before, I used to write this: ```twig {% block choice_widget %} {% set attr = attr|merge({'class': 'my-input-css-class'}) %} {# Condition simplified, let's assume the stimulus controller can only be autocomplete for the example #} {% if attr['data-controller'] is not defined %} <select name="{{ full_name }}" {{ block('widget_attributes') }}> {% for choice in choices %} <option value="{{ choice.value }}" {% if choice.value == value %}selected{% endif %}>{{ choice.label }}</option> {% endfor %} </select> {% else %} {{ parent() }} {% endif %} {% endblock %} ``` With this, it should be able to become as below: ```twig {% block choice_widget %} {% set attr = attr|merge({'class': 'my-input-css-class'}) %} {% if not autocomplete %} <select name="{{ full_name }}" {{ block('widget_attributes') }}> {% for choice in choices %} <option value="{{ choice.value }}" {% if choice.value == value %}selected{% endif %}>{{ choice.label }}</option> {% endfor %} </select> {% else %} {{ parent() }} {% endif %} {% endblock %} ``` Commits ------- a386722 [Autocomplete] Distinct autocomplete field from normal field in templates
2 parents 8e5cab3 + a386722 commit eeecf18

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ public static function getExtendedTypes(): iterable
3939
];
4040
}
4141

42-
public function finishView(FormView $view, FormInterface $form, array $options)
42+
public function finishView(FormView $view, FormInterface $form, array $options): void
4343
{
4444
if (!$options['autocomplete']) {
45+
$view->vars['uses_autocomplete'] = false;
46+
4547
return;
4648
}
4749

@@ -83,10 +85,11 @@ public function finishView(FormView $view, FormInterface $form, array $options)
8385
$attr['data-'.$controllerName.'-'.$name.'-value'] = $value;
8486
}
8587

88+
$view->vars['uses_autocomplete'] = true;
8689
$view->vars['attr'] = $attr;
8790
}
8891

89-
public function configureOptions(OptionsResolver $resolver)
92+
public function configureOptions(OptionsResolver $resolver): void
9093
{
9194
$resolver->setDefaults([
9295
'autocomplete' => false,

0 commit comments

Comments
 (0)