Thanks for building an alternative to OG this module is everything that I've needed for a new project. My big question is how the heck I can create a rule (or even custom code) to modify the role of all the members within a group. Essentially, the use case is that groups would be open for a period of time, but then need to be inactive at some point. This would maintain the content but disable all the abilities to create new content, etc.

I create a rule but when I set a data value (to a locked down role) I get an error indicating that role cannot be NULL? If this would be better suited through a custom module I'm all ears. I know I could also use hook_entity_presave() which is fine, but in the API I cannot see ways to get at the members and/or modify roles this way.

{ "rules_lock_forum" : {
    "LABEL" : "Lock Forum",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "CALACS" ],
    "REQUIRES" : [ "rules", "group" ],
    "ON" : { "group_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "group:group-type" ], "value" : "forum" } },
      { "entity_has_field" : { "entity" : [ "group" ], "field" : "field_forum_active" } },
      { "data_is" : { "data" : [ "group-unchanged:field-forum-active" ], "value" : "1" } },
      { "data_is" : { "data" : [ "group:field-forum-active" ], "value" : "0" } }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "group", "value" : [ "group" ] },
          "PROVIDE" : { "variable_added" : { "group_id" : "Group ID" } }
        }
      },
      { "entity_query" : {
          "USING" : {
            "type" : "group_membership",
            "property" : "group",
            "value" : [ "group-id" ],
            "limit" : [ "" ]
          },
          "PROVIDE" : { "entity_fetched" : { "group_memberships" : "Memberships" } }
        }
      },
      { "LOOP" : {
          "USING" : { "list" : [ "group-memberships" ] },
          "ITEM" : { "current_member" : "Current Member" },
          "DO" : [
            { "drupal_message" : { "message" : "Removed permissions for user [current-member:user]" } },
            { "data_set" : {
                "data" : [ "current-member:roles" ],
                "value" : { "value" : { "locked_member" : "locked_member" } }
              }
            },
            { "entity_save" : { "data" : [ "current-member" ], "immediate" : "1" } }
          ]
        }
      }
    ]
  }
}

Comments

ShaneOnABike created an issue. See original summary.

ShaneOnABike’s picture

Okay finally I managed to get this working through custom code. Well at least one part of it! The code below reacts on the presave and if they have inactivated the group then it modifies all the roles (except Admin) to the Locked Role. This role is simply not allowing them to change any content after it's be inactive.

The last issue I have is how could I remove the permission to Join the group completely? I don't want to do it for the Group type completely but just this group. Is that even possible? Or would I have to do something nasty whereby when someone requests to join that I just prevent them. Like using hook_group_membership_insert or something....

/*
 * hook_entity_presave()
 */
function glock_entity_presave($entity, $type) {
  // Determine if a group entity                                                                              
  if ($type == 'group') {
    // Determine if the field exists                                                                          
    if (isset($entity->field_forum_active)) {
      // Determine if group is inactive
      if (!$entity->field_forum_active['und'][0]['value']) {                                                  
        // Get each member and change role
        $members = $entity->getMembers();
        dpm($members);
        foreach($members as $guid => $member) {
          $roles = $member->roles;
          if ( !in_array('group_admin', $roles) ) {                                                           
            // Revoke all roles                                                                               
            $member->revokeRoles();
            $member->grantRoles(array('locked_member'));                                                      
          }
        }
      }
    }
  }
}
ShaneOnABike’s picture

Okay so my solution was to use Display Suite and create a custom field which called a function. This carried out the following:

/*
 * Custom output of membership action form from Groups
 */
function calacs_ds_fields_get_membership_action($group) {
  // Verify that this is active
  $active = field_get_items('group', $group, 'field_forum_active');                                                 
  if ($active[0]['value']) {
    global $user;
    module_load_include('inc', 'group', 'forms/group.membership_actions');
    return drupal_render(drupal_get_form('group_membership_actions_form', $group, $user));                          
  }
  return '';
}

Let me know if there was some configuration I was missing, but I suspect that the permissions are somewhat global for the group type. It could be interested to add the functionality to lock down a group (inactive) whereby people can no longer join and/or add content.

Soul88’s picture

Status: Active » Closed (outdated)

We thank everyone for their collaboration on this issue, but as the D7 version is no longer supported, we will now close all D7 issues to keep the issue queue a bit tidier. This information won't go anywhere, it just won't show up on the list of open issues anymore.

Please see: https://www.drupal.org/project/group/issues/3203863#comment-14100281 for more details.