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