2012-12-21 17:03:57 +00:00
<?php
/**
* @file
* Provides in-place content editing functionality for fields.
*
2014-04-16 21:42:14 +00:00
* The Quick Edit module makes content editable in-place. Rather than having to
* visit a separate page to edit content, it may be edited in-place.
2012-12-21 17:03:57 +00:00
*
* 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;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2014-06-06 18:34:14 +00:00
/**
* Implements hook_help().
*/
2014-06-30 03:33:08 +00:00
function quickedit_help($route_name, RouteMatchInterface $route_match) {
2014-06-06 18:34:14 +00:00
switch ($route_name) {
case 'help.page.quickedit':
$output = '<h3>' . t('About') . '</h3>';
Issue #2560783 by stefan.r, joelpittet, lauriii, Cottser, Sutharsan, kgoel, justAChris, Gábor Hojtsy, dawehner, jhodgdon, effulgentsia, xjm, andypost, googletorp: Replace !placeholder with :placeholder for URLs in hook_help() implementations
2015-09-21 11:38:19 +00:00
$output .= '<p>' . t('The Quick Edit module allows users with the <a href=":quickedit_permission">Access in-place editing</a> and <a href=":contextual_permission">Use contextual links</a> permissions to edit field content without visiting a separate page. For more information, see the <a href=":handbook_url">online documentation for the Quick Edit module</a>.', array(':handbook_url' => 'https://www.drupal.org/documentation/modules/edit', ':quickedit_permission' => \Drupal::url('user.admin_permissions', array(), array('fragment' => 'module-quickedit')), ':contextual_permission' => \Drupal::url('user.admin_permissions', array(), array('fragment' => 'module-contextual')))) . '</p>';
2014-06-06 18:34:14 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Editing content in-place') . '</dt>';
$output .= '<dd>';
Issue #2560783 by stefan.r, joelpittet, lauriii, Cottser, Sutharsan, kgoel, justAChris, Gábor Hojtsy, dawehner, jhodgdon, effulgentsia, xjm, andypost, googletorp: Replace !placeholder with :placeholder for URLs in hook_help() implementations
2015-09-21 11:38:19 +00:00
$output .= '<p>' . t('To edit content in place, you need to activate quick edit mode for a content item. Activate quick edit mode by choosing Quick edit from the contextual links for an area displaying the content (see the <a href=":contextual">Contextual Links module help</a> for more information about how to use contextual links).', array(':contextual' => \Drupal::url('help.page', array('name' => 'contextual')))) . '</p>';
2014-06-06 18:34:14 +00:00
$output .= '<p>' . t('Once quick edit mode is activated, you will be able to edit the individual fields of your content. In the default theme, with a JavaScript-enabled browser and a mouse, the output of different fields in your content is outlined in blue, a pop-up gives the field name as you hover over the field output, and clicking on a field activates the editor. Closing the pop-up window ends quick edit mode.') . '</p>';
$output .= '</dd>';
$output .= '</dl>';
return $output;
}
}
2012-12-21 17:03:57 +00:00
/**
2014-10-16 12:36:06 +00:00
* Implements hook_page_attachments().
2013-02-12 21:46:04 +00:00
*
2014-04-16 21:42:14 +00:00
* Adds the quickedit library to the page for any user who has the 'access
* in-place editing' permission.
2012-12-21 17:03:57 +00:00
*/
2014-10-16 12:36:06 +00:00
function quickedit_page_attachments(array &$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.
2014-03-21 12:35:45 +00:00
if (\Drupal::service('router.admin_context')->isAdminRoute()) {
2013-12-23 21:08:08 +00:00
return;
}
2014-04-16 21:42:14 +00:00
$page['#attached']['library'][] = 'quickedit/quickedit';
2012-12-21 17:03:57 +00:00
}
/**
2014-12-11 21:09:30 +00:00
* Implements hook_library_info_alter().
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
*
* Includes additional stylesheets defined by the admin theme to allow it to
2014-04-16 21:42:14 +00:00
* customize the Quick Edit toolbar appearance.
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
*
* 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
2014-04-16 21:42:14 +00:00
* quickedit_stylesheets:
* - css/quickedit.css
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
* @endcode
2013-10-05 07:14:39 +00:00
*/
2014-12-11 21:09:30 +00:00
function quickedit_library_info_alter(&$libraries, $extension) {
if ($extension === 'quickedit' && isset($libraries['quickedit'])) {
$theme = Drupal::config('system.theme')->get('admin');
// First let the base theme modify the library, then the actual theme.
$alter_library = function(&$library, $theme) use (&$alter_library) {
2015-05-08 14:33:16 +00:00
if (isset($theme) && $theme_path = drupal_get_path('theme', $theme)) {
2014-12-11 21:09:30 +00:00
$info = system_get_info('theme', $theme);
// Recurse to process base theme(s) first.
if (isset($info['base theme'])) {
$alter_library($library, $info['base theme']);
}
if (isset($info['quickedit_stylesheets'])) {
foreach ($info['quickedit_stylesheets'] as $path) {
$library['css']['theme']['/' . $theme_path . '/' . $path] = [];
}
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
}
}
2014-12-11 21:09:30 +00:00
};
$alter_library($libraries['quickedit'], $theme);
2013-10-05 07:14:39 +00:00
}
}
2013-06-13 23:03:32 +00:00
/**
* Implements hook_field_formatter_info_alter().
*
2014-04-16 21:42:14 +00:00
* Quick Edit extends the @FieldFormatter annotation with the following keys:
* - quickedit: 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
* Quick Edit module provides.
2013-06-13 23:03:32 +00:00
*/
2014-04-16 21:42:14 +00:00
function quickedit_field_formatter_info_alter(&$info) {
2013-06-13 23:03:32 +00:00
foreach ($info as $key => $settings) {
2013-11-13 15:04:04 +00:00
// Set in-place editor to 'form' if none is supplied.
2014-04-16 21:42:14 +00:00
if (empty($settings['quickedit'])) {
$info[$key]['quickedit'] = array('editor' => 'form');
2013-06-13 23:03:32 +00:00
}
}
}
2015-10-05 07:11:17 +00:00
/**
* Implements hook_preprocess_HOOK() for the page title template.
*/
function quickedit_preprocess_page_title(&$variables) {
$variables['title_attributes']['class'][] = 'js-quickedit-page-title';
}
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
*/
2014-04-16 21:42:14 +00:00
function quickedit_preprocess_field(&$variables) {
2016-03-04 18:43:24 +00:00
$variables['#cache']['contexts'][] = 'user.permissions';
if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
return;
}
2012-12-21 17:03:57 +00:00
$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
2014-04-16 21:42:14 +00:00
// Quick Edit module only supports view modes, not dynamically defined
// "display options" (which \Drupal\Core\Field\FieldItemListInterface::view()
// always names the "_custom" view mode).
2014-03-05 16:49:15 +00:00
// @see \Drupal\Core\Field\FieldItemListInterface::view()
2015-05-24 20:08:46 +00:00
// @see https://www.drupal.org/node/2120335
2013-11-24 04:02:22 +00:00
if ($element['#view_mode'] === '_custom') {
return;
}
2015-09-22 08:14:42 +00:00
// Fields that are computed fields are not editable.
2014-02-24 11:42:13 +00:00
$definition = $entity->getFieldDefinition($element['#field_name']);
2015-09-22 08:14:42 +00:00
if (!$definition->isComputed()) {
2014-04-16 21:42:14 +00:00
$variables['attributes']['data-quickedit-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
*/
2014-04-16 21:42:14 +00:00
function quickedit_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
2016-03-04 18:43:24 +00:00
$build['#cache']['contexts'][] = 'user.permissions';
if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
return;
}
2014-04-16 21:42:14 +00:00
$build['#attributes']['data-quickedit-entity-id'] = $entity->getEntityTypeId() . '/' . $entity->id();
2013-05-04 07:18:03 +00:00
}