1

I have a taxonomy with 2 levels

City1
-Park1
-Park2
-Park3 
City2
-Park4
-Park5
-Park6

In my form, I created a field that links to this taxonomy with an autocomplete widget. I also have a second field where the user chooses the city.

I would like to dynamically change the autocomplete value depending on the selected city. Which means if the user selects City1, the autocomplete will only filter through the values under City1.

After checking the form I've found that the widget entity_autocomplete have an attribute #selection_handler. Will it be possible to change this handler and somehow send the value of the selected city?

I think there should be an easier approach. Creating a custom autocomplete field based on a textfield doesn't seem like a good solution since it will not have (or I will need to do it manually) all the error handling and entity links.

Any suggestions ?

0

You can drop using entity_autocomplete and instead it use custom autocomplete field. Here is an example of using, you can translate this page from Russian to your language. Also the example contains the attached source files.

https://niklan.net/blog/77

In the example you can see a controller what creates the list with values. I suppose that you can pass a parameter to the controller (city_id) and SQL query will return set of resuts according the parameter.

You can add the parameter to the controller inside function buildForm() of your form. Also you can recreate the autocomplete element with new parameter after you selected a city from the list.

  • As I mentioned in my question, it will be better if I don't have to use textfield as it doesn't have any validation and I will have to do one myself. Is there any other way to achieve this ? – 113408 Sep 4 '17 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.