Skip to content
edit.module 6.4 KiB
Newer Older
<?php

/**
 * @file
 * Provides in-place content editing functionality for fields.
 *
 * The Edit module makes content editable in-place. Rather than having to visit
 * a separate page to edit content, it may be edited in-place.
 *
 * Technically, this module adds classes and data- attributes to fields and
 * entities, enabling them for in-place editing.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\edit\Form\EditFieldForm;
use Drupal\Component\Utility\NestedArray;
use Drupal\entity\Entity\EntityDisplay;

/**
 * Implements hook_permission().
 */
function edit_permission() {
  return array(
    'access in-place editing' => array(
      'title' => t('Access in-place editing'),
    ),
  );
}

/**
 * Adds the edit library to the page for any user who has the 'access in-place
 * editing' permission.
  if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
  $page['#attached']['library'][] = array('edit', 'edit');
 */
function edit_library_info() {
  $path = drupal_get_path('module', 'edit');
  $options = array(
    'scope' => 'footer',
  );
  $libraries['edit'] = array(
    'title' => 'Edit: in-place editing',
    'js' => array(
      // Core.
      $path . '/js/edit.js' => $options,
      // Models.
      $path . '/js/models/AppModel.js' => $options,
      $path . '/js/models/EntityModel.js' => $options,
      $path . '/js/models/FieldModel.js' => $options,
      $path . '/js/models/EditorModel.js' => $options,
      $path . '/js/views/AppView.js' => $options,
      $path . '/js/views/EditorDecorationView.js' => $options,
      $path . '/js/views/EntityView.js' => $options,
      $path . '/js/views/EntityToolbarView.js' => $options,
      $path . '/js/views/ContextualLinkView.js' => $options,
      $path . '/js/views/FieldToolbarView.js' => $options,
      $path . '/js/views/EditorView.js' => $options,
      // Other.
      $path . '/js/util.js' => $options,
      $path . '/js/theme.js' => $options,
    ),
    'css' => array(
      $path . '/css/edit.module.css' => array(),
      $path . '/css/edit.theme.css' => array(),
      $path . '/css/edit.icons.css' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'underscore'),
      array('system', 'backbone'),
      array('system', 'jquery.form'),
      array('system', 'drupal.form'),
      array('system', 'drupal.ajax'),
  $libraries['edit.inPlaceEditor.form'] = array(
    ),
    'dependencies' => array(
      array('edit', 'edit'),
    ),
  );
  $libraries['edit.inPlaceEditor.plainText'] = array(
    'title' => 'Plain text in-place editor',
      $path . '/js/editors/plainTextEditor.js' => $options,
    ),
    'dependencies' => array(
      array('edit', 'edit'),
    ),
  );
/**
 * Implement hook_library_info_alter().
 *
 * Allow the admin theme to override the Edit entity toolbar's default styling.
 * We must do it this way, because an admin theme's hooks do not fire while on
 * the front-end.
 */
function edit_library_info_alter(&$libraries, $module) {
  if ($module == 'edit' && isset($libraries['edit'])) {
    $css = _edit_theme_css();
    foreach ($css as $css_file) {
      $libraries['edit']['css'][$css_file] = array();
    }
  }
}

/**
 * Implements hook_field_formatter_info_alter().
 *
 * Edit extends the @FieldFormatter annotation with the following keys:
 * - edit: currently only contains one subkey 'editor' which indicates which
 *   in-place editor should be used. Possible values are 'form', 'plain_text',
 *   'disabled' or another in-place editor other than the ones Edit module
 *   provides.
 */
function edit_field_formatter_info_alter(&$info) {
  foreach ($info as $key => $settings) {
    // Set in-place editor to 'form' if none is supplied.
    if (empty($settings['edit'])) {
      $info[$key]['edit'] = array('editor' => 'form');
    }
  }
}

 * Implements hook_preprocess_HOOK() for field templates.
 */
function edit_preprocess_field(&$variables) {
  $element = $variables['element'];
  $entity = $element['#object'];

  // Fields that are not part of the entity (i.e. dynamically injected "pseudo
  // fields") and computed fields are not editable.
  $definition = $entity->getPropertyDefinition($element['#field_name']);
  if ($definition && empty($definition['computed'])) {
    $variables['attributes']['data-edit-field-id'] = $entity->entityType() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
 * Implements hook_entity_view_alter().
function edit_entity_view_alter(&$build, EntityInterface $entity, EntityDisplay $display) {
  $build['#attributes']['data-edit-entity-id'] = $entity->entityType() . '/' . $entity->id();

/**
 * Retrieves the admin theme's Edit stylesheets.
 *
 * Admin themes may specify CSS files to make the front-end administration
 * experience of in-place editing match the administration experience on the
 * Drupal back-end.
 * They can specify such CSS files using the "edit_stylesheets" key in
 * the theme .info.yml file.
 *
 * @code
 * edit_stylesheets[] = css/edit.css
 * @endcode
 *
 * @param string|NULL $theme
 *   The theme name for which to retrieve the edit_stylesheets CSS files.
 *
 * @return array
 *   An array of CSS file paths.
 */
function _edit_theme_css($theme = NULL) {
  $css = array();
  if (!isset($theme)) {
    $theme = Drupal::config('system.theme')->get('admin');
  }
  if ($theme_path = drupal_get_path('theme', $theme)) {
    $info = system_get_info('theme', $theme);
    if (isset($info['edit_stylesheets'])) {
      $css = $info['edit_stylesheets'];
      foreach ($css as $key => $path) {
        $css[$key] = $theme_path . '/' . $path;
      }
    }
    if (isset($info['base theme'])) {
      $css = array_merge(_edit_theme_css($info['base theme'], $css));
    }
  }
  return $css;
}