You are here

function fape_field_edit_page in Field API Pane Editor (FAPE) 7

Page callback to edit an entity field.

1 string reference to 'fape_field_edit_page'
fape_menu in ./fape.module
Implements hook_menu()

File

./fape.module, line 28
Adds direct field editing via contextual links.

Code

function fape_field_edit_page($entity_type, $entity_id, $field_name, $langcode = NULL) {

  // Ensure the entity type is valid:
  if (empty($entity_type)) {
    return MENU_NOT_FOUND;
  }
  $entity_info = entity_get_info($entity_type);
  if (!$entity_info) {
    return MENU_NOT_FOUND;
  }
  $entities = entity_load($entity_type, array(
    $entity_id,
  ));
  if (!$entities) {
    return MENU_NOT_FOUND;
  }
  $entity = reset($entities);
  if (!$entity) {
    return MENU_NOT_FOUND;
  }
  if (!isset($langcode)) {
    $langcode = entity_language($entity_type, $entity);
  }

  // Ensure access to update the entity is granted.
  if (!entity_access('update', $entity_type, $entity)) {
    return MENU_ACCESS_DENIED;
  }

  // Ensure access to actually update this particular field is granted.
  $field = field_info_field($field_name);
  if (!field_access('edit', $field, $entity_type, $entity)) {
    return MENU_ACCESS_DENIED;
  }
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);

  // This allows us to have limited support for non-field API fields.
  // Currently, we have support for node:title, node:author, and node:created.
  if ($entity_type == 'node' && in_array($field_name, array(
    'title',
    'author',
    'created',
  ))) {
    $field_instance = TRUE;
    switch ($field_name) {
      case 'title':
        $subform_id = 'fape_field_edit_node_title_form';
        break;
      case 'author':
        $subform_id = 'fape_field_edit_node_author_form';
        break;
      case 'created':
        $subform_id = 'fape_field_edit_node_created_form';
        break;
    }
    if (!node_access('update', $entity)) {
      return MENU_ACCESS_DENIED;
    }
  }
  else {
    $field_instance = field_info_instance($entity_type, $field_name, $bundle);
    $subform_id = 'fape_field_edit_field_form';
  }
  if (empty($field_instance)) {
    return MENU_NOT_FOUND;
  }
  $form_state = array(
    'entity_type' => $entity_type,
    'entity' => $entity,
    'field_name' => $field_name,
    'langcode' => $langcode,
    'no_redirect' => TRUE,
    'redirect' => $_GET['q'],
    'field_instance' => $field_instance,
    'bundle' => $bundle,
    'subform_id' => $subform_id,
  );

  // Try to figure out bundle's label for nicer field editing title.
  $entity_bundles = $entity_info['bundles'];
  $bundle_label = $bundle;
  foreach ($entity_bundles as $key => $entity_bundle) {
    if ($key == $bundle) {
      if (isset($entity_bundle['label'])) {
        $bundle_label = $entity_bundle['label'];
      }
      break;
    }
  }
  drupal_set_title(t('<em>Edit @type</em> @title', array(
    '@type' => $bundle_label,
    '@title' => $field_instance['label'],
  )), PASS_THROUGH);
  $output = drupal_build_form('fape_field_edit_form', $form_state);
  if (!empty($form_state['executed'])) {
    entity_save($entity_type, $form_state['entity']);
    drupal_goto($form_state['redirect']);
  }
  return $output;
}