drupal/core/modules/edit/edit.module

225 lines
6.8 KiB
Plaintext

<?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\Core\Entity\Display\EntityViewDisplayInterface;
/**
* Implements hook_permission().
*/
function edit_permission() {
return array(
'access in-place editing' => array(
'title' => t('Access in-place editing'),
),
);
}
/**
* Implements hook_page_build().
*
* Adds the edit library to the page for any user who has the 'access in-place
* editing' permission.
*/
function edit_page_build(&$page) {
if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
return;
}
// In-place editing is only supported on the front-end.
$path = \Drupal::request()->attributes->get('_system_path');
if (path_is_admin($path)) {
return;
}
$page['#attached']['library'][] = array('edit', 'edit');
}
/**
* Implements hook_library_info().
*/
function edit_library_info() {
$path = drupal_get_path('module', 'edit');
$options = array(
'scope' => 'footer',
);
$libraries['edit'] = array(
'title' => 'Edit: in-place editing',
'version' => \Drupal::VERSION,
'js' => array(
// Core.
$path . '/js/edit.js' => $options,
$path . '/js/util.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,
// Views.
$path . '/js/views/AppView.js' => $options,
$path . '/js/views/FieldDecorationView.js' => $options,
$path . '/js/views/EntityDecorationView.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/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', 'jquery.ui.position'),
array('system', 'drupal'),
array('system', 'drupal.displace'),
array('system', 'drupal.form'),
array('system', 'drupal.ajax'),
array('system', 'drupal.debounce'),
array('system', 'drupalSettings'),
array('system', 'drupal.dialog'),
),
);
$libraries['edit.inPlaceEditor.form'] = array(
'title' => 'Form in-place editor',
'version' => \Drupal::VERSION,
'js' => array(
$path . '/js/editors/formEditor.js' => $options,
),
'dependencies' => array(
array('edit', 'edit'),
),
);
$libraries['edit.inPlaceEditor.plainText'] = array(
'title' => 'Plain text in-place editor',
'version' => \Drupal::VERSION,
'js' => array(
$path . '/js/editors/plainTextEditor.js' => $options,
),
'dependencies' => array(
array('edit', 'edit'),
),
);
return $libraries;
}
/**
* 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'];
// Edit module only supports view modes, not dynamically defined "display
// options" (which field_view_field() always names the "_custom" view mode).
// @see field_view_field()
// @see https://drupal.org/node/2120335
if ($element['#view_mode'] === '_custom') {
return;
}
// 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 && !$definition->isComputed()) {
$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, EntityViewDisplayInterface $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;
}