Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This is a collection of sample code snippets that can be used in CCK fields enabled by the Computed Field module.
If you have a snippet that performs some new function, please add a new page to this section. If you have a variation on an already documented function (e.g. a different method to achieve broadly the same effect) please add a comment to the relevant page.
Comments
Our Example - This is code to fill node reference fields
We had a situation where we needed to fill three different reference node CCK fields. The city and county fields were being filled by geo location from openlayer module CCK wkt field type. The nearest city is a manual entry used by the editor of the node. Our situation is that the nearest city and the geo location's city could be the same or they could be different.
We wanted, if they were the same, that the reference would not be duplicated. So if they are the same we just set the node reference for one CCK field to NULL. Of course it might be obvious that we used three field entries that were not node references. The code below uses those three fields and some data base query to locate and provide the values in the reference fields.
We put the code into the Computed Code: field. Nothing is stored from this field directly to the database and the Display Format: We determined that the fields update when the record is merely viewed but the values were not saved. The event of editing the node also did not update the database and did not show the values in the node references. The final event of saving the node did update the database and made the entries more permanent.
Hope that this helps those of you who might need some additional ideas in this area. Thanks to your help - b.coleman (Drupal user name)
Chris Pergantis
Founder/Chairman/President
Accelerated Design Inc
duration from a date field with to and from values
this patch is great for entering the to and from and only having to enter a time to specify duration, but doesn't really address how to display that duration (that I can see). change field_your_datetime of course to your actual date field's name.
in Drupal 7
I wrote a super easy guide for beginners to computed_field.module.
Calculate Field Values Using Computed Field in Drupal 7
Here's the code:
Compute amounts from a field in Field collection
Since field collection values aren't stored in the node, only the reference is there, the way to get the values is a bit different than getting them from fields that aren't field collection fields.
In my case I have a field_amount as a field collection field, with unlimited number of values, and wanted to sum them all up:
There may be a way to do it through field collection API instead directly from db.
to avoid unexpected T-string errors
If you need to use classes in computed ckk eg:
$display_output = "<p class="mytest">test</p>";
You could get an unexpected T-string error because of the quote marks around the class. To avoid this use single quotes eg
$display_output = "<p class='mytest'>test</p>";