3

I have an entity that I has a taxonomy reference in it. it currently looks like this

enter image description here

and I want this

enter image description here

Is this one of the options in display options array? Or is it something different? How do I change this?

This is my code

$fields['category'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Category'))
      ->setDescription(t('Section this poll is to appear on'))
      ->setSetting('target_type', 'taxonomy_term')
      ->setTranslatable(TRUE)
            ->setDefaultValueCallback('Drupal\poll\Entity\Poll::getMain')
      ->setDisplayOptions('form', array(
        'type' => 'entity_reference_autocomplete',
        'weight' => -10,
        'settings' => array(
          'match_operator' => 'CONTAINS',
          'size' => '60',
          'autocomplete_type' => 'tags',
          'placeholder' => '',
        ),
      ))
      ->setDisplayConfigurable('form', TRUE)
            ->setRequired(FALSE);
  • 1
    Is it that you want the field widget to look different, or do you want it to behave as a multiple-value field instead of a single-value field? – Les Lim Sep 16 '15 at 21:19
  • I would like both. more so the multiple value field – BrandenB171 Sep 17 '15 at 1:22
7

You just need to set the cardinality of the field.

->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)

Note that you need to re-install (to be safe, uninstall before making the change) or write an update function to convert the database structure if you make this change.

  • This worked almost perfectly. I did this, uninstalled and reinstalled the module, but now i have a problem with view. I was using views contextual filters to change which one is displayed on different pages. In order to do that i needed to add a relationship. now it wont let me at the relationship, and my category field is not available for the contextual filter. why is that? – BrandenB171 Sep 17 '15 at 18:46
  • Yes, that's a known missing feature/bug, the automated views data generation doesn't work for multi-value base fields. See drupal.org/node/2477899. You'll either need to try the patches there but I have no idea in what state they are or define the views data yourself for now. Create a similar configurable field (that works), then dump the generated views data and adjust it. – Berdir Sep 18 '15 at 15:21

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.