Sunday, December 5, 2010

Rules vs Drupal action-triggers

Today I came across a strange issue in my app. I am using Drupal 6.19, Apache 2.2, PHP 5.2

Scenario 1:  Standard Drupal install with trigger module enabled.
I had  a custom action defined as below:
function mymdule_rules_action_info(){
    $info[mymodule_updateUserInfo] = array(
        'type' => 'user',
        'description' => t('Change user role'),
        'configurable' => FALSE,
        'hooks' => array(
        'user' => array('insert'),
        )
    );
    return $info;
}

function mymodule_updateUserInfo(&$object,$context= array()) {
//Check conditions here and add a specific role to the user.
}


I then saw my action listed under the admin > site configuration > actions page.

I set a trigger on the event of 'User has been created'  to run the above action of  'Change user role'.

So when a new user was added to the site, this trigger was evaluated and the role of the user changed as applicable.


Scenario 2:  Trigger + Rules 6.2.x
On setting up the rules module and using it for some other content based event - triggers, the above action stopped working.
What I noticed was that some other user saving hook was overriding the custom action and resetting the user roles to empty.
I tried using rules module to execute the above custom action on the user has been created event, but that did not help.

Solution: I changed the above action to run on the User has logged In event and that seemed to work. Now there is a overhead that this rule will be executed everytime a user logs in.
Not sure if the rules module event of User has been created does not function correctly.

Advantage of rules : More configuration and no customization.

0 comments: