1

I created a custom form in a custom module. (in a class that extends FormBase) I want to add a (very) simple html select element, this select must contains all taxonomy elements of a specific vocabulary, like this, where the integers are the taxonomy term IDs.

<select id="...>
    <option value="4">tutu</option>
    <option value="15">toto</option>
    <option value="61">tata</option>
    ...
    <option value="48">titi</option>
</select>

How can I create this <select> tag using the taxonomy term ID and the taxonomy term name? The users would not see the taxonomy term ID, but only the taxonomy term name.

With a vocabulary name, how can I generate the good structure (an array?) that allows me create the <select> tag?

5

You will need to get your terms (with a query or load the vocab), loop over them and create an array of term tid and term label. Set that array as the #options property on the select field in your form code.

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.