You are here

function og_get_best_group_audience_field in Organic groups 7.2

Get the first best matching group-audience field.

Parameters

$entity_type: The entity type.

$entity: The entity object.

$group_type: The group type.

$group_bundle: The group bundle.

$skip_access: TRUE, if current user access to the field, should be skipped. Defaults to FALSE.

5 calls to og_get_best_group_audience_field()
OgMigrateMembership::prepare in includes/migrate/7200/og_og_membership.migrate.inc
og_form_group_manager_validate in ./og.module
Validate handler; Make sure a group can be created.
og_group in ./og.module
Set an association (e.g. subscribe) an entity to a group.
og_ui_field_formatter_view in og_ui/og_ui.module
Implements hook_field_formatter_view().
og_ui_subscribe in og_ui/og_ui.pages.inc
Subscribe the current user to a group.

File

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

Code

function og_get_best_group_audience_field($entity_type, $entity, $group_type, $group_bundle, $skip_access = FALSE) {
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $field_names = og_get_group_audience_fields($entity_type, $bundle);
  if (!$field_names) {
    return;
  }
  foreach ($field_names as $field_name => $label) {
    $field = field_info_field($field_name);
    $settings = $field['settings'];
    if ($settings['target_type'] != $group_type) {

      // Group type doesn't match.
      continue;
    }
    if (!empty($settings['handler_settings']['target_bundles']) && !in_array($group_bundle, $settings['handler_settings']['target_bundles'])) {

      // Bundles don't match.
      continue;
    }
    if (!og_check_field_cardinality($entity_type, $entity, $field_name)) {

      // Field reached maximum.
      continue;
    }
    if (!$skip_access && !field_access('view', $field, $entity_type, $entity)) {

      // User can't access field.
      continue;
    }
    return $field_name;
  }
}