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.

Sample Scenario

basic layoutAssume 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:

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".

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
I use a simple HTML form to pass arguments to my view. On this form I have a select box with age ranges: 15-25, 25-35 etc... The action property of this form is an URL of the page where my view is placed (the form and the view can be on the same page). I use post method for passing values. On my view edit page (something like /admin/build/views/allpeople_view/edit) there is collapsing fieldset called 'Arguments', and inside this setfield there is a text area called 'Argument Handling Code'. Here I use a PHP coding to do almost whatever I want with this view. I use $age_range = $_POST['user_age_range'] to get an age range, then I use explode() function to get $from and $to range values. And here is an important part that dinamically creates a filter for my view: $pos = count($view->filter); // gets current filter index if ($age_range ) { $view->filter[] = array( 'vid' => $view->vid, 'tablename' => '', 'field' => 'node_data_field_user_age.field_user_age_value_default', 'value' => $from, 'operator' => '>=', 'options' => '', 'position' => $pos++, 'id' => 'node_data_field_user_age.field_user_age_value_default', );
Hi there, I am trying to find a solution to something that sounds similar to the project just discribed. I am working on a site, where each page is an office; (e.g. london paris etc) every office page is built on panels; I would like to have now a block at the top of the office nodes, where based on a tag (i.e. office page is an office content type, and the tag required is the name of the office, also used for cumulus tag cloud), it will show relevant videos ; e.g. I am in the london page and in the block I will see all videos with the london tag on them; user may be able either to add videos in the particular office page, of in a generic "video upload" page and then the system will do the various associations; Do you have any idea on how I could accomplish this? Regards
Which this? Do you want to know how to make a list of videos with the same tag as the office node appear in a block in the panel? Do you want to know how to make the system automatically assign a tag? If I assume the first question, I have kinda done this already so I posted a blog on the topic http://idcminnovations.com/article/drupal-views-using-term-arguments - see if it does what you need. As for the second question regarding having Drupal automatically assign a term, I have never thought about it. I did a quick search and found this http://drupal.org/node/595336 - is this you? Does it give you any ideas? Sorry I cant help further.
I am trying to add 3 cck fields such that the third field be the sum for first two fields. All fields are integer, is it possible to autofill the third field based on the values of first two fields. Thanks Deepak
there are two parts, what to do and where to do it. part of me thinks you might need a small custom module to write to the database and store the added value (if that is what you want). i have never tried this with Drupal but if I were thinking like the old days, I would use sql to compute the value and php to write the value to the field in the database. unfortunately i dont know what the syntax would be because its been way too long since i manually did this process and secondly, i used to do it with asp and cold fusion (yes a long time ago). So, if you cant get computed field to do it, you might want a module to do it. sorry i cant be more help
I have tried saving and re-editing the instructor node over and over again. i definitely have a few computed fields on that instructor node type and they never show up. help!
without more information, i can't help. if there is a something wrong with the right up above, i will help fix it but i need to know more.
I'm new to the Drupal scene and always appreciate the work that developers like you do. I am not an experienced coder, so trying to do things like this would be impossible without this kind of help. It took me quite awhile to find this post; thankfully I stumbled across it when I did. Otherwise I'd be left struggling with views for another week. Thanks.
glad you liked it. I am using it on a project as well. Thanks to Jess for sharing her knowledge.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Insert Flickr images: [flickr-photo:id=230452326,size=s] or [flickr-photoset:id=72157594262419167,size=m].
  • Twitter-style @usersnames are linked to their Twitter account pages.
  • You may insert videos with [video:URL]

More information about formatting options