original songs

I wanted to use taxonomy terms as triggers to perform certain actions with the Drupal Rules module (There really should be a "Drupal Rules!" module), but there was no preset condition for this. No problem, it's really easy to add new conditions and actions (a post about creating an action is forthcoming).     

To do so, we need to add 3 functions:

  1. Add a hook_rules_condition_info() hook in our module
  2. Add a function for the condition
  3. Add a function for the condition form

Here's the code:

/**
 * Implementation of hook_rules_condition_info()
 */
function custom_misc_rules_condition_info() {
  $defaults = array(
    'arguments' => array(
      'node' => array('type' => 'node', 'label' => t('Content')),
    ),
    'module' => 'Taxonomy',
  );
  $items['rules_condition_content_has_term'] = $defaults + array(
    'label' => t('Content has term'),
  );
  return $items;
}


/**
 * Condition: Check if the node is published
 */
function rules_condition_content_has_term(&$node, $settings) {
  if (!is_numeric($settings['term'])) {
    $settings['term'] = db_result(db_query("SELECT tid FROM {term_data} WHERE name = '%s'", $settings['term']));
  }
  if ($row = db_fetch_array(db_query("SELECT * FROM {term_node} WHERE nid = '%d' AND tid = '%d'", $node->nid, $settings['term']))) {
    return true;
  }
  return false;
}


/**
 * Check for a term configuration form.
 */
function rules_condition_content_has_term_form($settings, &$form, $form_state) {
  $form['settings']['term'] = array(
    '#type' => 'textfield',
    '#title' => t('Term ID or text of term'),
    '#default_value' => isset($settings['term']) ? $settings['term'] : '',
    '#description' => t("Provide a number for a term ID (tid) or a string for matching text."),
    '#required' => TRUE,
  );
}


Great article on the pros and cons of using screenshots in documentation

Syndicate content