I tried the following code, but it doesn't work.
$form['field_name']['widget'][0]['target_id']['#default_value'] = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
The default widget used for the entity reference fields (with the exception of entity reference fields for taxonomy tags) is implemented by the EntityReferenceAutocompleteWidget
class which, in its formElement()
implementation, returns the following form element.
$element += [
'#type' => 'entity_autocomplete',
'#target_type' => $this->getFieldSetting('target_type'),
'#selection_handler' => $this->getFieldSetting('handler'),
'#selection_settings' => $selection_settings,
// Entity reference field items are handling validation themselves via
// the 'ValidReference' constraint.
'#validate_reference' => FALSE,
'#maxlength' => 1024,
'#default_value' => isset($referenced_entities[$delta]) ? $referenced_entities[$delta] : NULL,
'#size' => $this->getSetting('size'),
'#placeholder' => $this->getSetting('placeholder'),
];
Instead of $form['field_name']['widget'][0]['target_id']['#default_value']
, the code should set $form['field_name']['widget'][0]['#default_value']
.
$form['field_name']['widget'][0]['#default_value'] = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
As side note, to alter the form element(s) returned from a field widget, Drupal uses hook_field_widget_complete_form_alter()
(and hook_field_widget_complete_WIDGET_TYPE_form_alter()
). It has access to the following information.
$context['widget']
), which allows to access the widget ID (for example, entity_reference_autocomplete, entity_reference_autocomplete_tags) with $context['widget']->getPluginId()
$context['items']
), which allows to get the target type of the reference field ($context['items']->getFieldDefinition()->getFieldStorageDefinition()->getSetting('target_type')
)or the field type ($context['items']->getFieldDefinition()->getType()
), for exampleWhen possible, it should preferable to use this hook for field widgets, instead of hook_form_alter()
.
media_field_widget_complete_form_alter()
is the implementation of that hook done by the Media module.
form['field_name']['widget'][0]['#default_value']
? Because according to git.drupalcode.org/project/drupal/-/blob/9.5.x/core/lib/Drupal/… it should be an opposite: form['field_name']['widget'][0]['target_id']['#default_value']
Commented
Dec 17, 2023 at 20:55
I know this is tagged for Drupal 9, but if anyone finds this page from Google looking for a Drupal 7 solution, it took me about an hour of searching, but I finally found this:
function mymodule_form_alter(&$form, $form_state, $form_id) {
// .... //
$whatever_node = node_load($whatever_nid);
$form['field_event']['und'][0]['target_id']['#default_value'] = array ($whatever_node->nid => $whatever_node->title);
}
To clarify-- this is for when your entity reference is an auto-complete field.
This is for an Entity Reference, not a node reference. Also, if you're doing anything to the title of the node in hook_node_load, make sure you are setting the original node title.