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?

5

Try adding

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

Source

  • I'm on Drupal 8.5 and by adding this I get no results at all :( – hugronaphor Apr 15 '18 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 '18 at 19:40

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.