You are here

function quickedit_preprocess_field in Quick Edit 7

Implements hook_preprocess_field().

This is the main entry point for marking up a field as in-place editable.

File

./quickedit.module, line 418
Provides in-place content editing functionality for fields.

Code

function quickedit_preprocess_field(&$variables) {

  // If the user lacks the appropriate permission, return early to avoid
  // processing.
  if (!user_access('access in-place editing')) {
    return;
  }
  $element = $variables['element'];

  // Quick Edit module only supports view modes, not dynamically defined "display
  // options" (which field_view_field() always names the "_custom_display" view
  // mode).
  // @see field_view_field()
  // @see https://drupal.org/node/2120335
  // However, we do support Panelizer, and Panelizer always uses a custom view
  // mode when rendering field panes, in all of its own "view modes".
  // @see includes/panelizer.inc
  if ($element['#view_mode'] === '_custom_display' && !isset($element['#object']->panelizer)) {
    return;
  }

  // Some fields might be rendered through theme_field()
  // but are not Field API fields, e.g. Display Suite fields.
  if (!empty($element['#skip_quickedit'])) {
    return;
  }
  $entity_type = $element['#entity_type'];

  // The 'comment' entity type will never support in-place editing, since it
  // doesn't get Contextual Links.
  if ($entity_type === 'comment') {
    return;
  }

  // For now, Quick Edit only supports 'node' entities. Therefor, don't annotate
  // fields of other entity types with Quick Edit's metadata.
  // @todo https://drupal.org/node/2168725
  if ($entity_type != 'node') {
    return;
  }
  $field_name = $element['#field_name'];
  $language = $element['#language'];
  $view_mode = $element['#view_mode'];
  $entity = $element['#object'];
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // Support for field-collection fields.
  if ($entity_type === 'field_collection_item') {
    $host_entity = field_collection_item_get_host_entity($element['#object']);

    // When this field_collection_item entity is rendered as a field inside its
    // host entity, we should not mark up its fields; the field_collection_item
    // entity as a whole will already be in-place editable (since the entity is
    // considered a field in this contect); it does not make sense to make
    // fields within a field editable.
    if (isset($host_entity
      ->value()->entity_view_prepared)) {
      return;
    }
  }

  // Panelizer support; see explanation at the top of this function.
  // @see includes/panelizer.inc
  if ($element['#view_mode'] === '_custom_display' && isset($element['#object']->panelizer)) {
    $view_mode = _panelizer_generate_quickedit_viewmode($element['#object']);
  }

  // Provide metadata through data- attributes.
  $variables['attributes_array']['data-quickedit-field-id'] = "{$entity_type}/{$id}/{$field_name}/{$language}/{$view_mode}";
}