For the classroom module; I'm trying to add token support so I may use the certain values from a "course" node in various ways.
Since it doesn't have Token support built-in, I'm trying to add it myself (and later submit a patch).
For whatever reason, I cannot seem to get it to recognize the "course" token values.
I can create the list, but they never translate into their actual values.
IMPORTANT NOTE: "course", "assignment", and "resource" types are NOT node-types in of themselves. They are pseudo-types that can be assigned to node-types already defined in your Drupal installation. (This may be what's causing my trouble, but any help is greatly appreciated).
Here's what I've created thus far:
In classroom.module
This is the general module that defines the "course", "assignment", and "resource" types.
<?php
...
function classroom_token_values($type, &$object = NULL, $options = array()) {
$tokens = array();
switch (drupal_strtolower($type)) {
default:
return classroom_course_token_values('all', $object, $options);
}
return $tokens;
}
function classroom_token_list($type = 'all') {
$tokens = array();
switch (drupal_strtolower($type)) {
default:
return classroom_course_token_list();
}
return $tokens;
}
...
?>
In classroom.courses.inc
This file provides specific support for "course" nodes.
<?php
...
function classroom_course_token_values($type, &$object = NULL, $options = array()) {
$tokens = array();
switch (drupal_strtolower($type)) {
case 'course':
case 'node':
$tokens['classroom_course']['continuous-ed'] = $object->classroom_course['unlimitedend'] ? t('Continuous') : t('No');
$tokens['classroom_course']['open-enrollment'] = $object->classroom_course['open_end_inscription'] ? t('Open Enrollment') : t('No');
$tokens['classroom_course']['use-grade-pct'] = $object->classroom_course['use_grade_percentages'] ? t('Use grade percentages') : t('No');
$tokens['classroom_course'] += _classroom_course_token_values_dates('course-start-date', (int)$object->classroom_course['start_t']);
$tokens['classroom_course'] += _classroom_course_token_values_dates('course-end-date', (int)$object->classroom_course['end_t']);
$tokens['classroom_course'] += _classroom_course_token_values_dates('registration-start-date', (int)$object->classroom_course['registration_start']);
$tokens['classroom_course'] += _classroom_course_token_values_dates('registration-end-date', (int)$object->classroom_course['registration_end']);
break;
case 'all':
return classroom_course_token_values('course', $object, $options);
break;
default:
return classroom_course_token_values('all', $object, $options);
}
return $tokens;
}
function _classroom_course_token_values_dates($fieldname, $objectvalue) {
$tokens = array();
$type = "custom";
$tokens[$fieldname.'-yy'] = format_date($objectvalue, $type, 'y');
$tokens[$fieldname.'-yyyy'] = format_date($objectvalue, $type, 'Y');
$tokens[$fieldname.'-month'] = format_date($objectvalue, $type, 'F');
$tokens[$fieldname.'-mon'] = format_date($objectvalue, $type, 'M');
$tokens[$fieldname.'-mm'] = format_date($objectvalue, $type, 'm');
$tokens[$fieldname.'-m'] = format_date($objectvalue, $type, 'n');
$tokens[$fieldname.'-day'] = format_date($objectvalue, $type, 'l');
$tokens[$fieldname.'-ddd'] = format_date($objectvalue, $type, 'D');
$tokens[$fieldname.'-dd'] = format_date($objectvalue, $type, 'd');
$tokens[$fieldname.'-d'] = format_date($objectvalue, $type, 'j');
$tokens[$fieldname.'-date'] = format_date($objectvalue, $type, 'c');
$tokens[$fieldname.'-value'] = $objectvalue;
return $tokens;
}
function classroom_course_token_list($type = 'all') {
$tokens = array();
switch (drupal_strtolower($type)) {
case 'course':
case 'node':
$tokens['classroom_course']['use-grade-pct'] = t("Whether or not the course uses grade percentages.");
$tokens['classroom_course']['open-enrollment'] = t("Whether or not the course has open enrollment.");
$tokens['classroom_course']['continuous-ed'] = t("Whether or not the course has no end date.");
$tokens['classroom_course'] += _classroom_course_token_list_dates("course-start-date", "Course start date.");
$tokens['classroom_course'] += _classroom_course_token_list_dates("course-end-date", "Course end date.");
$tokens['classroom_course'] += _classroom_course_token_list_dates("registration-start-date", "Course registration start date.");
$tokens['classroom_course'] += _classroom_course_token_list_dates("registration-end-date", "Course registration end date.");
break;
case 'all':
return classroom_course_token_list('course');
default:
return classroom_course_token_list('all');
}
return $tokens;
}
function _classroom_course_token_list_dates($fieldname, $value) {
$tokens = array();
$tokens[$fieldname.'-yy'] = t(sprintf("%s date year, two digits", $value));
$tokens[$fieldname.'-yyyy'] = t(sprintf("%s date year, four digits", $value));
$tokens[$fieldname.'-month'] = t(sprintf("%s date month, full word", $value));
$tokens[$fieldname.'-mon'] = t(sprintf("%s date month, abbreviated", $value));
$tokens[$fieldname.'-mm'] = t(sprintf("%s date month, two digits (zero padded)", $value));
$tokens[$fieldname.'-m'] = t(sprintf("%s date month, number, no zero padding", $value));
$tokens[$fieldname.'-day'] = t(sprintf("%s date day, full word", $value));
$tokens[$fieldname.'-ddd'] = t(sprintf("%s date day, abbreviated", $value));
$tokens[$fieldname.'-dd'] = t(sprintf("%s date day, two digits (zero padded)", $value));
$tokens[$fieldname.'-d'] = t(sprintf("%s date day, number, no zero padding", $value));
$tokens[$fieldname.'-date'] = t(sprintf("%s ISO date (YYYY-MM-DD)", $value));
$tokens[$fieldname.'-value'] = t(sprintf("%s raw value", $value));
return $tokens;
}
...
?>
Comments
Comment #1
wjaspers commentedDiscovered the Token list can place the fields into a sub-array; whereas the values array cannot. Rather confusing; but, resolved.
Comment #2
dave reidSome comments on the code:
Please, please, please just pass on the $type variable in both cases. I don't even see why you need the two separate functions. Just put your token code into classroom_token_list() and classroom_token_values(). Please see other modules's token implementations (or the core implementations included in the Token module) for best practices.
Also use the functions provided by the token module for you. Replace _classroom_course_token_list_dates() with token_get_date_token_info() and _classroom_course_token_values_dates() with token_get_date_token_values().
Comment #3
wjaspers commentedI didn't realize those functions were available. A lot of what I've learned has been adapted from other modules (my work environment blocks wayyy too many websites).
The reason I created a broader date function was that Classroom contains other content-types which use date-related data. I figured by setting this on a case-by-case basis I could loosen the functions a little.
Thanks for the info!
Comment #4
wjaspers commented