2

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());

2 Answers 2

1

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.

  • The widget plugin instance ($context['widget']), which allows to access the widget ID (for example, entity_reference_autocomplete, entity_reference_autocomplete_tags) with $context['widget']->getPluginId()
  • The field values ($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 example

When 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.

1
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.