"hidden" data type?

Events happening in the community are now at Drupal community events on www.drupal.org.
ryan_courtnage's picture

Hi,

I'm attempting to develop a simple module that triggers an event when the watchdog is written to. My module also makes some configurable conditions about the watchdog message type and severity.

My event triggers okay. The conditions also work, but am completely confused about the presentation layer for Conditions. When using condition arguments with 'type' => 'value', they present themselves in the "Arguments configuration" part of the condition creation page. According to the docs (assuming i'm reading them right), type=value should be hidden fields.

Here's my hook_rules_condition_info:

<?php
function watchdog_rules_rules_condition_info() {
  return array(
   
'watchdog_rules_severity_condition' => array(
     
'label' => t('Watchdog event severity is'),
     
'module' => 'Watchdog',
     
'arguments' => array(
       
'watchdog_type' => array('type' => 'value', 'label' => t('The type of watchdog event')),
       
'watchdog_severity' => array('type' => 'value', 'label' => t('The severity of the watchdog event')),
      ),
    ),
   
'watchdog_rules_type_condition' => array(
     
'label' => t('Watchdog event type is'),
     
'module' => 'Watchdog',
     
'arguments' => array(
       
'watchdog_type' => array('type' => 'value', 'label' => t('The type of watchdog event')),
       
'watchdog_severity' => array('type' => 'value', 'label' => t('The severity of the watchdog event')),
      ),
    )
  );
}
?>

I need to pass these 'watchdog_type' and 'watchdog_severity' arguments to my watchdog_rules_severity_condition() and watchdog_rules_type_condition() functions. And indeed they do get passed. However, they appear in useless dropdowns on my condition add/edit page.

I'm I doing something wrong?

Comments

you can just add 'hidden' =>

fago's picture

you can just add 'hidden' => TRUE for your arguments. However this way you could only manually configure your condition by configuring the rule by code.

How does your event look like?

Based on hook_watchdog() you could add a new rules data type for the given $log / $entry and make your conditions for it. We could also add tokens for these new data type. Then one could route some special watchdog messages to be sent by mail or even SMS (once there is such an action..). Nice.. :)

Just sad that once the syslog module is enabled, it's used automatically. Else we could provide an action for logging by syslog too.

Thanks for your response.

ryan_courtnage's picture

Thanks for your response. I'll try out 'hidden' => TRUE.

I originally attempted to add a new data type, but wasn't able to make sense of it. I figured I'd first try to just use condition forms for configuring the options.

Here's my .rules.inc code. Perhaps you can make some suggestions:

<?php
/**
* Implementation of hook_rules_event_info().
* @ingroup rules
*/
function watchdog_rules_rules_event_info() {
  return array(
   
'watchdog_event' => array(
     
'label' => t('A watchdog event is being written'),
     
'module' => 'Watchdog',
     
'arguments' => array(
       
'watchdog_type' => array('type' => 'value', 'label' => t('The type of watchdog event.')),
       
'watchdog_severity' => array('type' => 'value', 'label' => t('The severity of the watchdog event.')),
      ),
    ),
  );
}

/**
* Implementation of hook_rules_condition_info().
* @ingroup rules
*/
function watchdog_rules_rules_condition_info() {
  return array(
   
'watchdog_rules_severity_condition' => array(
     
'label' => t('Watchdog event severity is'),
     
'module' => 'Watchdog',
     
'arguments' => array(
       
'watchdog_type' => array('type' => 'value', 'label' => t('The type of watchdog event')),
       
'watchdog_severity' => array('type' => 'value', 'label' => t('The severity of the watchdog event')),
      ),
    ),
   
'watchdog_rules_type_condition' => array(
     
'label' => t('Watchdog event type is'),
     
'module' => 'Watchdog',
     
'arguments' => array(
       
'watchdog_type' => array('type' => 'value', 'label' => t('The type of watchdog event')),
       
'watchdog_severity' => array('type' => 'value', 'label' => t('The severity of the watchdog event')),
      ),
    )
  );
}

/*
* Checks the severity of a watchdog event
*/
function watchdog_rules_severity_condition($watchdog_type, $watchdog_severity, $settings) {
  die(
'watchdog_severity: '.print_r($watchdog_severity,TRUE));
}

/*
* Checks the type of a watchdog event
*/
function watchdog_rules_type_condition($watchdog_type, $watchdog_severity, $settings) {
 
$arr_types = watchdog_rules_get_message_types();
 
$type_key = array_search($watchdog_type, $arr_types);
  if(
$settings['watchdog_type'][$type_key]) {
    die(
'good');
    return
TRUE;
  }
  die(
'bad');
  return
FALSE;
}

function
watchdog_rules_severity_condition_form($settings, &$form) {
 
$form['settings']['watchdog_severity'] = array(
   
'#type' => 'checkboxes',
   
'#title' => t('Match against any of the selected severities'),
   
'#options' => watchdog_severity_levels(),
   
'#default_value' => isset($settings['watchdog_severity']) ? $settings['watchdog_severity'] : array(),
  );
  return
$form;
}

function
watchdog_rules_type_condition_form($settings, &$form) {
 
$form['settings']['watchdog_type'] = array(
   
'#type' => 'checkboxes',
   
'#title' => t('Match against any of the selected types'),
   
'#options' => watchdog_rules_get_message_types(),
   
'#default_value' => isset($settings['watchdog_type']) ? $settings['watchdog_type'] : array(),
  );
  return
$form
}

function
watchdog_rules_get_message_types() {
 
$types = array();

 
$result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
  while (
$object = db_fetch_object($result)) {
   
$types[$object->type] = $object->type;
  }

  return
$types;
}
?>

data type

fago's picture

sry for the last response. I somehow missed you answer.

'type' => 'value' doesn't make much sense it this case, don't use it. It's suitable for other cases when you want to pass a fixed value to an action.

I'd suggest to create a new rules data type for the given $log / $entry variable and let your conditions work on top of it. Do you plan to share this code? As it's integration code for drupal core it would make sense to include it with the module. Just do so and post a patch in the rules issue queue - then I can help getting it done right.

Any update on this? I'd be

fago's picture

Any update on this? I'd be interested in it.. :)

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: