1

I'm creating a form with an entity_autocomplete field targeting taxonomy terms, and I would like to restrict it to a certain vocabulary. Is there a way to do that with '#selection_settings'? I can't find any example.

This is the code I am using.

$form['p_cat_ac'] = array(
    '#type' => 'entity_autocomplete',
    '#title' => t('Product Category'),
    '#target_type' => 'taxonomy_term',
    '#selection_settings' => [
        'include_anonymous' => FALSE,
    ],
);
3

The vocabulary is the taxonomy term's bundle, so you just have to add a target_bundles array to the #selection_settings entry. In my case the desired vocabulary ID is 'category'.

$form['p_cat_ac'] = array(
    '#type' => 'entity_autocomplete',
    '#title' => t('Product Category'),
    '#target_type' => 'taxonomy_term',
    '#selection_settings' => [
        'include_anonymous' => FALSE,
        'target_bundles' => array('category'),
    ],
);
| improve this answer | |

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.