Currently it is not possible to act through rules when a OG role is granted or revoked from a user. It would be interesting to have rules events that can be triggered by those events and a small patch would allow that, but I couldn't find anything related in the issue queue.

Is that a feature that you would consider to include, if I submit a patch?

Thanks!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hernani’s picture

And patch attached!

hernani’s picture

Status: Active » Needs review

Changing status.

amitaibu’s picture

Status: Needs review » Needs work

I would expect the rule to pass the role as-well.

hernani’s picture

What should be the variable type of the role?

maximpodorov’s picture

Maybe it's a separate issue...
Is it possible to add new actions which grant and revoke user role?
Something like this:

/**
 * Implements hook_rules_action_info().
 */
function og_rules_rules_action_info() {
  $actions = array();

  $actions['og_rules_rules_grant_role'] = array(
    'label' => t('Grant a group role to a user'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'group' => array(
        'type' => array_keys(og_get_all_group_entity()),
        'label' => t('Group'),
        'description' => t('Group'),
        'wrapped' => TRUE,
      ),
      'account' => array(
        'type' => 'user',
        'label' => t('The user which will be a granted a role.'),
      ),
      'og_role' => array(
        'type' => 'integer',
        'label' => t('The role ID.'),
      ),
    ),
  );

  $actions['og_rules_rules_revoke_user_role'] = array(
    'label' => t('Revoke a group role from a user'),
    'group' => t('Organic groups'),
    'parameter' => array(
      'group' => array(
        'type' => array_keys(og_get_all_group_entity()),
        'label' => t('Group'),
        'description' => t('Group'),
        'wrapped' => TRUE,
      ),
      'account' => array(
        'type' => 'user',
        'label' => t('The user which will lose a role.'),
      ),
      'og_role' => array(
        'type' => 'integer',
        'label' => t('The role ID.'),
      ),
    ),
  );

  return $actions;
}

function og_rules_rules_grant_role(EntityDrupalWrapper $group, $account, $rid) {
  if (FALSE == og_role_load($rid)) {
    throw new RulesEvaluationException('Unable to load group role with rid "@rid"', array('@rid' => $rid));
  }
  og_role_grant($group->type(), $group->getIdentifier(), $account->uid, $rid);
}

function og_rules_rules_revoke_user_role(EntityDrupalWrapper $group, $account, $rid) {
  if (FALSE == og_role_load($rid)) {
    throw new RulesEvaluationException('Unable to load group role with rid "@rid"', array('@rid' => $rid));
  }
  og_role_revoke($group->type(), $group->getIdentifier(), $account->uid, $rid);
}
othermachines’s picture

Status: Needs work » Needs review
FileSize
2.48 KB

I need this, so I've created a new patch against 7.x-2.x. It passes in the role id.

pippopeppe’s picture

#6: og-add_og_role_events-1816800.patch queued for re-testing.

othermachines’s picture

Rerolled without whitespace errors.

amitaibu’s picture

+++ b/og.module
@@ -2856,6 +2856,10 @@ function og_role_grant($group_type, $gid, $uid, $rid) {
+      rules_invoke_event('og_role_grant', og_get_membership('node', $gid, 'user', $uid), entity_metadata_wrapper('user', user_load($uid)), $rid);

Why hardcode to node?

othermachines’s picture

You know, that's a very good question. Again...

(Also fixed a small typo.)

amitaibu’s picture

entity_metadata_wrapper() can get the $uid, no need to user_load() it.

othermachines’s picture

Again...

amitaibu’s picture

Status: Needs review » Fixed

Committed, thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.