3

I am using Drupal 7 and the Group module. I would like to create reaction rule or some code which automatically signs the user up to group 'blue', when they sign up to group 'red'.

I wouldn't mind using Rules or hard coding it. The only thing is that if the user leaves the first group, I want to be able to cancel their membership in the second group too.

What would be the easiest way? I have of course tried Rules without success and I cannot find a simple way to hard code it.

2

Found the solution. When using the group module, there are methods which work well. I used a hook_group_membership_insert($group_membership):

function [my_module]_group_membership_insert($group_membership) {

   $grouptest = group_load($group_membership->gid);
   $grouptest->addMember($group_membership->uid);
}

The same exists for deleting a member, just use the following function:

$grouptest->removeMember($uid);
  • Hi "same firstname" ... Please enhance your answer to explain your code. E.g. what your "first group" is in it, and what your "second group is". Also, let me know if you're interested in an extra answer to illustrates "How to add a user to another group" (as in your title). Note: your 2nd parg of your question body is really like a different question I think, no? – Pierre.Vriens Aug 31 '16 at 13:54
  • Hi Pierre :) I would love it if you knew how to do it via Rules for people who are less happy with coding. Thanks! – Pierre_colin Aug 31 '16 at 15:09
1

Add user to another group

To answer 1 of your questions (= "How to add a user to another group?"): it is indeed possible to use the Rules module (the Group module has all sorts of out-of-the-box integrations with Rules).

To do so, you can use a variation of what's explained in comment # 5 of the issue about "Bulk adding users to a group". Here is a rule (in Rules export format) that should do the job:

{ "rules_create_additional_group_membership" : {
    "LABEL" : "Create additional group membership",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "group" ],
    "REQUIRES" : [ "rules", "group" ],
    "ON" : { "group_membership_insert" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "group-membership:group:gid" ], "value" : "1" } }
    ],
    "DO" : [
      { "entity_create" : {
          "USING" : { "type" : "group_membership" },
          "PROVIDE" : { "entity_created" : { "created_group_mbrship" : "Created group mbrship" } }
        }
      },
      { "data_set" : { "data" : [ "created-group-mbrship:group" ], "value" : "2" } },
      { "data_set" : {
          "data" : [ "created-group-mbrship:user" ],
          "value" : [ "site:current-user" ]
        }
      },
      { "data_set" : { "data" : [ "created-group-mbrship:status" ], "value" : "active" } },
      { "entity_save" : { "data" : [ "created-group-mbrship" ], "immediate" : "1" } }
    ]
  }
}

Some more details about this rule:

  • Rules Event: After saving a new group membership.
  • Rules Conditions: Check that the group for the new membership is 1 (adapt this to fit your "1st group").
  • Rules Actions:
    • Create a new entity (Entity type = Group membership).
    • Set a data value for group (= 2, adapt this to fit your "2nd group").
    • Set a data value for user (= [site:current-user]).
    • Set a data value for status (= Active, assuming that's the status you want).
    • Save the entity (make sure to use Force saving immediately).

If you have the Rules UI (and the Group module) enabled, you should be able to just import the above rule in your own site (after you eventually edited the group IDs 1 and 2 to fit your own needs).

Remove user from another group

Your 2nd question (= "... if the user leaves the first group, I want to be able to cancel their membership in the second group too ...") is really a different question. The major challenge to answer it is that when something happens to the 1st group, you want something to happen in the 2nd group.

Here is a (rather basic) Rules Component that you could use to change such membership to status "blocked":

{ "rules_remove_a_single_group_membership" : {
    "LABEL" : "Remove a single group membership",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : {
      "group_mbrship" : { "label" : "Group membership", "type" : "group_membership" },
      "user" : { "label" : "User", "type" : "user" }
    },
    "IF" : [
      { "data_is" : { "data" : [ "group-mbrship:group:gid" ], "value" : "2" } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "group-mbrship:status" ], "value" : "blocked" } }
    ]
  }
}

Note the Rules Condition that checks that the group for the membership to be blocked is 2 (adapt this to fit your "2nd group").

The only thing left is to have this Rules Component triggered via a Rules Action of another (extra) rule that is triggered when a user leaves the 1st Group. If you wonder how to do that, and to avoid my answer gets marked as access only for +15K users, please use this link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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