4

I'm really struggling to add a taxonomy_term as an entity reference to a custom entity I've created programmatically.

Here is my configuration based on BaseFieldDefinition (the name of the vocabulary is "specialite":

$fields['speciality_field'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('speciality'))
  ->setRevisionable(TRUE)
  ->setSetting('target_type', 'taxonomy_term')
  >setSetting('handler_settings', ['target_bundles' => ['specialite' => 'specialite']])
  ->setSetting('handler', 'default')
  ->setTranslatable(TRUE)
  ->setDisplayOptions('view', array(
    'label' => 'hidden',
    'type' => 'author',
    'weight' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '10',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

This piece of code doesn't work, would you please help to find what I am missing, or redirect me to a good documentation explaining different values for handler_settings

Thank you so much.

  • include this in 'settings' => [ 'target_type' => 'taxonomy_term', ], – Shreya Shetty Sep 9 '16 at 8:50
  • already done , as mentionned in my question:->setSetting('target_type', 'taxonomy_term') – Asy Sep 9 '16 at 8:51
9

Well thank you, the answer of 4k4 was very useful. My problem was that for my field I wanted to reference to a taxonomy term, I had already created it and dropped it directly in the database. It's seems no to be the right way to do this, so I was having such trouble recreating it. So unisntalled my module (after removing all the data) , and reinstalled it , and then this piece of code worked perfectly to have a reference to a taxonomy term:

$fields['speciality'] = BaseFieldDefinition::create('entity_reference')
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default:taxonomy_term')
  ->setSetting('handler_settings', 
      array(
    'target_bundles' => array(
     'specialite' => 'specialite'
    )))
  ->setDisplayOptions('view', array(
    'label' => 'hidden',
    'type' => 'author',
    'weight' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => 3,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '10',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

PS: specialite is my the name of my vocabulary

| improve this answer | |
3

You can use the handler settings drupal generates, when you add a taxonomy field in ui:

settings:
  handler: 'default:taxonomy_term'
  handler_settings:
    target_bundles:
      tags: tags
    sort:
      field: _none
    auto_create: false

This is the config of tags in article, so you need to swap tags with your vocabulary.

| improve this answer | |
  • I'll try this. But please where do you find this settings generated in drupal ui? – Asy Sep 9 '16 at 13:31
  • This is stored in config. Use drush for example: drush cedit field.field.node.article.field_tags, or the config export options in ui. – 4k4 Sep 9 '16 at 13:36
  • I doesn't solve my problem, here my modification: ->setSetting('handler', 'default:taxonomy_term') ->setSetting('handler_settings', array( 'target_bundles' => array( 'specialite' => 'specialite' ))) – Asy Sep 9 '16 at 19:55

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.