You are here

function og_group in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og.module \og_group()

Set an association (e.g. subscribe) an entity to a group.

Parameters

$group_type: The entity type of the group.

$gid: The group entity or ID.

$values: Array with the information to pass along, until it is processed in the field handlers.

  • "entity_type": (optional) The entity type (e.g. "node" or "user"). Defaults to 'user'
  • "entity": (optional) The entity object or entity Id to set the association. Defaults to the current user if the $entity_type property is set to 'user'.
  • "field_name": The name of the field, the membership should be registered in. If no value given, a first field with the correct membership type will be used. If no field found, an execpetion will be thrown.

$save_created: (optional) If the OG membership is new, it determines whether the membership will be saved. Defaults to TRUE.

Return value

The OG membership entity.

37 calls to og_group()
generate-og-d7-content-update-7001.php in scripts/generate-og-d7-content-update-7001.php
MigrateDestinationOGMembership::import in includes/migrate/plugins/destinations/og_membership.inc
Import a single membership.
OgAccess::testOgAccessEntity in ./og.test
Verify og_user_access_entity() returns correct value.
OgAccessTestCase::testAccessChangeBatchApi in og_access/og_access.test
Test if group content privacy is changing when group privacy is changed.
OgAccessTestCase::testSubGroupContentAccess in og_access/og_access.test
Testing special case in which a group is also group content, and it is inside another group. When those groups are set to private (either when set specifically or given from the group defaults), it should be verified groups are accessible, even for…

... See full list

File

./og.module, line 1987
Enable users to create and manage groups with roles and permissions.

Code

function og_group($group_type, $gid, $values = array(), $save_created = TRUE) {
  global $user;

  // Set default values.
  $values += array(
    'entity_type' => 'user',
    'entity' => FALSE,
    'field_name' => FALSE,
    'state' => OG_STATE_ACTIVE,
  );
  $entity_type = $values['entity_type'];
  $entity = $values['entity'];
  $field_name = $values['field_name'];
  $state = $values['state'];
  if ($entity_type == 'user' && empty($entity)) {

    // We don't pass the object, as we want entity_metadata_wrapper() to reload
    // the user object.
    $entity = $user->uid;
  }
  $wrapper = entity_metadata_wrapper($entity_type, $entity);

  // If entity was an ID, get the object.
  $entity = $wrapper
    ->value();
  $bundle = $wrapper
    ->getBundle();
  $id = $wrapper
    ->getIdentifier();
  if (is_object($gid)) {
    $group = $gid;
  }
  else {
    $group = entity_load_single($group_type, $gid);
  }

  // the group ID might be the entity, so re-popualte it.
  list($gid, , $group_bundle) = entity_extract_ids($group_type, $group);

  // Get membership if exists.
  $og_membership = og_get_membership($group_type, $gid, $entity_type, $id);
  if (!$og_membership && empty($field_name)) {
    $params = array(
      '%entity-type' => $entity_type,
      '%bundle' => $bundle,
      '%group-type' => $group_type,
      '%group-bundle' => $group_bundle,
    );

    // Allow getting fields not accessible by the user.
    $field_name = og_get_best_group_audience_field($entity_type, $entity, $group_type, $group_bundle, TRUE);
    if (!$field_name) {
      throw new OgException(format_string('There are no OG fields in entity %entity-type and bundle %bundle referencing %group-type - %group-bundle.', $params));
    }
  }
  if ($og_membership) {
    if (empty($og_membership->is_new) && $og_membership->field_name == $field_name && $og_membership->state == $state) {

      // Entity is already associated with group.
      return $og_membership;
    }
    elseif (!empty($field_name) && $og_membership->field_name != $field_name) {

      // Ungroup the current association, as it needs to change field.
      og_ungroup($group_type, $gid, $entity_type, $id);
      $og_membership = FALSE;
    }
    elseif ($og_membership->state != $state) {

      // Change the state.
      $og_membership->state = $state;
    }
    else {

      // Nothing changed.
      return $og_membership;
    }
  }
  if (!$og_membership) {

    // Unset the values, so we don't try to process them.
    unset($values['entity_type'], $values['entity'], $values['field_name']);

    // Create a new OG membership.
    $og_membership = og_membership_create($group_type, $gid, $entity_type, $id, $field_name, $values);
  }
  if (empty($og_membership->is_new) || $save_created) {

    // Pass the entity object along to OgMembership::save() so we don't have
    // to reload it.
    $og_membership->entity = $entity;

    // Save the membership for update, or if the OG membership is new when
    // "save-created" is TRUE.
    $og_membership
      ->save();
  }
  return $og_membership;
}