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'] = '<a href="#">Edit</a>';

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'] = '<a href="#">Edit</a>';

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 & 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('<a class="view" href="node/' + lesson_nid + '">View</a>');
  $(this).append('<a class="edit" href="node/' + lesson_nid + '/edit">Edit</a>');

1 Answer 1


This'll do the trick (I've tested it this time ;) ):

foreach($form['field_lessons'][LANGUAGE_NONE] as $key => $lesson) {
  // If we have a default value for the nid, we have a populated node reference
  if (isset($lesson['nid']) && is_array($lesson['nid']) && $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;
  • Soooooo close. This: $link = l('Edit', "node/$lesson[nid]/edit"); makes a link like this /node/Array/edit though. And unfortunately...the link breaks the drag & drop rearrange ability :s Commented May 31, 2012 at 21:57
  • Sorry about that I somehow forgot it was a form! I've updated the answer, it's tested so should work this time
    – Clive
    Commented May 31, 2012 at 22:15
  • Works, but I'm getting this error now on this line if (isset($lesson['nid']['#default_value']) && $lesson['nid']['#default_value'])... Commented May 31, 2012 at 23:18
  • What error are you getting?
    – Clive
    Commented May 31, 2012 at 23:19
  • Notice: Uninitialized string offset: 0 Commented May 31, 2012 at 23:20

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.