9

I'm on Drupal 8, using BaseFieldDefinition::create('entity_reference') on my custom entity type to add entity reference field;

I also use this ->setSetting('target_type', 'node') to get nodes but this gives me all nodes of any content type;

Is there any way or possibility to filter these nodes to specific content type?

Here is the code for my field:

$fields['project_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Project'))
      ->setDescription(t('The project ID, transaction was created for.'))
      ->setSetting('target_type', 'node')
      ->setSetting('handler', 'default')
      ->setTargetEntityTypeId('project')
      ->setDisplayOptions('view', array(
        'label'  => 'hidden',
        'type'   => 'project',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type'     => 'entity_reference_autocomplete',
        'weight'   => 5,
        'settings' => array(
          'match_operator'    => 'CONTAINS',
          'size'              => '60',
          'autocomplete_type' => 'tags',
          'placeholder'       => '',
        ),
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

I want this field to be referenced to Content Type called project and not all types of nodes.

2 Answers 2

10

You're looking for the handler_settings setting, which provides configuration for the handler you've chosen, in your case, you want:

$handler_settings = [
  'target_bundles' => [
    'project' => 'project',
  ],
];

As always with fields, widgets and formatters, just create a configurable field in the UI, export it and look at the settings there. You can always copy the settings and other things almost 1:1 from the exported YAML files to a PHP array.

0
11

Also you can set it with ->setSetting('handler_settings',['target_bundles'=>['YOUR_CONTENT_TYPE_MACHINE_NAME'=>'YOUR_CONTENT_TYPE_MACHINE_NAME']] ) , you can pass multi value to it. I mean

->setSetting('handler_settings',['target_bundles'=>['YOUR_CONTENT_TYPE_MACHINE_NAME1'=>'YOUR_CONTENT_TYPE_MACHINE_NAME1','YOUR_CONTENT_TYPE_MACHINE_NAME2'=>'YOUR_CONTENT_TYPE_MACHINE_NAME2','YOUR_CONTENT_TYPE_MACHINE_NAME3'=>'YOUR_CONTENT_TYPE_MACHINE_NAME3']] )

So the final solution for your case is :

$fields['project_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Project'))
      ->setDescription(t('The project ID, transaction was created for.'))
      ->setSetting('target_type', 'node')
      ->setSetting('handler', 'default')
      ->setSetting('handler_settings',['target_bundles'=>['project'=>'project']] )
      ->setDisplayOptions('view', array(
        'label'  => 'hidden',
        'type'   => 'project',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type'     => 'entity_reference_autocomplete',
        'weight'   => 5,
        'settings' => array(
          'match_operator'    => 'CONTAINS',
          'size'              => '60',
          'autocomplete_type' => 'tags',
          'placeholder'       => '',
        ),
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
2
  • This was helpful for me to set up an entity_reference field. Do you know if it is possible to: - use a rendered entity for the display type or view - use a media/image widget for the form display:
    – Daniel
    Oct 9, 2019 at 11:49
  • i.e. I currently I have the following: ->setSetting('handler', 'default') ->setDisplayOptions('form', array( 'type' => 'entity_reference_autocomplete', 'weight' => 5, 'settings' => array( 'match_operator' => 'CONTAINS', 'size' => 60, 'placeholder' => '', 'autocomplete_type' => 'tags', 'display_label' => TRUE, ), )) ->setDisplayOptions('view', array( 'label' => 'hidden', 'type' => 'list_default', 'weight' => 0, ))
    – Daniel
    Oct 9, 2019 at 11:53

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.