If a field on the node edit form is a entity relation to a taxonomy term vocabulary, I want the taxonomy terms be restricted on certain conditions. I don't know how to do it safely. I have tried it in hook_form alter(). Deep in the "array stack" of the form variable is the definition of the entity reference form field.

enter image description here

Is the only way to "hack" the "#process" array, or is there any object-oriented way to filter the items in the drop down element?
Is there any form "plugin" to filter the items in Drupal 8?


You're deep in the field definitions there, that's the wrong place, you can't change anything there and you don't need to.

In case of a select, the available options are already part of the form; they should be in $form[$field_name]['widget'] somewhere.
You could also try to use hook_options_list_alter().

  • hook_options_list_alter() looks like perfect for this job. Thanks for the hint! – jepster Aug 28 '17 at 18:52
  • 1
    I thought it might. Note that the hook only applies to the widget. Meaning, you can use it to filter/remove options as well as change their labels, but you can't add options as they then wouldn't exist during validation/render – Berdir Aug 28 '17 at 19:34

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.