0

My Code looks like this:

$fields['color'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Color'))
  ->setDescription(t('The color of the fruit.'))
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default:taxonomy_term')
  ->setSetting('handler_settings', ['target_bundles' => ['Color' => 'Color']])
  ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'author',
    'weight' => -3,
  ])
  ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
    'weight' => -3,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

I have a vocabulary with the name : 'Color'
I have 3 terms in it : 'Red', 'Yellow', 'Green'

The error I'm getting is this :

enter image description here

I'm trying to figure out what went wrong.

1
  • Just Change the Color to color in ['target_bundles' => ['Color' => 'Color']]) if you create a vocabulary caller Color the machine name will be color Not Color .
    – berramou
    Sep 6, 2019 at 16:15

1 Answer 1

0

Change the Color to color in ['target_bundles' => ['Color' => 'Color']]) if you create a vocabulary caller Color the machine name will be color Not Color So try with:

$fields['color'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Color'))
      ->setDescription(t('The color of the fruit.'))
      ->setSetting('target_type', 'taxonomy_term')
      ->setSetting('handler', 'default:taxonomy_term')
      // Target is color not Color.
      ->setSetting('handler_settings', ['target_bundles' => ['color' => 'Color']])
      ->setDisplayOptions('view', [
        'label'  => 'above',
        'type'   => 'author',
        'weight' => -3,
      ])
      ->setDisplayOptions('form', [
        'type'     => 'entity_reference_autocomplete',
        'settings' => [
          'match_operator'    => 'CONTAINS',
          'size'              => 60,
          'autocomplete_type' => 'tags',
          'placeholder'       => '',
        ],
        'weight'   => -3,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
5
  • Thanks for the reply. My machine name is: fruits_color. So should I change it to ['fruits_color' => 'Color'] ? Sep 9, 2019 at 5:42
  • welcome, yes you should change it to ['fruits_color' => 'Color']
    – berramou
    Sep 9, 2019 at 8:47
  • Now, getting a different error, 'taste' column unknown. not found in field list. sorry but cant understand this error Sep 9, 2019 at 9:41
  • so you should debug this error not from this code there is no taste field as i can see
    – berramou
    Sep 9, 2019 at 9:45
  • Okay, Thankyou sir. Sep 9, 2019 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.