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();
Related videos on Youtube
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, 2022Comments
-
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 about 7 yearsYou might want to add some accompanying text to this. Its not overly clear what you're demonstrating.
-
Christian about 7 yearsThis seems to be code for creating a custom entity type? But, the question is about creating an entity reference field.
-
zkolnik almost 6 yearsdid 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 almost 6 yearsI 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 thetarget_bundles
andtarget_type
as well. Also, if you look at the settings page the checkboxes do too. -
Balu Ertl over 4 yearsThis 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.