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;
Dries Buytaert
committed
use Drupal\user\TempStoreFactory;
/**
* Implements hook_permission().
*/
function edit_permission() {
return array(
'access in-place editing' => array(
'title' => t('Access in-place editing'),
),
);
}
/**
Dries Buytaert
committed
* Implements hook_page_build().
Dries Buytaert
committed
*
Dries Buytaert
committed
* Adds the edit library to the page for any user who has the 'access in-place
* editing' permission.
*/
Dries Buytaert
committed
function edit_page_build(&$page) {
if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
return;
}
Dries Buytaert
committed
$page['#attached']['library'][] = array('edit', 'edit');
}
/**
Jennifer Hodgdon
committed
* 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.
Dries Buytaert
committed
$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.
Dries Buytaert
committed
$path . '/js/views/AppView.js' => $options,
$path . '/js/views/EditorDecorationView.js' => $options,
Dries Buytaert
committed
$path . '/js/views/EntityView.js' => $options,
$path . '/js/views/EntityToolbarView.js' => $options,
Dries Buytaert
committed
$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(
Alex Pott
committed
$path . '/css/edit.module.css' => array(),
Dries Buytaert
committed
$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'),
Dries Buytaert
committed
array('system', 'jquery.ui.position'),
array('system', 'drupal'),
Dries Buytaert
committed
array('system', 'drupal.displace'),
array('system', 'drupal.form'),
array('system', 'drupal.ajax'),
Dries Buytaert
committed
array('system', 'drupal.debounce'),
array('system', 'drupalSettings'),
Angie Byron
committed
array('system', 'drupal.dialog'),
),
);
$libraries['edit.inPlaceEditor.form'] = array(
Dries Buytaert
committed
'title' => 'Form in-place editor',
'version' => \Drupal::VERSION,
Angie Byron
committed
'js' => array(
Dries Buytaert
committed
$path . '/js/editors/formEditor.js' => $options,
Angie Byron
committed
),
'dependencies' => array(
array('edit', 'edit'),
),
);
$libraries['edit.inPlaceEditor.plainText'] = array(
'title' => 'Plain text in-place editor',
'version' => \Drupal::VERSION,
Angie Byron
committed
'js' => array(
$path . '/js/editors/plainTextEditor.js' => $options,
Angie Byron
committed
),
'dependencies' => array(
array('edit', 'edit'),
),
);
return $libraries;
}
Angie Byron
committed
/**
* 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();
}
}
}
Alex Pott
committed
/**
* 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.
Alex Pott
committed
*/
function edit_field_formatter_info_alter(&$info) {
foreach ($info as $key => $settings) {
// Set in-place editor to 'form' if none is supplied.
Alex Pott
committed
if (empty($settings['edit'])) {
$info[$key]['edit'] = array('editor' => 'form');
}
}
}
/**
Angie Byron
committed
* 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'];
}
}
Dries Buytaert
committed
/**
* Implements hook_entity_view_alter().
Angie Byron
committed
*/
function edit_entity_view_alter(&$build, EntityInterface $entity, EntityDisplay $display) {
$build['#attributes']['data-edit-entity-id'] = $entity->entityType() . '/' . $entity->id();
Angie Byron
committed
}
Angie Byron
committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* 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;
}