You are here

function entityreference_current_get_values in Entityreference Current 7

Wrapper function to get context (e.g. from Menu Object 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_current_get_values()
entityreference_current_field_access in ./entityreference_current.module
Implements hook_field_access().
entityreference_current_field_attach_form in ./entityreference_current.module
Implements hook_field_attach_form().
entityreference_current_field_default_value in ./entityreference_current.module
Field default value callback.

File

./entityreference_current.inc, line 23
Common non-hook functions for Entity Reference Current module.

Code

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

    // Do nothing when current is disabled for this field.
    return;
  }
  $field_name = $field['field_name'];
  $target_type = $field['settings']['target_type'];
  $cache =& drupal_static(__FUNCTION__, array());
  $identifier = array(
    $instance['entity_type'],
    $instance['bundle'],
    $field_name,
    $validate,
  );
  $identifier = implode(':', $identifier);
  if (isset($cache[$identifier])) {
    return $cache[$identifier];
  }
  $cache[$identifier] = $ids = array();

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

  // Check if there are values in the Menu Object.
  if (!$ids) {
    $entity = entityreference_current_get_current_entity($field, $instance);
    if ($entity) {
      $entity_ids = entity_extract_ids($target_type, $entity);
      $ids = array(
        $entity_ids[0],
      );
    }
  }
  if (!$ids || !$validate) {

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

  // 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;
}