5

I'm having trouble creating a small module to add an Event scenario to Rules using the Rules API. Basically, I'm creating a rule that should detect to see if certain fields of a file entity have certain values upon saving that file. Then, if the conditions are met, the action is to change the URL path alias of /video/ to point to the newest video file in my system (file/[fid])

But I'm getting stuck even creating the new Event to even work. My code below does succeed in making it appear in the Rules event list, and then when setting conditions, the Data Selector exposes the correct file fields. But, when I set a very simple condition or even no condition, my action does not fire.

(A bigger aside: shouldn't the Rules module come pre-packaged with functionality for dealing with file entities? There are plenty of events for nodes, users, and comments, but nothing for files.)

So, what is wrong with my code below?

/**
 * Implementation of hook_rules_event_info().
 */
function rules_extended_rules_event_info() {
  return array(
    'rules_extended_rules_event_file_insert' => array(
      'group' => t('Entity'),
      'label' => t('After saving a new entity'),
      'module' => 'rules_extended',
      'variables' => array (
        'file' => array ('type' => 'entity', 'label' => t('created entity')),           
      ),
    ),
    'rules_extended_rules_event_file_update' => array(
      'group' => t('Entity'),
      'label' => t('After updating an existing entity'),
      'module' => 'rules_extended',
      'variables' => array (
        'file' => array ('type' => 'entity', 'label' => t('updated entity')),
      ),
    ),
  );
}

1 Answer 1

1

You can't just declare hook_rules_event_info(), it just tells rules about an event type, but you still need to invoke that event.

To invoke a declare event, use rules_invoke_event().

Example

/**
 * Implements hook_entity_insert().
 */
function rules_extended_entity_insert($entity, $type) {
  if ($type == 'file') {
    rules_invoke_event('rules_extended_rules_event_file_insert', $entity);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.