Quantcast
Channel: drupal.org - Site administrators
Viewing all articles
Browse latest Browse all 426

Using hooks to alter the groups audience field and user permissions

$
0
0

By default OG only allows group members with correct permissions to post and/or edit content associated with a group. It is, however, possible to change that behavior relatively easily using a couple of hooks:

Click headings for link to documentation.

hook_og_user_access_alter()

With hook_og_user_access_alter() you can intercept calls to check a users group permissions and change them, giving or removing permissions as necessary based on the logic you write within the function. Fro example you can give any user the edit permission for a group's content. Or selectively give permissions based on logic in your function.

hook_entity_query_alter()

Using hook_og_user_access_alter() alone, however, will not allow users to post content to a group they are not a member, nor change the group audience of content. To allow users to post to additional groups you will need to use hook_entity_query_alter() to change what groups are available in the groups audience field.

Here is example code from the OG test module. The groups audience field is altered to include all groups of type node, allowing users to post content to any group, whether they are a member or not. Notice that within the IF statement there is code to limit only the group audience field with a specific name.

function og_test_entity_query_alter($query) {
  $instance = !empty($query->metaData['entityreference_selection_handler']->instance) ? $query->metaData['entityreference_selection_handler']->instance : array();
  // Alter OG_AUDIENCE_FIELD . '_altered' to allow selection of all group nodes.
  if (!empty($query->metaData['field']['field_name']) && $query->metaData['field']['field_name'] == OG_AUDIENCE_FIELD . '_altered'&& !empty($instance['field_mode']) && $instance['field_mode'] == 'default'&& !empty($query->propertyConditions[0]['operator']) && $query->propertyConditions[0]['operator'] == 'IN') {
    // Set the property conditions to match all groups of type node.
    $query->propertyConditions[0]['value'] = og_get_all_group('node');
  }
}

Here is code for creating the uniquely named audience field:

  // Create a group audience field with a unique name.
  $og_field = og_fields_info(OG_AUDIENCE_FIELD);
  og_create_field(OG_AUDIENCE_FIELD . '_altered', $entity_type, $bundle, $og_field);

Viewing all articles
Browse latest Browse all 426

Trending Articles