You are here

function entityreference_prepopulate_get_values in Entityreference prepopulate 7

Wrapper function to get context (e.g. from URL or OG-context).

Parameters

$entity_type: The entity type the entity.

$entity: The entity object that is being checked.

$field: The field info array.

$instance: The instance info array.

$validate: Determine if access validation should be performed. Defaults to TRUE.

Return value

Array of IDs a user may view.

3 calls to entityreference_prepopulate_get_values()
entityreference_prepopulate_field_access in ./entityreference_prepopulate.module
Implements hook_field_access().
entityreference_prepopulate_field_attach_form in ./entityreference_prepopulate.module
Implements hook_field_attach_form().
entityreference_prepopulate_field_default_value in ./entityreference_prepopulate.module
Field default value callback.

File

./entityreference_prepopulate.module, line 215
Prepopulate entity reference values from URL.

Code

function entityreference_prepopulate_get_values($field, $instance, $validate = TRUE) {
  if (!$instance['settings']['behaviors']['prepopulate']['status']) {

    // Do nothing when prepopulate is disabled for this field.
    return;
  }
  $field_name = $field['field_name'];
  $cache =& drupal_static(__FUNCTION__, array());
  $identifier = array(
    $instance['entity_type'],
    $instance['bundle'],
    $field_name,
    $validate,
  );
  $is_audience_field = module_exists('og') && og_is_group_audience_field($field_name);

  // Add field mode to ID if the audience field has one.
  if ($is_audience_field && !empty($instance['field_mode'])) {
    $identifier[] = $instance['field_mode'];
  }
  $identifier = implode(':', $identifier);
  if (isset($cache[$identifier])) {
    return $cache[$identifier];
  }
  if ($is_audience_field && empty($instance['field_mode'])) {

    // Group audience field, but no field-mode provided.
    // So we iterate over the "default" and possibly "admin" field-modes,
    // and return those values together.
    $ids = array();
    $field_modes = !user_access('administer group') ? array(
      'default',
    ) : array(
      'default',
      'admin',
    );
    foreach ($field_modes as $field_mode) {
      $instance['field_mode'] = $field_mode;
      if ($og_ids = entityreference_prepopulate_get_values($field, $instance)) {
        $ids = array_merge($ids, $og_ids);
      }
    }

    // Cache and return the values.
    return $cache[$identifier] = $ids;
  }
  $cache[$identifier] = $ids = array();

  // Check if we have cached values.
  if (!($ids = entityreference_prepopulate_get_values_from_cache($field, $instance))) {

    // Get the providers.
    $enabled_providers = !empty($instance['settings']['behaviors']['prepopulate']['providers']) ? array_filter($instance['settings']['behaviors']['prepopulate']['providers']) : array();
    if (!$enabled_providers) {

      // If the instance isn't updated yet, we assume the URL provider is enabled.
      $enabled_providers += array(
        'url' => TRUE,
      );
    }

    // For backwards compatibility with version 1.4, we check the "og_context"
    // property.
    if (!empty($instance['settings']['behaviors']['prepopulate']['og_context'])) {
      $enabled_providers += array(
        'og_context' => TRUE,
      );
    }
    $providers = entityreference_prepopulate_providers_info();
    foreach (array_keys($enabled_providers) as $name) {
      if (empty($providers[$name]) || !empty($providers[$name]['disabled'])) {

        // Provider doesn't exist any more or is disabled.
        continue;
      }
      $provider = $providers[$name];
      $function = $provider['callback'];
      if ($ids = $function($field, $instance)) {

        // We found values, so we can break.
        break;
      }
    }
  }
  if (!$ids || !$validate) {

    // No IDs found, or no validation is needed.
    $cache[$identifier] = $ids;
    return $ids;
  }
  $handler = entityreference_get_selection_handler($field, $instance);
  if (!($valid_ids = $handler
    ->validateReferencableEntities($ids))) {
    $cache[$identifier] = FALSE;
    return;
  }

  // Only include valid ids.
  $ids = array_intersect($ids, $valid_ids);

  // Check access to the provided entities.
  $target_type = $field['settings']['target_type'];
  entity_load($target_type, $ids);
  foreach ($ids as $delta => $id) {
    $entity = entity_load_single($target_type, $id);
    if (!$entity || !entity_access('view', $target_type, $entity)) {
      unset($ids[$delta]);
    }
  }
  $cache[$identifier] = $ids;
  return $ids;
}