0

I have a config entity form that has a taxonomy field, and it currently works with 1 term

$form['integration'] = array(
  '#type' => 'entity_autocomplete',
  '#title' => 'Integration',
  '#required' => TRUE,
  '#target_type' => 'taxonomy_term',
  '#default_value' => $entity->getIntegration(),
  '#selection_settings' => array(
    'target_bundles' => array(
      'integration',
    ),
  ),
);

That works fine, but I need to be able to select multiple terms, I've tried setting '#cardinality' => -1, but it's only allowing 1 element. I added a taxonomy field with unlimited cardinality to a content type, then set a breakpoint on the form to see how it was being set up for the page rendering. It looks like it creates a container form element, then I assume iterates over an array containing all the terms?

I would have thought there would be a way to set the cardinality of an element by making use of field widgets on the config form page, but I'm not sure that's possible.

3

(Content) fields have cardinality. There is no such concept for form elements.

However, entity_autcomplete does have a #tags flag, which you can see in the getInfo() method of the class: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Element%21EntityAutocomplete.php/function/EntityAutocomplete%3A%3AgetInfo/8.

If you set that to TRUE, then it allows you to select a comma-separated list of references.

If you're looking for the "Add another" functionality of entity_autocomplete or any other form element, then you have to build it yourself.

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.