You are here

function entityreference_prepopulate_field_access in Entityreference prepopulate 7

Implements hook_field_access().

File

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

Code

function entityreference_prepopulate_field_access($op, $field, $entity_type, $entity, $account) {
  if ($op != 'edit' || $field['type'] != 'entityreference') {
    return;
  }
  if (empty($entity)) {

    // $entity might be NULL, so return early.
    // @see field_access().
    return;
  }
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  if (empty($instance['settings']['behaviors']['prepopulate']['status'])) {
    return;
  }
  $settings = $instance['settings']['behaviors']['prepopulate'];
  if (!empty($settings['skip_perm']) && user_access($settings['skip_perm'])) {
    return;
  }
  if ($id && empty($entity->is_new)) {

    // The field need to be hidden when editing the entity.
    return $settings['action'] == 'hide' && !empty($settings['action_on_edit']) ? FALSE : NULL;
  }
  if ($settings['action'] == 'hide') {

    // If entity is already saved and not just inserted, deny access, otherwise
    // ignore.
    return $id && empty($entity->is_new) ? FALSE : NULL;
  }
  $ids = entityreference_prepopulate_get_values($field, $instance);
  if (!$ids && $settings['fallback'] == 'hide') {
    return FALSE;
  }
}