Set default value for entity reference field in form

Public

Set default value for entity reference field in form

</> CopyGet raw version
php
  1. function ModuleName_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  2. //Autocomplete widget
  3. $nid = Put_here_node_ID;
  4. $node = node_load($nid);
  5. if ($node) {
  6. $form['field_ref_name'][LANGUAGE_NONE][0]['target_id']['#default_value'] = "$node->title ($node->nid)";
  7. }
  8.  
  9. //Select list widget
  10. $form['field_ref_name'][LANGUAGE_NONE]['#default_value'] = $node->nid;
  11.  
  12. //Disable field
  13. $form['field_ref_name'][LANGUAGE_NONE]['#attributes']['disabled'] = TRUE;
  14. }

Comments

phponwebsites's picture

Ok fine. Then how can set default autocomplete value wihtout nid?