function quickedit_preprocess_field

Same name in other branches
  1. 8.9.x core/modules/quickedit/quickedit.module \quickedit_preprocess_field()

Implements hook_preprocess_HOOK() for field templates.

File

core/modules/quickedit/quickedit.module, line 168

Code

function quickedit_preprocess_field(&$variables) {
    $variables['#cache']['contexts'][] = 'user.permissions';
    $element = $variables['element'];
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $element['#object'];
    if (!\Drupal::currentUser()->hasPermission('access in-place editing') || $entity instanceof RevisionableInterface && !$entity->isLatestRevision()) {
        return;
    }
    // Quick Edit module only supports view modes, not dynamically defined
    // "display options" (which \Drupal\Core\Field\FieldItemListInterface::view()
    // always names the "_custom" view mode).
    // @see \Drupal\Core\Field\FieldItemListInterface::view()
    // @see https://www.drupal.org/node/2120335
    if ($element['#view_mode'] === '_custom') {
        return;
    }
    // Fields that are computed fields are not editable.
    $definition = $entity->getFieldDefinition($element['#field_name']);
    if (!$definition->isComputed()) {
        $variables['attributes']['data-quickedit-field-id'] = $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.