Building a Lunar Calendar with Drupal’s CCK Computed Field

Jess Walls | 06-07-09

Building a Lunar Calendar with Drupal's CCK Computed Field

You can get a lot of mileage out of a Drupal site with the knockout combination of Views and CCK. Both of these mainstay projects have many contributed Modules to extend their power. In this post we are going to look at CCK’s Computed Field module, and how it can be used to automatically add contextual information to a content type on a per node basis.

To get started here’s what we’ll need:

  1. CCK
  2. Computed Field
  3. Views
  4. Calendar
  5. Date

Assuming all the above Modules have been installed and configured, we can focus on the details of setting up our computed fields. For this example, we’re going to add 3 new fields to Drupal’s Blog content type. Navigating through the admin section to admin/content/node-type/blog/fields, we’ll add the fields shown below:

fields

We’ll add a date field to our Blog content type with a pop-up widget, plus two computed fields: one to display the phase of the moon associated with date as a string, and another to display the phase of the moon as an image.

On the configuration screen for our first computed field field_moon_phase, we’ll see, in addition to the standard cck widget options, 3 additional configuration options—a text area for computed code, a text area for display format, and a group of options for database settings.

The text area for computed code is where the majority of the magic is going to happen. This is where we will enter our our custom php to parse the node’s CCK date field to compute the phase of the moon for the date selected by the user, and then display that information on the node both as a string (e.g. whether it is Full Moon, or a Crescent Moon) along with a graphical representation of the moon phase.

So, what are we allowed access to in the computed code text area? Well here’s the description from the Computed Field project page:

Computed Field is a very powerful CCK field module that lets you add a custom “computed fields” to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it.

Below the text area for the computed code there is also a helpful description of the variables that are immediately available to us:

&$node, $field, &$field_node
While the easy thing to do is to enter our php code directly into the text area provided for us, it will be easier to maintain in the long run if we place our custom code into a module. For this example I’ve added two functions to a custom module,
moonphases_phase_string()
and
moonphase_image()
, which I will reference from the text areas provided. To get started, we will access the date field value we added earlier field_moon_date, and pass the value to
moonphases_phase_string()
. This function will parse the date value to determine what the phase of the moon is on the given date, and then return a string, which we will assign to our computed field variable
$node_field[0]['value']

computed code

Now that we have assigned our computed value to the node, we need to configure how it will be displayed and stored. Since we will just be using a string value in this field, we will leave the default display settings as they are, but we are going to select the option to store the value in the database as a varchar so we can reference it from our View’s Calendar later.

display format database

We will configure the

field_blog_moon
computed field in a similar manner, with the same display and database settings, this time calling our custom code with the value from our previously configured
field_blog_moon_phase
field. This function will return an html img tag string with the graphical display of the moon phase. (It is worth noting at this point the order of the fields is important, if we want to use a field previously defined in this node, then that field should be set with a ‘lighter’ weight.)

computed code

To recap, we now have three additional fields defined for our Blog content type—a date field and two computed fields.

At this point, we can test out our moon display by creating a new Blog entry, choosing a date, and publishing the node.

blog view

Since we elected to store our computed fields in the database, we have access to our fields through the Views’ UI. To pull our Blog posts into the Calendar view (with the accompanying moon data), we will edit the Calendar view to add a new Page display.
We set this view to use the Calendar style, choose the field

field_moon_date
as our argument, and select three fields to display on the Calendar: the node title and the two computed fields.

view

Navigating to the path configured for this page view we have the finished result:

calendar view

| More

Comments

Name
Comment
Attila Hooper

Very cool Jess. Just starting out with cck and calendars and this is the kind of advanced stuff that helps master the fundamentals.

Cheers !

Jim

Great post, nice image.