Can I add a link to the node reference when using the autocomplete widget? I love the autocomplete widget for the References module in Drupal 7, but I find it odd that it doesn't include either a link to the referenced node (view or edit)?

Is there some way in hook_form_alter that I can add this somehow?



I can do this:

$form['field_lessons']['und'][0]['#suffix'] = '&lt;a href="#"&gt;Edit&lt;/a&gt;';


And the autocomplete field has an edit link underneath of it, but this doesn't work:

foreach($form['field_lessons']['und'] AS $lesson) {
  $lesson['#suffix'] = '&lt;a href="#"&gt;Edit&lt;/a&gt;';
}


And also loops 10+ times even though I only have 1 node reference currently :s



I don't necessarily like it but I can do it in Javascript like this and the Drag &amp; Drop still works:

$('#edit-field-lessons .form-item').each(function(i) {
  var lesson_nid = $(this).find('input[type="text"]').val();
  lesson_nid = lesson_nid.substring(lesson_nid.indexOf('[nid:')+5, lesson_nid.indexOf(']'));

  $(this).append('&lt;a class="view" href="node/' + lesson_nid + '"&gt;View&lt;/a&gt;');
  $(this).append('&lt;a class="edit" href="node/' + lesson_nid + '/edit"&gt;Edit&lt;/a&gt;');
});
This'll do the trick (I've tested it this time ;) ):

foreach($form['field_lessons'][LANGUAGE_NONE] as $key =&gt; $lesson) {
  // If we have a default value for the nid, we have a populated node reference
  if (isset($lesson['nid']) &amp;&amp; is_array($lesson['nid']) &amp;&amp; $lesson['nid']['#default_value'])) {
    // Get the nid
    $nid = $lesson['nid']['#default_value'];

    // Create an edit link
    $link = l('Edit', "node/$nid/edit");

    // Add it to the element
    $form['field_lessons'][LANGUAGE_NONE][$key]['nid']['#suffix'] = $link;
  }
}