You are here

function og_membership_create in Organic groups 7.2

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

Creates a new OG membership.

If a group membership already exists, an exception will be thrown.

Parameters

$group_type: The entity type of the group.

$gid: The group ID.

$entity_type: The entity type of the group content.

$etid: The entity ID of the group content.

$field_name: The group audience field name.

$values: (optional) Array of fields values to be attached to the OG membership, that will be processed using entity-metadata wrapper.

Return value

OgMembership Returns a new OG membership object.

See also

entity_property_values_create_entity()

3 calls to og_membership_create()
og_group in ./og.module
Set an association (e.g. subscribe) an entity to a group.
og_ui_add_users in og_ui/og_ui.admin.inc
Add users to group form.
og_ui_confirm_subscribe in og_ui/og_ui.pages.inc
Confirm subscribe form.

File

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

Code

function og_membership_create($group_type, $gid, $entity_type, $etid, $field_name, $values = array()) {
  global $language;
  $values += array(
    'group_type' => $group_type,
    'gid' => $gid,
    'entity_type' => $entity_type,
    'etid' => $etid,
    'state' => OG_STATE_ACTIVE,
    'created' => time(),
    'field_name' => $field_name,
    'language' => $language->language,
  );
  if (!og_is_group_audience_field($field_name)) {
    throw new OgException(format_string('%field-name is not a valid group-audience field.', array(
      '%field-name' => $field_name,
    )));
  }

  // Get the type from the field.
  $field = field_info_field($field_name);
  $values['type'] = $field['settings']['handler_settings']['membership_type'];
  $wrapper = entity_property_values_create_entity('og_membership', $values);
  return $wrapper
    ->value();
}