drupal/core/modules/edit/edit.module

191 lines
5.8 KiB
Plaintext
Raw Normal View History

<?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;
/**
* Implements hook_custom_theme().
*
* @todo Add an event subscriber to the Ajax system to automatically set the
* base page theme for all Ajax requests, and then remove this one off.
*/
function edit_custom_theme() {
if (substr(current_path(), 0, 5) === 'edit/') {
return ajax_base_page_theme();
}
}
/**
* Implements hook_permission().
*/
function edit_permission() {
return array(
'access in-place editing' => array(
'title' => t('Access in-place editing'),
),
);
}
/**
* Implements hook_contextual_links_view_alter().
*
* In-place editing builds upon contextual.module, but doesn't actually add its
* "Quick edit" contextual link in PHP (i.e. here) because:
* - that would require to add a local task menu item in the menu system, which
* doesn't make any sense, since there is no corresponding page;
* - it should only work when JavaScript is enabled, because only then in-place
* editing is possible.
*/
function edit_contextual_links_view_alter(&$element, $items) {
if (!user_access('access in-place editing')) {
return;
}
$element['#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',
'website' => 'http://drupal.org/project/edit',
'version' => VERSION,
'js' => array(
// Core.
$path . '/js/edit.js' => $options,
$path . '/js/app.js' => $options,
// Models.
$path . '/js/models/edit-app-model.js' => $options,
// Views.
$path . '/js/views/propertyeditordecoration-view.js' => $options,
$path . '/js/views/contextuallink-view.js' => $options,
$path . '/js/views/modal-view.js' => $options,
$path . '/js/views/toolbar-view.js' => $options,
// Backbone.sync implementation on top of Drupal forms.
$path . '/js/backbone.drupalform.js' => $options,
// VIE service.
$path . '/js/viejs/EditService.js' => $options,
// Create.js subclasses.
$path . '/js/createjs/editable.js' => $options,
$path . '/js/createjs/storage.js' => $options,
// Other.
$path . '/js/util.js' => $options,
$path . '/js/theme.js' => $options,
// Basic settings.
array(
'data' => array('edit' => array(
'metadataURL' => url('edit/metadata'),
'fieldFormURL' => url('edit/form/!entity_type/!id/!field_name/!langcode/!view_mode'),
'context' => 'body',
)),
'type' => 'setting',
),
),
'css' => array(
$path . '/css/edit.css' => array(),
),
'dependencies' => array(
array('system', 'jquery'),
array('system', 'underscore'),
array('system', 'backbone'),
array('system', 'vie.core'),
array('system', 'create.editonly'),
array('system', 'jquery.form'),
array('system', 'drupal.form'),
array('system', 'drupal.ajax'),
array('system', 'drupalSettings'),
),
);
$libraries['edit.editorWidget.form'] = array(
'title' => '"Form" Create.js PropertyEditor widget',
'version' => VERSION,
'js' => array(
$path . '/js/createjs/editingWidgets/formwidget.js' => $options,
),
'dependencies' => array(
array('edit', 'edit'),
),
);
$libraries['edit.editorWidget.direct'] = array(
'title' => '"Direct" Create.js PropertyEditor widget',
'version' => VERSION,
'js' => array(
$path . '/js/createjs/editingWidgets/drupalcontenteditablewidget.js' => $options,
),
'dependencies' => array(
array('edit', 'edit'),
),
);
return $libraries;
}
/**
* 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_preprocess_HOOK() for node.tpl.php.
*
* @todo Remove this, handle in generic way: http://drupal.org/node/1972514.
*/
function edit_preprocess_node(&$variables) {
$node = $variables['elements']['#node'];
$variables['attributes']['data-edit-entity'] = 'node/' . $node->nid;
}
/**
* Implements hook_preprocess_HOOK() for taxonomy-term.tpl.php.
*
* @todo Remove this, handle in generic way: http://drupal.org/node/1972514.
*/
function edit_preprocess_taxonomy_term(&$variables) {
$term = $variables['elements']['#term'];
$variables['attributes']['data-edit-entity'] = 'taxonomy_term/' . $term->id();
}
/**
* Implements hook_preprocess_HOOK() for block.tpl.php.
*
* @todo Remove this, handle in generic way: http://drupal.org/node/1972514.
*/
function edit_preprocess_block(&$variables) {
if (isset($variables['elements']['content']['#custom_block'])) {
$custom_block = $variables['elements']['content']['#custom_block'];
$variables['attributes']['data-edit-entity'] = 'custom_block/' . $custom_block->id();
}
}
/**
* Form constructor for the field editing form.
*
* @ingroup forms
*/
function edit_field_form(array $form, array &$form_state, EntityInterface $entity, $field_name) {
$form_handler = new EditFieldForm();
return $form_handler->build($form, $form_state, $entity, $field_name);
}