Community Documentation

HOW-TO: Use a view as a source for a node reference field

Last updated October 29, 2009. Created by webchick on October 29, 2009.
Log in to edit this page.

Here are instructions for using a View to dynamically populate the list of options in a node/user reference field. The most common use cases for this feature are:

  1. When just the node title / user name by itself doesn't provide enough context to know what you're referencing (for example, you want to see options like "John Smith" instead of "stinky500")
  2. If you need to filter the list of options in ways that aren't provided by the field configuration options. For example, selecting from a list of nodes that are tagged with a particular set of keywords.

Here is a simple example: A node reference field showing only nodes that are of type "Story" and have their "Sticky at the top of lists" box checked.

  1. Go to Administer >> Site building >> Views.
  2. Add a new View with the name 'nodereference_featured_nodes' (or whatever you'd like). The other options can be left at defaults. Click "Next."
  3. The next screen gives you the Views interface. Next to "Fields" click the "+" sign. Here, you are selecting what values to display in the node reference field's drop-down list/autocomplete field. Change "Groups" to "Node" to filter the list down, then select "Node: Title", then click "Add."
  4. After adding the field, you'll be taken to its configuration form. Remove the text from the "Label" field and click "Update." Otherwise, your options will all be like "Title: Foobar", "Title: Blarbaz", etc.
  5. After clicking update, the "Live preview" section will show 10 random node titles of every type. We need to limit down the list, using filters. (tip: You can actually check all three of these in one go, which is what you normally do, but it's easier to write out directions if we take it one field at a time. ;))
  6. To limit this list to only show "Story" nodes, click "+" next to the "Filters" section. As before, limit "Groups" to "Node" and this time select "Node: Type" as the thing to filter by. Click "Add."
  7. At the configuration screen, check off "Story" as the type of nodes to display. Click "Update."
  8. Another thing you should almost always do when creating a list of nodes is make sure the "Published" flag is set to TRUE. Otherwise, you'll end up with draft nodes in the list, which is usually not what you want. Click "+" next to "Filters" again, and select "Node: Published." Click "Add," choose "Yes" at the configuration screen, and click "Update."
  9. Finally, we also want to limit by nodes that have their "Promoted to front page" flag checked. Once again, click "+" next to "Filters" and check "Node: Sticky at the top of lists" and then "Yes" its the configuration page.
  10. Before we leave, we should put these in some sort of an order; how about alphabetically? Click "+" next to "Sort criteria" and select "Node: Title." You can leave the configuration options at the default, which will sort with A first, and Z last.
  11. Now, after you click "Save" on the view, you can go back to your node reference field and in its configuration settings. Instead of checking off the boxes for type, expand the "Advanced - Nodes that can be referenced (View)" and choose "nodereference_featured_nodes" from the list.

Now, when you go to create your content, you'll only see sticky story posts that are published in your list of choices.

Views is a very powerful tool, and this is just a simple example to get you started, but you can create just about any kind of filtered list you can imagine this way.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x
Audience
Site administrators
Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here