You are here

function quickedit_field_edit in Quick Edit 7

Page callback: Returns a single field edit form as an Ajax response.

Parameters

$entity_type: The type of the entity being edited.

$entity_id: The ID of the entity being edited.

string $field_name: The name of the field that is being edited.

string $langcode: The name of the language for which the field is being edited.

string $view_mode: The view mode the field should be rerendered in.

Return value

The Ajax response.

See also

Drupal 8's QuickEditController::fieldForm()

1 string reference to 'quickedit_field_edit'
quickedit_menu in ./quickedit.module
Implements hook_menu().

File

includes/pages.inc, line 187
AJAX endpoint to retrieve & save subforms for fields and re-render fields.

Code

function quickedit_field_edit($entity_type, $entity_id, $field_name, $langcode = NULL, $view_mode = NULL) {
  $commands = array();

  // Load entity whose field is to be edited.
  $entity = entity_load_single($entity_type, $entity_id);
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $latest_entity_timestamp = $entity->changed;

  // Ensure the user is allowed to edit this field.
  module_load_include('php', 'quickedit', 'includes/EditEntityFieldAccessCheck');
  $accessChecker = new EditEntityFieldAccessCheck();
  if (!$accessChecker
    ->accessEditEntityField($entity_type, $entity, $field_name)) {
    return MENU_ACCESS_DENIED;
  }

  // Replace entity with TempStore copy if available and not resetting, init
  // TempStore copy otherwise.
  ctools_include('object-cache');
  $tempstore_id = _quickedit_entity_tempstore_id($entity_type, $entity_id);
  $tempstore_entity = ctools_object_cache_get('quickedit', $tempstore_id);
  if ($tempstore_entity && (!isset($_POST['reset']) || $_POST['reset'] !== 'true')) {

    // @todo: https://drupal.org/node/2158977 implemented content locking only
    // for nodes, because Drupal 7 only implements it for nodes. We may need to
    // disable content locking for other entity types, sadly.
    $tempstore_entity_timestamp = $tempstore_entity->changed;
    $entity = $tempstore_entity;
  }
  else {
    ctools_object_cache_clear('quickedit', $tempstore_id);
    ctools_object_cache_set('quickedit', $tempstore_id, $entity);
  }

  // Determine which subform should be used, initialize form state, build form.
  $subform_id = 'quickedit_field_edit_form';
  if (_quickedit_is_extra_field($entity_type, $field_name)) {
    $subform_id = quickedit_extra_field_info($entity_type, $field_name, 'quickedit subform id');
  }
  $form_state = array(
    'entity_type' => $entity_type,
    'entity_id' => $entity_id,
    'entity' => $entity,
    'field_name' => $field_name,
    'langcode' => $langcode,
    'view_mode' => $view_mode,
    'no_redirect' => TRUE,
    'bundle' => $bundle,
    'subform_id' => $subform_id,
    'build_info' => array(
      'args' => array(
        $entity,
        $field_name,
      ),
    ),
  );
  $form = drupal_build_form('quickedit_field_form', $form_state);

  // Detect an edit conflict if the current entity timestamp is different
  // from the TempStore entity timestamp (whch is a copy from when the
  // editing process started).
  if (!empty($latest_entity_timestamp) && !empty($tempstore_entity_timestamp) && $latest_entity_timestamp != $tempstore_entity_timestamp) {
    drupal_set_message(t('The copy of the content being edited is outdated. Reload the page to edit an up-to-date version.'), 'error');
    $commands[] = array(
      'command' => 'quickeditFieldFormValidationErrors',
      'data' => theme('status_messages'),
    );
  }
  else {

    // If the form was successfully submitted (executed), then rerender the field.
    if (!empty($form_state['executed'])) {

      // The form submission saved the entity in TempStore. Return the
      // updated view of the field from the TempStore copy.
      $entity = ctools_object_cache_get('quickedit', $tempstore_id);

      // Re-render the updated field.
      $output = _quickedit_render_field($entity_type, $entity, $field_name, $langcode, $view_mode);

      // Re-render the updated field for other view modes (i.e. for other
      // instances of the same logical field on the user's page).
      $other_view_mode_ids = isset($_POST['other_view_modes']) ? $_POST['other_view_modes'] : array();
      $other_view_modes = array();
      foreach ($other_view_mode_ids as $other_view_mode_id) {
        $other_view_modes[$other_view_mode_id] = _quickedit_render_field($entity_type, $entity, $field_name, $langcode, $other_view_mode_id);
      }
      $commands[] = array(
        'command' => 'quickeditFieldFormSaved',
        'data' => $output,
        'other_view_modes' => $other_view_modes,
      );
    }
    else {
      $commands[] = array(
        'command' => 'quickeditFieldForm',
        'data' => drupal_render($form),
      );
      $errors = form_get_errors();
      if (count($errors)) {
        $commands[] = array(
          'command' => 'quickeditFieldFormValidationErrors',
          'data' => theme('status_messages'),
        );
      }
    }
  }

  // When working with a hidden form, we don't want any CSS or JS to be loaded.
  if (isset($_POST['nocssjs']) && $_POST['nocssjs'] === 'true') {
    drupal_static_reset('drupal_add_css');
    drupal_static_reset('drupal_add_js');
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}