3

I have a custom entity type which has an entity reference field.

   $fields['referenced_article'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Article'))
  ->setDescription(t('The associated article.'))
  ->setSetting('target_type', 'node')
  ->setSetting('handler', 'default')
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => 60,
      'placeholder' => '',
    ),
    'weight' => -3,
  ))
  ->setDisplayConfigurable('form', TRUE);

This field returns now the nodes of all the node types when finding a match. How can I specify a node type? (I only want to show the nodes of type "article")

Do I need to use a specific setting for selecting content only one node type?

2

1 Answer 1

6

Try adding

->setSetting('handler_settings', ['target_bundles' => ['article' => 'article']]),

Source

2
  • I'm on Drupal 8.5 and by adding this I get no results at all :( Apr 15, 2018 at 15:41
  • 1
    @hugronaphor you might have to check that you don't have conflicting setSetting or setSettings calls in the field definition. If you do, only the last one wins.
    – Gogowitsch
    Jun 9, 2018 at 19:40

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.