annotate core/modules/quickedit/quickedit.module @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Provides in-place content editing functionality for fields.
Chris@0 6 *
Chris@0 7 * The Quick Edit module makes content editable in-place. Rather than having to
Chris@0 8 * visit a separate page to edit content, it may be edited in-place.
Chris@0 9 *
Chris@0 10 * Technically, this module adds classes and data- attributes to fields and
Chris@0 11 * entities, enabling them for in-place editing.
Chris@0 12 */
Chris@0 13
Chris@0 14 use Drupal\Core\Entity\ContentEntityInterface;
Chris@0 15 use Drupal\Core\Entity\EntityInterface;
Chris@0 16 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
Chris@0 17 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Implements hook_help().
Chris@0 21 */
Chris@0 22 function quickedit_help($route_name, RouteMatchInterface $route_match) {
Chris@0 23 switch ($route_name) {
Chris@0 24 case 'help.page.quickedit':
Chris@0 25 $output = '<h3>' . t('About') . '</h3>';
Chris@0 26 $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>.', [':handbook_url' => 'https://www.drupal.org/documentation/modules/edit', ':quickedit_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-quickedit']), ':contextual_permission' => \Drupal::url('user.admin_permissions', [], ['fragment' => 'module-contextual'])]) . '</p>';
Chris@0 27 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 28 $output .= '<dl>';
Chris@0 29 $output .= '<dt>' . t('Editing content in-place') . '</dt>';
Chris@0 30 $output .= '<dd>';
Chris@0 31 $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).', [':contextual' => \Drupal::url('help.page', ['name' => 'contextual'])]) . '</p>';
Chris@0 32 $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>';
Chris@0 33 $output .= '</dd>';
Chris@0 34 $output .= '</dl>';
Chris@0 35 return $output;
Chris@0 36 }
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Implements hook_page_attachments().
Chris@0 41 *
Chris@0 42 * Adds the quickedit library to the page for any user who has the 'access
Chris@0 43 * in-place editing' permission.
Chris@0 44 */
Chris@0 45 function quickedit_page_attachments(array &$page) {
Chris@0 46 if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
Chris@0 47 return;
Chris@0 48 }
Chris@0 49
Chris@0 50 // In-place editing is only supported on the front-end.
Chris@0 51 if (\Drupal::service('router.admin_context')->isAdminRoute()) {
Chris@0 52 return;
Chris@0 53 }
Chris@0 54
Chris@0 55 $page['#attached']['library'][] = 'quickedit/quickedit';
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Implements hook_library_info_alter().
Chris@0 60 *
Chris@0 61 * Includes additional stylesheets defined by the admin theme to allow it to
Chris@0 62 * customize the Quick Edit toolbar appearance.
Chris@0 63 *
Chris@0 64 * An admin theme can specify CSS files to make the front-end administration
Chris@0 65 * experience of in-place editing match the administration experience in the
Chris@0 66 * back-end.
Chris@0 67 *
Chris@0 68 * The CSS files can be specified via the "edit_stylesheets" property in the
Chris@0 69 * .info.yml file:
Chris@0 70 * @code
Chris@0 71 * quickedit_stylesheets:
Chris@0 72 * - css/quickedit.css
Chris@0 73 * @endcode
Chris@0 74 */
Chris@0 75 function quickedit_library_info_alter(&$libraries, $extension) {
Chris@0 76 if ($extension === 'quickedit' && isset($libraries['quickedit'])) {
Chris@0 77 $theme = Drupal::config('system.theme')->get('admin');
Chris@0 78
Chris@0 79 // First let the base theme modify the library, then the actual theme.
Chris@0 80 $alter_library = function (&$library, $theme) use (&$alter_library) {
Chris@0 81 if (isset($theme) && $theme_path = drupal_get_path('theme', $theme)) {
Chris@0 82 $info = system_get_info('theme', $theme);
Chris@0 83 // Recurse to process base theme(s) first.
Chris@0 84 if (isset($info['base theme'])) {
Chris@0 85 $alter_library($library, $info['base theme']);
Chris@0 86 }
Chris@0 87 if (isset($info['quickedit_stylesheets'])) {
Chris@0 88 foreach ($info['quickedit_stylesheets'] as $path) {
Chris@0 89 $library['css']['theme']['/' . $theme_path . '/' . $path] = [];
Chris@0 90 }
Chris@0 91 }
Chris@0 92 }
Chris@0 93 };
Chris@0 94
Chris@0 95 $alter_library($libraries['quickedit'], $theme);
Chris@0 96 }
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Implements hook_field_formatter_info_alter().
Chris@0 101 *
Chris@0 102 * Quick Edit extends the @FieldFormatter annotation with the following keys:
Chris@0 103 * - quickedit: currently only contains one subkey 'editor' which indicates
Chris@0 104 * which in-place editor should be used. Possible values are 'form',
Chris@0 105 * 'plain_text', 'disabled' or another in-place editor other than the ones
Chris@0 106 * Quick Edit module provides.
Chris@0 107 */
Chris@0 108 function quickedit_field_formatter_info_alter(&$info) {
Chris@0 109 foreach ($info as $key => $settings) {
Chris@0 110 // Set in-place editor to 'form' if none is supplied.
Chris@0 111 if (empty($settings['quickedit'])) {
Chris@0 112 $info[$key]['quickedit'] = ['editor' => 'form'];
Chris@0 113 }
Chris@0 114 }
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * Implements hook_preprocess_HOOK() for the page title template.
Chris@0 119 */
Chris@0 120 function quickedit_preprocess_page_title(&$variables) {
Chris@0 121 $variables['#cache']['contexts'][] = 'user.permissions';
Chris@0 122 if (\Drupal::currentUser()->hasPermission('access in-place editing')) {
Chris@0 123 $variables['title_attributes']['class'][] = 'js-quickedit-page-title';
Chris@0 124 }
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * Implements hook_preprocess_HOOK() for field templates.
Chris@0 129 */
Chris@0 130 function quickedit_preprocess_field(&$variables) {
Chris@0 131 $variables['#cache']['contexts'][] = 'user.permissions';
Chris@0 132 $element = $variables['element'];
Chris@14 133 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
Chris@0 134 $entity = $element['#object'];
Chris@0 135
Chris@14 136 if (!\Drupal::currentUser()->hasPermission('access in-place editing') || !$entity->isLatestRevision()) {
Chris@0 137 return;
Chris@0 138 }
Chris@0 139
Chris@0 140 // Quick Edit module only supports view modes, not dynamically defined
Chris@0 141 // "display options" (which \Drupal\Core\Field\FieldItemListInterface::view()
Chris@0 142 // always names the "_custom" view mode).
Chris@0 143 // @see \Drupal\Core\Field\FieldItemListInterface::view()
Chris@0 144 // @see https://www.drupal.org/node/2120335
Chris@0 145 if ($element['#view_mode'] === '_custom') {
Chris@0 146 return;
Chris@0 147 }
Chris@0 148
Chris@0 149 // Fields that are computed fields are not editable.
Chris@0 150 $definition = $entity->getFieldDefinition($element['#field_name']);
Chris@0 151 if (!$definition->isComputed()) {
Chris@0 152 $variables['attributes']['data-quickedit-field-id'] = $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
Chris@0 153 }
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Implements hook_entity_view_alter().
Chris@0 158 */
Chris@0 159 function quickedit_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
Chris@14 160 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
Chris@0 161 $build['#cache']['contexts'][] = 'user.permissions';
Chris@14 162 if (!\Drupal::currentUser()->hasPermission('access in-place editing') || !$entity->isLatestRevision()) {
Chris@0 163 return;
Chris@0 164 }
Chris@0 165
Chris@0 166 $build['#attributes']['data-quickedit-entity-id'] = $entity->getEntityTypeId() . '/' . $entity->id();
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Check if a loaded entity is the latest revision.
Chris@0 171 *
Chris@0 172 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
Chris@0 173 * The entity to check.
Chris@0 174 *
Chris@0 175 * @return bool
Chris@0 176 * TRUE if the loaded entity is the latest revision, FALSE otherwise.
Chris@0 177 *
Chris@14 178 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use
Chris@14 179 * \Drupal\Core\Entity\RevisionableInterface::isLatestRevision() instead.
Chris@14 180 * As internal API, _quickedit_entity_is_latest_revision() may also be removed
Chris@14 181 * in a minor release.
Chris@0 182 *
Chris@0 183 * @internal
Chris@0 184 */
Chris@0 185 function _quickedit_entity_is_latest_revision(ContentEntityInterface $entity) {
Chris@14 186 @trigger_error('_quickedit_entity_is_latest_revision() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\RevisionableInterface::isLatestRevision() instead. As internal API, _quickedit_entity_is_latest_revision() may also be removed in a minor release.', E_USER_DEPRECATED);
Chris@14 187 return $entity->isLatestRevision();
Chris@0 188 }