I needed a slight extension of the Context module. Treehouse Agency wrote up a great explanation of how they extended context_condition to create two new conditions. I’m doing pretty much the same thing, but my condition is slightly different so I thought it might benefit somewhat to see another slant on this.
My site has a vocabulary for event categories. The vocabulary has a hierarchy
which is one deep. For one of the parents, “Kids Programs”, I needed to be able to establish a different look & feel on each node view. I set this up via context, using node type event and path “kids/events/*” vs path “events/*” to establish the difference. That worked great until I got to the comment pages. I wanted a user who was commenting on an event to maintain the same context, even while on “comment/reply/nid”. So, I created a condition to check if a node was an event and if it had a category which was within Kids Programs. Then, I had this condition be checked via both hook_nodeapi and hook_form_comment_form_alter, which allow the context be set for all flavors of node viewing.
Here’s where I use the hook hook_context_plugins
/**
* Implementation of hook_context_plugins().
*/
function events_context_conditions_context_plugins() {
$plugins = array();
$plugins['events_context_conditions_context_condition_kids_event'] = array(
'handler' => array(
'path' => drupal_get_path('module', 'events_context_conditions'),
'file' => 'events_context_conditions_context_condition_kids_event.inc',
'class' => 'events_context_conditions_context_condition_kids_event',
'parent' => 'context_condition',
),
);
return $plugins;
}
Next you have to tell Context about your new condition, using hook_context_registry:
/**
* Implementation of hook_context_registry().
*/
function events_context_conditions_context_registry() {
return array(
'conditions' => array(
'kids_event' => array(
'title' => t('Custom Condition: Event node page'),
'plugin' => 'events_context_conditions_context_condition_kids_event',
),
),
);
}
Now we write our new class, events_context_conditions_context_condition_kids_event, extending condition_context. The class has two values, 1 means the node is tagged with at least one kids program, 2 means it isn’t.
class events_context_conditions_context_condition_kids_event extends context_condition {
function condition_values() {
$values = array(
1 => t('Event of type node in a Kids Program category'),
2 => t('Event of type node not in any Kids Program category'));
return $values;
}
function execute($node) {
//If node is an event and has any kids program terms, then meet value 1
//If node is an event and has no kids program terms, then meet value 2
//If the node is not an event, then you are done
if($node->type == 'event'){
//Assume there are no kids program terms, then set this to 1 if you find one.
$kids = 2;
//First gather all the terms in the event category vocabulary
$cats = taxonomy_node_get_terms_by_vocabulary($node, 4);
foreach ($cats as $cat) {
//This is the Kids Program term itself
if($cat->tid == '19') {
$kids = 1;
} else {
//Get all the parents of this term
$parents = taxonomy_get_parents($cat->tid);
if($parents){
//Check each parent to see if it is the Kids Program category
foreach($parents as $parent) {
if ($parent->tid == '19'){
$kids = 1;
}
}
}
}
}
foreach ($this->get_contexts($kids) as $context) {
$this->condition_met($context,$kids);
}
}
}
}
The only thing left is to make sure this condition gets checked whenever we might be displaying a node:
/**
* Implementation of hook_nodeapi().
*/
function events_context_conditions_nodeapi(&$node, $op, $teaser, $page) {
if ($op == 'view' && $page) {
if (module_exists('taxonomy')) {
if ($plugin = context_get_plugin('condition', 'kids_event')) {
$plugin->execute($node);
}
}
}
}
/**
* Implementation of hook_form_alter() for comment_form.
*/
function events_context_conditions_form_comment_form_alter(&$form, $form_state) {
if ($nid = $form['nid']['#value']) {
if (module_exists('taxonomy')) {
if ($plugin = context_get_plugin('condition', 'kids_event')) {
$plugin->execute(node_load($nid));
}
}
}
}
Posted in: Drupal 1 Comment »