Drupal Pages and Views: CCK View Reference Field

In this short article, I am going to look at using the view reference module as a means of adding a view to a page. With this method, you can create a content type that has an option to display content from a view. You can use a view as is or pass it an argument. The sections below present a sample scenario, steps for implementing view reference, and a layout tip.
Sample Scenario
You have a web site where you have multiple content authors contributing their ideas, information, stories, and so on. The content authors can post up to five types of content (designated, in the case, by a taxonomy vocabulary) each associated with 10 topics (also designated by a taxonomy vocabulary).
You want to put the content posts into context by writing a story or scenario that explains why the topic is valid and how it relates to the other topics on the site. You want your text to be followed by a list of related content posts that are tagged with the topic you are writing about.
This process can be accomplished several ways. For one, you could simply create a page view and use the header area of the page view to show story or scenario. In this case, let’s assume two things:

  • You want the text of your story or scenario to look like your other nodes and you don’t feel like tweaking your theme in the event the View header ends up looking different than your other pages.
  • You aren't the only one creating context pages. You think there is going to be others wanting to do the same. You need a method that allows the content authors access to the views without having to open the Views UI.

In order to use this method, you will need CCK, the View Reference Module, and Views installed. You will also need to know how to create content types with cck fields and views. If you need help with cck or views, check out the resources at the following links:

  • http://learnbythedrop.com/category/cck
  • http://learnbythedrop.com/category/views

Below is a summary of the steps you need to perform in order to support the scenario above.

  1. Create the view(s) you need for your stories or scenarios.
    1. You can create 10 separate views, one for each topic
    2. OR you can create one view with a topic argument
  2. Create a content type (use an existing content type). In our sample scenario, maybe call it context page.
    1. Add a view reference field.
    2. When choosing the "form element to edit the data," you can use a select list (good for users who don't know the name of the views they can choose) or autocomplete text field.
    3. When configuring the view reference field, check the views that you want available in the content type. There will be a list available to choose from.
  3. Create your node with the new content type. By default, you will see the text you entered in the body followed by the results of the view.

Layout Tip
If you want to control the layout of the fields in your node, check out the composite module.

"Composite Layout allows your nodes to be displayed in complex layouts. Currently, two and three column layouts are provided. You can also add other nodes, blocks, and even CCK fields to your node's layout. The content area of your node is divided into zones and you decide what should appear in those zones. Zones are essentially the same as Drupal blocks, but they apply to nodes rather than the entire site."

You have some things to think about. For example, do you want to use arguments in your views? Also, don't forget your permissions. Who will be able to use this new content type? Remember to think through the process of how this strategy will be used and by whom. Sometimes the best solution for you is not the best solution for your users.