I’ve been using the great CCK module on a bunch of projects (including this blog, for my bikelog). It’s really cool in that it lets you construct custom content types within the Drupal admin interface, without having to touch any PHP code. It’s like having your own personal microformat manager – it could handle things like compound content on a web page (and generate the editing form so you just fill in the blanks – abstract, body, url for more info, email contact, etc…) and I think it could even handle something like a simplified LOM for a “learning object repository”.
One thing I wanted to do on my bikelog was to automatically calculate average speed. I have fields for “duration” and “distance” so entering a value for “average speed” is redundant. Better to just derive the value from the known values already entered.
Which is where the really cool Computed Field module comes in. It’s a contributed module for CCK, and lets you do all kinds of funky things by writing PHP code to perform operations to generate the value of a field. These operations can include calculations on the other fields, or even more complicated things like, I suppose, performing a lookup on wikipedia or something…
After adding the Computed Field module, I just edited my “bikeride” CCK content type to add a new “computed” field called “average speed”. It’s got a “computed code” value, which is executed whenever a value needs to be derived. I just used this:
$node_field[0]['value'] = ($node->field_distance[0]['value'] / $node->field_duration[0]['value']) * 60;
I gave it a data type of “float”, a length of “10,2” and a default value of 0. I told the module to store the value in the database, so I can use the computed field with Views.module as well (in this case, it performs the computation on edit, and stores the result in a field just as any other CCK field would do). I also flagged the field as being sortable, in case that helped Views.module at all.
The end result is a tiny bit of work entering a simple PHP calculation, and then the module takes over the monotonous and redundant calculation work. All I do is enter the base data, and it can derive the rest from that.
I’d imagine this type of derived field could be really useful in an event manager – looking up the number of registrations and updating an “available seats” field, for instance. Or, in a store or inventory system to calculate prices and stock counts. It’s a really cool addition, and although the setup interface (manually entering PHP code) isn’t something you’d unleash on a noob, it’s simple enough to set up and forget about.
Wow – good to see you use CCK already… I want to use that ASAP / as soon as I migrated all my wordpress and MT installations…
BTW – do you use contemplate also??
see http://weblog.cemper.com/a/200605/30-flexible-data-modelling-with-cck-contemplate-and-drupal-part-2-in-great-reasons-for-drupal.php
How can we send a attached word document with a mail, which should be automatically generated at a particular time.
Note; User will not run the script. The mail should reach at certain to the receiptent at particular time.
And the mail is already predefined.
sunny – I have no idea what you’re asking.
I’m using contemplate on our Teaching & Learning Centre department website, and will be adding it here. I only started using Contemplate on Monday, but am really liking it. The CCK node template is coded in a .tpl.php file here, which is kind of nasty to edit remotely.
is there anyway to put some logic to check on the computed field? For example, in my CCK form, I ask the user to input a date in the future, and the computed field will calculate the difference (in days) between current date and the future date entered by the user. If the computed field result is more than, say 60 days, then there should be an error saying that the date can not be more than 2 months from today and prevent the submission. Possible with CCK+Computed field?
http://www.darcynorman.net/bikelogs is dead.
@eep2: yeah. and it’ll likely stay that way – I switched from Drupal to WordPress several months ago, so had to dump that functionality because WordPress can’t do it (in the same way).
Why did you switch to WordPress?
@solson I switched because Drupal wasn’t the right tool for the job. I just need blogging, and while Drupal can do that, I was spending a fair amount of time fine tuning Drupal to make it behave like WordPress. Decided to just use WordPress for straight blogging. I still use Drupal for dozens of websites – it’s by far the best tool for that job. But for straight blogging, WordPress is the right tool…
dnorman… do you know if CCK module exists for WordPress?
The closest thing I’ve seen to CCK for WordPress is Flutter – I wrote a post about it recently.
Hi, my field is just not computing… I’ve tried running the cron, and using different settings configurations. I’m not sure what the problem is. Online resources are quite sparse, but your comment section seems helpful..
I have two integer fields, and i want to multiply and add them to get CO2 saved:
$node_field[0][‘value’] = $node->field_natgas_saved[0][‘value’] * 123.248 + $node->field_kwh_saved[0][‘value’] * 1.038;
I have it to show as a float 10,2 with not null & sortable both checked.
But the value only shows the default 0. Any ideas?
Ok, update.
I found that if I go into the node, click edit, and save, it updates the field with the calculated value. I have like 400 nodes… is there an automatic way to do this? And also, if I make any change to the formula, none of the fields will automatically update? Is that correct?