2012-12-21 17:03:57 +00:00
|
|
|
<?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;
|
2013-12-12 23:34:44 +00:00
|
|
|
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
2012-12-21 17:03:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_permission().
|
|
|
|
*/
|
|
|
|
function edit_permission() {
|
|
|
|
return array(
|
|
|
|
'access in-place editing' => array(
|
|
|
|
'title' => t('Access in-place editing'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-14 19:02:48 +00:00
|
|
|
* Implements hook_page_build().
|
2013-02-12 21:46:04 +00:00
|
|
|
*
|
2013-05-14 19:02:48 +00:00
|
|
|
* Adds the edit library to the page for any user who has the 'access in-place
|
|
|
|
* editing' permission.
|
2012-12-21 17:03:57 +00:00
|
|
|
*/
|
2013-05-14 19:02:48 +00:00
|
|
|
function edit_page_build(&$page) {
|
2013-09-16 03:58:06 +00:00
|
|
|
if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
|
2012-12-21 17:03:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-23 21:08:08 +00:00
|
|
|
// In-place editing is only supported on the front-end.
|
|
|
|
$path = \Drupal::request()->attributes->get('_system_path');
|
|
|
|
if (path_is_admin($path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-09 19:59:45 +00:00
|
|
|
$page['#attached']['library'][] = 'edit/edit';
|
2012-12-21 17:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
Issue #1996238 by sun, nod_, damiankloip, Wim Leers, longwave, alexpott, Xano, mdrummond, Mark Carver, Jeff Burnz, highrockmedia, joelpittet, et al: Replace hook_library_info() by *.libraries.yml file.
2014-02-23 04:56:51 +00:00
|
|
|
* Implements hook_library_alter().
|
|
|
|
*
|
|
|
|
* Includes additional stylesheets defined by the admin theme to allow it to
|
|
|
|
* customize the Edit toolbar appearance.
|
|
|
|
*
|
|
|
|
* An admin theme can specify CSS files to make the front-end administration
|
|
|
|
* experience of in-place editing match the administration experience in the
|
|
|
|
* back-end.
|
|
|
|
*
|
|
|
|
* The CSS files can be specified via the "edit_stylesheets" property in the
|
|
|
|
* .info.yml file:
|
|
|
|
* @code
|
|
|
|
* edit_stylesheets:
|
|
|
|
* - css/edit.css
|
|
|
|
* @endcode
|
|
|
|
*
|
|
|
|
* The library needs to be dynamically enhanced, because an admin theme normally
|
|
|
|
* does not participate in the front-end.
|
|
|
|
*
|
|
|
|
* @param string $theme
|
|
|
|
* (optional) Internal use only. A base theme name for which to retrieve the
|
|
|
|
* 'edit_stylesheets' property.
|
2013-10-05 07:14:39 +00:00
|
|
|
*
|
Issue #1996238 by sun, nod_, damiankloip, Wim Leers, longwave, alexpott, Xano, mdrummond, Mark Carver, Jeff Burnz, highrockmedia, joelpittet, et al: Replace hook_library_info() by *.libraries.yml file.
2014-02-23 04:56:51 +00:00
|
|
|
* @todo Remove this in favor of the 'stylesheets-additional' property proposed
|
|
|
|
* in https://drupal.org/node/1209958
|
2013-10-05 07:14:39 +00:00
|
|
|
*/
|
2014-03-09 19:59:45 +00:00
|
|
|
function edit_library_alter(array &$library, $name, $theme = NULL) {
|
|
|
|
if ($name == 'edit/edit') {
|
Issue #1996238 by sun, nod_, damiankloip, Wim Leers, longwave, alexpott, Xano, mdrummond, Mark Carver, Jeff Burnz, highrockmedia, joelpittet, et al: Replace hook_library_info() by *.libraries.yml file.
2014-02-23 04:56:51 +00:00
|
|
|
// Retrieve the admin theme.
|
|
|
|
if (!isset($theme)) {
|
|
|
|
$theme = Drupal::config('system.theme')->get('admin');
|
|
|
|
}
|
|
|
|
if ($theme && $theme_path = drupal_get_path('theme', $theme)) {
|
|
|
|
$info = system_get_info('theme', $theme);
|
|
|
|
// Recurse to process base theme(s) first.
|
|
|
|
if (isset($info['base theme'])) {
|
2014-03-09 19:59:45 +00:00
|
|
|
edit_library_alter($library, $name, $info['base theme']);
|
Issue #1996238 by sun, nod_, damiankloip, Wim Leers, longwave, alexpott, Xano, mdrummond, Mark Carver, Jeff Burnz, highrockmedia, joelpittet, et al: Replace hook_library_info() by *.libraries.yml file.
2014-02-23 04:56:51 +00:00
|
|
|
}
|
|
|
|
if (isset($info['edit_stylesheets']) && is_array($info['edit_stylesheets'])) {
|
|
|
|
foreach ($info['edit_stylesheets'] as $path) {
|
|
|
|
$library['css'][$theme_path . '/' . $path] = array(
|
|
|
|
'group' => CSS_AGGREGATE_THEME,
|
2014-03-04 18:04:01 +00:00
|
|
|
'weight' => CSS_THEME,
|
Issue #1996238 by sun, nod_, damiankloip, Wim Leers, longwave, alexpott, Xano, mdrummond, Mark Carver, Jeff Burnz, highrockmedia, joelpittet, et al: Replace hook_library_info() by *.libraries.yml file.
2014-02-23 04:56:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-10-05 07:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 23:03:32 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2013-11-13 15:04:04 +00:00
|
|
|
* 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.
|
2013-06-13 23:03:32 +00:00
|
|
|
*/
|
|
|
|
function edit_field_formatter_info_alter(&$info) {
|
|
|
|
foreach ($info as $key => $settings) {
|
2013-11-13 15:04:04 +00:00
|
|
|
// Set in-place editor to 'form' if none is supplied.
|
2013-06-13 23:03:32 +00:00
|
|
|
if (empty($settings['edit'])) {
|
|
|
|
$info[$key]['edit'] = array('editor' => 'form');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 17:03:57 +00:00
|
|
|
/**
|
2013-10-03 20:55:34 +00:00
|
|
|
* Implements hook_preprocess_HOOK() for field templates.
|
2012-12-21 17:03:57 +00:00
|
|
|
*/
|
|
|
|
function edit_preprocess_field(&$variables) {
|
|
|
|
$element = $variables['element'];
|
2014-01-30 12:06:58 +00:00
|
|
|
/** @var $entity \Drupal\Core\Entity\EntityInterface */
|
2012-12-21 17:03:57 +00:00
|
|
|
$entity = $element['#object'];
|
2013-10-05 07:33:07 +00:00
|
|
|
|
2013-11-24 04:02:22 +00:00
|
|
|
// Edit module only supports view modes, not dynamically defined "display
|
2014-03-05 16:49:15 +00:00
|
|
|
// options" (which \Drupal\Core\Field\FieldItemListInterface::view() always
|
|
|
|
// names the "_custom" view mode).
|
|
|
|
// @see \Drupal\Core\Field\FieldItemListInterface::view()
|
2013-11-24 04:02:22 +00:00
|
|
|
// @see https://drupal.org/node/2120335
|
|
|
|
if ($element['#view_mode'] === '_custom') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-05 07:33:07 +00:00
|
|
|
// Fields that are not part of the entity (i.e. dynamically injected "pseudo
|
|
|
|
// fields") and computed fields are not editable.
|
2014-02-24 11:42:13 +00:00
|
|
|
$definition = $entity->getFieldDefinition($element['#field_name']);
|
2013-11-27 21:54:33 +00:00
|
|
|
if ($definition && !$definition->isComputed()) {
|
2014-01-30 12:06:58 +00:00
|
|
|
$variables['attributes']['data-edit-field-id'] = $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
|
2013-10-05 07:33:07 +00:00
|
|
|
}
|
2012-12-21 17:03:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-12 21:46:04 +00:00
|
|
|
/**
|
2013-06-12 15:57:44 +00:00
|
|
|
* Implements hook_entity_view_alter().
|
2013-05-04 07:18:03 +00:00
|
|
|
*/
|
2013-12-12 23:34:44 +00:00
|
|
|
function edit_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
|
2014-01-30 12:06:58 +00:00
|
|
|
$build['#attributes']['data-edit-entity-id'] = $entity->getEntityTypeId() . '/' . $entity->id();
|
2013-05-04 07:18:03 +00:00
|
|
|
}
|
2013-10-05 07:14:39 +00:00
|
|
|
|