When using the amazing Node Reference URL Widget (http://drupal.org/project/nodereference_url), I've often wanted to display a more helpful/attractive display of the node that is being referenced, instead of the sometimes unhelpful node title that is displayed by default on the node creation form. For instance, if I am adding a Song node that references an Artist node, I would rather show an actual picture of the artist and their name, instead of just the title of the artist node. Here's one way that you can do this, by embedding a custom view in place of the default text:

  1. Create the view.
    • Items to display: 1
    • Add an argument of Node: NID.
      • Set Action to take if argument is not present: to "Display empty text".
  2. Add the fields that you would like to display.
  3. Finally, in your theme's template.php file, add the following code:
/**
* Implementation of hook_theme.
*
* Register custom theme functions.
*/
function YOURTHEMENAME_theme() {
  return array(
    // The form ID.
    'YOURCONTENTTYPE_node_form' => array(
      // Forms always take the form argument.
      'arguments' => array('form' => NULL),
    ),
    
  );
}

/**
* Theme override for node page form.
*
* The function is named themename_formid.
*/
function YOURTHEMENAME_YOURCONTENTTYPE_node_form($form) {
  // Replace the nodereference url widget field with a view to display the item that was referenced.
  if(!empty($form['YOURFIELDNAME'][0]['nid']['#value'])){
    $my_nodes = array($form['YOURFIELDNAME'][0]['nid']['#value']);
    $form['YOURFIELDNAME'][0]['nid']['#display_title'] = views_embed_view('YOURVIEWNAME', 'default', $my_nodes);
  }
  return drupal_render($form);
}

Comments

WorldFallz’s picture

Can also be done with the http://drupal.org/project/nodereference_views module.

cwebster’s picture

@WorldFallz - Thanks for the comment.

I may be wrong, but I don't think that module is able to modify the node form ("This module does nothing to node edit forms."). The method I was presenting actually changes the display of the Node Reference URL Widget before a node is saved, not the final output of the node once it has been saved. Does that make sense?

WorldFallz’s picture

i'm an idiot, lol-- you're absolutely right!

m_i_c_h_a_e_l’s picture

I think (I think!) this is exactly what I need.

I have a content type called "writing", which is a worksheet for students to submit work. And a second content type called "prompt" which contains instructions for the assignment. I have hundreds of prompts, and each is different.

"Prompt" is node referenced into "writing" using node reference url. The idea is that the student will view a "prompt" node, and then click on the link provided by node reference url, which then adds a new "writing" node.

The problem, as you described above, is that the body of the referenced node does not appear on the add/edit form. Only the title. It's really important for my students to be able to view the instructions contained in the referenced node while they are doing the actual writing. The referenced node should be visible on the add/edit form and also on the saved node.

I also tried attaching a view (in the node reference field configuration) but no joy.

Please tell me that you can help. It seems like editing the node creation form is the only way to get around this problem.

P.S. A second minor question: If you can cause a view to display on the node creation form, is it also possible to make the body of a referenced node appear? Instead of a view?

This is very important for me. Thank you for any advice you can give.

Anonymous’s picture

Works great. Just what I needed.