It would be great if Subuser could have some Rule triggers, like when a subuser account is created or if an account is deleted.

Comments

mlalu’s picture

I am very, very new to php so here is my first stab at an event rule. I created a new file called "subuser.rules.inc" and added this:

function subuser_rules_event_info() {
  return array(
	'subuser_add_event' => array(
      'label' => t('Subuser was Created'),
      'module' => 'Subuser',
      'arguments' => array(
        'ruleparent_userid' => array('type' => 'user', 'label' => t('Parent user')),
        'ruleuserid' => array('type' => 'user', 'label' => t('Sub user')),
      ),
      'help' => 'This event is triggered when a parent user creates a subuser account.',
    ),
  );
}

I then added this to the subuser.pages.inc file at line 300:

 if (module_exists('rules')) {
     
     $parentuser_uid = $user->uid;
     $subuser_uid = $form_state['user'];
  
          rules_invoke_event('subuser_add_event', $parentuser_uid, $subuser_uid);
     }  
scalp’s picture

Thank you for this. I had to change the call to rules_invoke_event a little to get it to work. The line number isn't going to stay consistent through versions so I added it in the function subuser_create_form_submit($form, &$form_state) right after the following line:

watchdog('user', 'New user: %name (%email).', array('%name' => $name, '%email' => $mail), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit'));

  // INVOKE RULES EVENT
  if (module_exists('rules')) {
     global $user;
     $parentuser_uid = $user;
     $subuser_uid = $account;
 
          rules_invoke_event('subuser_add_event', $parentuser_uid, $subuser_uid);
     } 

I left the variables as named _uid, but to be more precise you should probably remove that since it's not really giving a uid. You'd need to change them in the function subuser_rules_event_info() as well if you did that.

mlalu’s picture

Great idea. Thanks for the feedback.