How to create an entity reference field in Drupal 8 programmatically?

11,499

Solution 1

This will create an entity reference field for node content types with the article bundle.

$form['node_id'] = array(
    '#type' => 'entity_autocomplete',
    '#title' => $this->t('Node'),
    '#target_type' => 'node',
    '#selection_settings' => ['target_bundles' => ['article']],
    '#tags' => TRUE,
    '#size' => 30,
    '#maxlength' => 1024,
  );

Note you can change target_type for other entities like taxonomy_term.

Solution 2

So I can hopefully find this faster if I need to again...

Create field storage:

 if (!$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) {
   FieldStorageConfig::create([
     'field_name' => $field_name,
     'entity_type' => $entity_type,
     'type' => $type,
     'cardinality' => -1,

     // Optional to target entity types.
     'settings' => [
       'target_type' => $entity_target_type, // Ex: node, taxonomy_term.
     ],
   ])->save();
 }

Create field:

FieldConfig::create([
  'field_name' => $field_name,
  'entity_type' => $entity_type,
  'bundle' => $bundle,
  'label' => $label,
  'cardinality' => -1,

  // Optional to target bundles.
  'settings' => [
    'handler' => 'default',
    'handler_settings' => [
      'target_bundles' => [
        $vid1,
        $vid2,
      ],
    ],
  ],
])->save();
Share:
11,499

Related videos on Youtube

Allex
Author by

Allex

Web Developer by profession, Having more than 5 years experience in Web 2.0 technologies. like HTML, CSS, HTML5 and CSS3. Also having experience PHP, Drupal, Magento, X-cart and CakePHP etc.

Updated on June 15, 2022

Comments

  • Allex
    Allex over 1 year

    I want to create an entity reference field in Drupal 8 using my custom module. Like when someone clicks a link on a Drupal page, an entity reference field would be created in the page node type automatically.

    Can anyone help me with this?

  • Christian
    Christian about 7 years
    You might want to add some accompanying text to this. Its not overly clear what you're demonstrating.
  • Christian
    Christian about 7 years
    This seems to be code for creating a custom entity type? But, the question is about creating an entity reference field.
  • zkolnik
    zkolnik almost 6 years
    did you find any official documentation on your code above? Showing the key values? It should be noted that the "settings" will map (to a certain extent) the form field names found on the settings page. So in order to have a checkbox checked for a given content type; 'page' => 'page'.
  • sareed
    sareed almost 6 years
    I was unable to find much documentation online, which is a major reason I posted here. However the core Field module has a good example of this in the EntityReferenceAutoCreateTest class. The example there uses the target_bundles and target_type as well. Also, if you look at the settings page the checkboxes do too.
  • Balu Ertl
    Balu Ertl over 4 years
    This code snippet supposed to be used in the context of rendering a form. Therefore it adds the extra field widget to the form, but does nothing about creating the field itself – as OP asked. Afterall, once this form is being submitted, the data inserted into this widget will be lost, because the storage is not prepared for it.