By yzerman on
Hi, iam trying to set default value to node reference field (autocomplete) from nid.
For example my code is
$nid = \Drupal::request()->query->get('field_event');
$node = is_numeric($nid) ? \Drupal\node\Entity\Node::load($nid) : NULL;
if ($node && isset($form['field_event'])) {
$form['field_event']['widget'][0]['#target_id']['#default_value'] = $node;
}But the field doesn't accept the default value.
Thank you for your suggestons.
Comments
The default value is not set in the reference list
Hi,
I'm wondering if someone has been able update the reference list, so it shows selected widget.
My code is similar to the above code. Using print_r($node) I've seen that the correct value is stored in
$node->title->value and $node->title->label. However it's not shown on the form even when I replace ...=$node to ...=$node->title->value.
Any help is appreciated!
Cheers,
Marcel