Drupal Pages and Views: CCK Computed Field

In this short article, I am going to share with you what Jess Snyder is doing with her sites by using the CCK computed field module. With this method, you can create a content type that is designed to display content from a view after the node has passed an argument to the view.
<!--break-->
Sample Scenario
Assume you have an educational site. On this site you have a content type for instructor and a content type for course. When you create an instructor node, you want the courses that instructor teaches listed below the instructor’s bio. You don’t want to have to remember which courses you just want the list to appear.
In order to make this happen, you need an instructor content type that has a view. The view will create a list of courses based on the instructor’s node ID. You also need a course content type that allows you to connect the instructor to the course. Assume that there is a one-to-one match between instructor and course.
This process can be accomplished several ways. For one, you could use the node relativity module (node_relativity) which allows you to create parent-child relationships between nodes. But this module limits what gets displayed to the node title. The method below enables you to control what you see in the view (one field or multiple fields)
Pre-requisites
In order to use this method, you will need CCK (enable the node reference option) and Views installed as well as the computed field module. 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

Steps
Below is a summary of the steps you need to perform. Details regarding the computed field configuration are provided below the steps.

  1. Create an instructor content type. At this time, just use the defaults settings.
  2. Create a course content type.
    1. Add a node reference field.
    2. When configuring the node reference field, select the instructor content type to be referenced.
    3. If you assume a one-to-one relationship between the instructor and the course, leave the number of values setting = 1.
  3. Create an instructor node.
  4. Create a course node and reference the instructor node.
  5. Create a node view.
    1. Filter the nodes to list all nodes with content type = course.
    2. Set the fields to node title (at the least) and check the option to “Link this field to its node.”
    3. Include an argument to limit list results to only those with instructor node ID. The NID is what gets passed by the computed field to the view.
    4. Note: you can call the default view in the computed field so you don’t have to create a block or page.
  6. Edit the instructor content type.
    1. Add the computed field.
    2. Set up the field computed code and display format. In this scenario, you would use:
      1. Computed code: $node_field[0]['value'] = $node->nid;
      2. Display format: $display = views_embed_view('nameofview', 'default', $node_field_item['value']);
    3. Set the database storage setting.
      1. Data type = int
      2. Date length = 11 (because you only need to store the NID, you don't need a long length. It just needs to hold the argument that will be passed to the view. )
    4. If you position this field below the node body, the view results will show below the body. If you want the view results to appear above the node body, re-order the content type fields.
    5. If you do not want the label of the computed field to appear when the page is viewed, change the display settings for the computed field.
  7. Go to the instructor node you created earlier. Click edit and save to get the new field to show up. View the instructor node and see that the course link appears as well.

Computed_field Information
When you add the computed_field to your content type, you are taken to the configuration screen. In the Global settings box you will see two forms: computed code and display format.
Computed Code
The form provides this statement:

The variables available to your code are: &$node, $field, and &$node_field. To set the value of the field, set $node_field[0]['value']. Here's a simple example which sets the computed field's value to the value of the sum of the number fields field_a and field_b: $node_field[0]['value'] = $node->field_a[0]['value'] + $node->field_b[0]['value'];

The code provided in the instructions above (and below) is consistent with these instructions.

$node_field[0]['value'] = $node->nid;

If you are new to coding, let’s look at each part of this statement.

  • $node_field[0]['value'] – this is a variable. The variable gets assigned a value that will be sent to the view.
  • [0] – set the first value of the array to zero
  • ['value'] - the value of the field

If you don’t want to use the NID, you could also assign the value of another field in the content. If, for example, you have a content type that that has a status field. An you want to show a view that shows all other nodes with the same status, you could assign the $node_field[0]['value'] variable to be the value of the status field. Your view would then be designed to show all nodes with the status value argument.
Display Format
The form provides this statement:

This code should assign a string to the $display variable, which will be printed as the value of the field. The stored value of the field is in $node_field_item['value']. Note: this code has no effect if you use the "Computed Value" formatter option.

The code provided in the instructions above was provided with the Views module. Jessica looked at the functions available in the views.module file.

$display = views_embed_view('nameofview', 'default', $node_field_item['value']);

If you are new to coding, let’s look at each part of this statement.

  • $display – this is a variable whose value gets deplayed by the computed_field module.
  • views_embed_view() – views function from views.module
  • nameofview – this is the $name of the view
  • default – this is the $display_id of the view
  • $node_field_item['value'] – this is an additional parameter. Notice that it is the computed code variable. The first thing that happens is, this variables gets assigned a value that, in this case, is an argument that the view needs. Then, the value of the variable is sent to the view.

Summary
As you can imagine, you can do a lot with the computed_field module. Jessica has found a convenient way to make a connection between between books and authors on http://www.thebookstudio.com".