An Automatic Nodetitles PHP Snippet to automatically create titles based on taxonomy terms and date fields

So the political situation is heating up in Argentina just like Paris, France, and class struggle organizations and political parties are taking part in the current electoral process in order to focus on the struggles and issues facing the challenge of making the bosses pay for the crisis instead of the working class. So, helping out here, on a certain website I am using a content type to publicize events. The content type happens to be called "event" and happens to be used by the Event module (a long standing and ancient module created and maintained by many Drupal masters). Most of the events concern the visits of an electoral candidate to a specific physical location. So the best thing would be to have the title reflect that.

So the socially conscious webmaster justifiably got sick and tired of entering the candidate and location into taxonomy term fields, entering the date into the start date field, and then repeating all of that in the title.

So the political situation is heating up in Argentina just like Paris, France, and class struggle organizations and political parties are taking part in the current electoral process in order to focus on the struggles and issues facing the challenge of making the bosses pay for the crisis instead of the working class. So, helping out here, on a certain website I am using a content type to publicize events. The content type happens to be called “event” and happens to be used by the Event module (a long standing and ancient module created and maintained by many Drupal masters). Most of the events concern the visits of an electoral candidate to a specific physical location. So the best thing would be to have the title reflect that.

So the socially conscious webmaster justifiably got sick and tired of entering the candidate and location into taxonomy term fields, entering the date into the start date field, and then repeating all of that in the title.

So I figured it would be a relatively simple thing to use the fabulous Automatic Nodetitles module (a power salute to fago) to create the titles for me based on the data already entered into the fields making up the event. This is a technique that can be used with any content type, of course.

Since the fabulous Automatic Nodetitles module depends on the even more fabulous “the little API that could” (the Token module — a power salute to eaton) my initial attempt (with the candidate as author and the location as the only taxonomy applied to the content type) using handy tokens suggested by the Automatic Nodetitle module itself (once you install it, it can be configured at the top of the content type configuration edit form, and it suggests a wealth of  handy tokens you can use with that content type):

[author-name] en [term-raw] el [eventdd]-[eventmm] 

That worked, producing titles like “Candidate Jones at the Factory occupied by the workers on 1-5” (adjust date format to suit your locale).

But of course, what happens if you want titles like: “Candidate Jones, Candidate Smith at Pan American Highway picket blockade, Buenos Aires on 1-5”? Yes, more than one candidate, more than one location.

So I spent some time actually trying to get this done by creating a candidate taxonomy together with the existing location taxonomy, applying both to the event content type, and using an adroit combination of tokens.

However, after some time I still could not get the modules to distinguish between terms in different vocabularies.

So I decided to take advantage of the option to return a string for the title using PHP, taking into account the help text indicating that the $node object is available for your coding pleasure. I mean, if we’re blocking highways so that the bosses pay for the crisis instead of the workers, what’s a bit of PHP?

My first brave attempt went as follows (practice your Spanish a bit: “lugar::location, fecha::date):

<?php
$terms = $node->taxonomy;
$candidato_term = array_shift($terms);
$candidato = $candidato_term->name;
$lugar_term = array_shift($terms);
$lugar = $lugar_term->name;
$fecha = $node->event['start_exploded']['day'] . '-' . $node->event['start_exploded']['month'];
return $candidato . ' en ' . $lugar . ' el ' . $fecha;
?>

Well, the “fecha” stuff worked fine, but the taxonomy is different in the node than what you would expect from looking at what the devel module might show you for a loaded node. So I used the title as a debugging device with the following snippet:

<?php
$candidato = print_r($node->taxonomy, true);
$lugar_term = array_shift($terms);
$lugar = $lugar_term->name;
$fecha = $node->event['start_exploded']['day'] . '-' . $node->event['start_exploded']['month'];
return $candidato . ' en ' . $lugar . ' el ' . $fecha;
?>

This “title as debugger” showed that taxonomy was actually a very handy array, like this:

Array( [tags] => Array ( [6] => Jones [5] => Ciudad de Buenos Aires ))
tags (Array)
6 (Array)
Jones
5 (Array)
Ciudad de Buenos Aires

So we need:

<?php
$candidato = $node->taxonomy['tags'][6]; // knowing the id of the vocabulary where we expect the candidate
$lugar = $node->taxonomy['tags'][5]; // knowing the id of the vocabulary where we expect the place
$fecha = $node->event['start_exploded']['day'] . '-' . $node->event['start_exploded']['month'];
return $candidato . ' en ' . $lugar . ' el ' . $fecha;
?>

Sprucing up a bit in case no lugar or candidato is selected:

<?php
$candidato = $node->taxonomy['tags'][6];
$lugar = $node->taxonomy['tags'][5];
$fecha = $node->event['start_exploded']['day'] . '-' . $node->event['start_exploded']['month'];
if ($candidato && $lugar) {
return $candidato . ' en ' . $lugar . ' el ' . $fecha;
}
else {
return $candidato . '-' . $lugar . '-' . $fecha;
}
?>

Of course, more sophisticated logic may be brought to bear, but this gave me great titles like:

“Freddy, Jack, Murray at Commuter Bridge Occupation, Entre Rios on 7-5”

“Ted, Harry at Subway Turnstyle Liberation, Travel for free to combat fare increases, Corrientes and 9 de Julio, Ciudad de Buenos Aires on 10-5”

Power!

Use this snippet at a factory occupation near you!

(I will update this post with a link to the actual site as soon as it is launched).