You are here

function ddf_update_access_callback in Dynamic dependent fields 7

Access callback returning TRUE if the user can edit current entity.

1 string reference to 'ddf_update_access_callback'
ddf_menu in ./ddf.module
Implements hook_menu().

File

./ddf.module, line 84

Code

function ddf_update_access_callback($controlling_field_name, $entity_type, $bundle, $entity_id) {
  $entity = NULL;
  if ($entity_id === 'NULL') {
    $entity_id = NULL;
  }
  else {
    $entity = entity_load_single($entity_type, $entity_id);
    if (empty($entity)) {
      return FALSE;
    }
  }
  $dependencies = ddf_load_dependencies($entity_type, $bundle);
  if (empty($dependencies)) {
    return FALSE;
  }
  $dependency_exists = FALSE;
  foreach ($dependencies as $dependency) {
    if ($dependency[0] == $controlling_field_name) {
      $dependency_exists = TRUE;
      break;
    }
  }
  if (!$dependency_exists) {
    return FALSE;
  }
  $field = field_info_field($controlling_field_name);
  $instance = field_info_instance($entity_type, $controlling_field_name, $bundle);
  if (!$field || !$instance || !field_access('edit', $field, $entity_type)) {
    return FALSE;
  }
  switch ($entity_type) {
    case 'node':
      return empty($entity_id) ? node_access('create', $bundle) : node_access('update', $entity);
    case 'taxonomy_term':
      return empty($entity_id) ? user_access('administer taxonomy') : taxonomy_term_edit_access($entity);
    case 'user':
      return empty($entity_id) ? user_access('administer users') || user_register_access() : user_access('administer users') || $entity_id == $GLOBALS['user']->uid;
    case 'comment':
      return empty($entity_id) ? user_access('administer comments') || user_access('post comments') : comment_access('edit', $entity);
  }

  // Enable access for unknown entity types.
  return TRUE;
}