function og_field_create_instance in Organic groups 7.2
Implements hook_field_create_instance().
- Create default OG roles per entity-type and bundle.
- Create a group audience field on the user's entity, referencing the first group defined.
File
- ./
og.module, line 639 - Enable users to create and manage groups with roles and permissions.
Code
function og_field_create_instance($instance) {
if ($instance['field_name'] != OG_GROUP_FIELD) {
return;
}
// Create default roles per entity-type per bundle.
og_roles_override($instance['entity_type'], $instance['bundle'], 0);
// Check if we need to add a group audience on the user's entity.
// We add a different field, so each field can be set differently.
$entity_type = $instance['entity_type'];
$bundle = $instance['bundle'];
foreach (array_keys(og_get_group_audience_fields()) as $field_name) {
$field = field_info_field($field_name);
if ($field['settings']['target_type'] == $entity_type && empty($field['settings']['handler_settings']['target_bundles'])) {
return;
}
if ($field['settings']['target_type'] == $entity_type && in_array($bundle, $field['settings']['handler_settings']['target_bundles'])) {
return;
}
}
// If we reached here, it means we need to create a field.
// Pick an unused name.
$field_name = substr("og_user_{$entity_type}", 0, 32);
$i = 1;
while (field_info_field($field_name)) {
$field_name = substr("og_user_{$entity_type}", 0, 32 - strlen($i)) . $i;
++$i;
}
$og_field = og_fields_info(OG_AUDIENCE_FIELD);
$og_field['field']['settings']['target_type'] = $entity_type;
if ($entity_type == 'node') {
$og_field['instance']['label'] = t('Group membership');
}
else {
$entity_info = entity_get_info($entity_type);
$og_field['instance']['label'] = t('@label group membership', array(
'@label' => $entity_info['label'],
));
}
// If the user entity type has multiple bundles, make sure to attach a field
// instance to all of them.
$entity_info = entity_get_info('user');
foreach (array_keys($entity_info['bundles']) as $user_bundle) {
og_create_field($field_name, 'user', $user_bundle, $og_field);
}
}