function og_membership_create in Organic groups 7
Same name and namespace in other branches
- 7.2 og.module \og_membership_create()
Creates a new OG membership.
If a group membership already exists, an exception will be thrown.
Parameters
$gid: The group ID
$entity_type: The entity type of the group content.
$etid: The entity ID of the group content.
$values: Optional; Array of fields values to be attached to the OG membership, that will be processed using their entity-metadata wrapper
Return value
OgMembership Returns a new OG membership object.
See also
entity_property_values_create_entity()
5 calls to og_membership_create()
- og_entity_insert in ./
og.module - Implements hook_entity_insert().
- og_membership_insert_on_entity_presave in ./
og.module - Create a new group membership on entity presave.
- og_og_migrate_group_membership in plugins/
og_migrate/ group_membership.inc - Add group membership enteties instead of field data.
- 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 1065 - Enable users to create and manage groups with roles and permissions.
Code
function og_membership_create($gid, $entity_type, $etid, $values = array()) {
// Make sure the group membership doesn't already exist.
if (og_get_group_membership($gid, $entity_type, $etid)) {
$group = og_label($gid);
throw new OgException('OG membership for entity ' . check_plain($entity_type) . ' with ID ' . check_plain($etid) . ' for group ID ' . $gid . ' (' . $group . ') already exists.');
}
$values['entity_type'] = $entity_type;
$values['etid'] = $etid;
$values['gid'] = $gid;
// Process the values as it might arrived from og_group().
if (!empty($values['membership type'])) {
$values['name'] = $values['membership type'];
unset($values['membership type']);
}
if (!empty($values['membership fields'])) {
foreach ($values['membership fields'] as $field_name => $value) {
$values[$field_name] = $value;
}
unset($values['membership fields']);
}
$values += array(
'type' => OG_MEMBERSHIP_TYPE_DEFAULT,
'state' => OG_STATE_ACTIVE,
'created' => time(),
);
$return = entity_create('og_membership', $values);
return $return;
}