drupal/core/modules/edit/edit.module

188 lines
5.5 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\edit\Form\EditFieldForm;
use Drupal\Component\Utility\NestedArray;
use Drupal\entity\Entity\EntityDisplay;
use Drupal\user\TempStoreFactory;
/**
* Implements hook_menu().
*/
function edit_menu() {
// @todo Remove these menu items in http://drupal.org/node/1954892 when theme
// callbacks are replaced with something else.
$items['edit/metadata'] = array(
'route_name' => 'edit_metadata',
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
$items['edit/form/%/%/%/%/%'] = array(
'route_name' => 'edit_field_form',
'theme callback' => 'ajax_base_page_theme',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* 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;
}
$page['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('edit' => array(
'fieldFormURL' => url('edit/form/!entity_type/!id/!field_name/!langcode/!view_mode'),
'context' => 'body',
)),
);
$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,
// 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/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', 'jquery.ui.position'),
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.editorWidget.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.editorWidget.direct'] = array(
'title' => 'Direct in-place editor',
'version' => Drupal::VERSION,
'js' => array(
$path . '/js/editors/directEditor.js' => $options,
),
'dependencies' => array(
array('edit', 'edit'),
),
);
return $libraries;
}
/**
* 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', 'direct' or
* 'disabled'.
*/
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.tpl.php.
*/
function edit_preprocess_field(&$variables) {
$element = $variables['element'];
$entity = $element['#object'];
$variables['attributes']['data-edit-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'] = $entity->entityType() . '/' . $entity->id();
}
/**
* Form constructor for the field editing form.
*
* @ingroup forms
*/
function edit_field_form(array $form, array &$form_state, EntityInterface $entity, $field_name, TempStoreFactory $temp_store_factory) {
$form_handler = new EditFieldForm();
return $form_handler->build($form, $form_state, $entity, $field_name, $temp_store_factory);
}