Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Adds contextual links to perform actions related to elements on a page.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Component\Serialization\Json;
|
Chris@0
|
10 use Drupal\Component\Utility\UrlHelper;
|
Chris@0
|
11 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
12 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Implements hook_toolbar().
|
Chris@0
|
16 */
|
Chris@0
|
17 function contextual_toolbar() {
|
Chris@0
|
18 $items = [];
|
Chris@0
|
19 $items['contextual'] = [
|
Chris@0
|
20 '#cache' => [
|
Chris@0
|
21 'contexts' => [
|
Chris@0
|
22 'user.permissions',
|
Chris@0
|
23 ],
|
Chris@0
|
24 ],
|
Chris@0
|
25 ];
|
Chris@0
|
26
|
Chris@0
|
27 if (!\Drupal::currentUser()->hasPermission('access contextual links')) {
|
Chris@0
|
28 return $items;
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 $items['contextual'] += [
|
Chris@0
|
32 '#type' => 'toolbar_item',
|
Chris@0
|
33 'tab' => [
|
Chris@0
|
34 '#type' => 'html_tag',
|
Chris@0
|
35 '#tag' => 'button',
|
Chris@0
|
36 '#value' => t('Edit'),
|
Chris@0
|
37 '#attributes' => [
|
Chris@0
|
38 'class' => ['toolbar-icon', 'toolbar-icon-edit'],
|
Chris@0
|
39 'aria-pressed' => 'false',
|
Chris@0
|
40 'type' => 'button',
|
Chris@0
|
41 ],
|
Chris@0
|
42 ],
|
Chris@0
|
43 '#wrapper_attributes' => [
|
Chris@0
|
44 'class' => ['hidden', 'contextual-toolbar-tab'],
|
Chris@0
|
45 ],
|
Chris@0
|
46 '#attached' => [
|
Chris@0
|
47 'library' => [
|
Chris@0
|
48 'contextual/drupal.contextual-toolbar',
|
Chris@0
|
49 ],
|
Chris@0
|
50 ],
|
Chris@0
|
51 ];
|
Chris@0
|
52
|
Chris@0
|
53 return $items;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Implements hook_page_attachments().
|
Chris@0
|
58 *
|
Chris@0
|
59 * Adds the drupal.contextual-links library to the page for any user who has the
|
Chris@0
|
60 * 'access contextual links' permission.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @see contextual_preprocess()
|
Chris@0
|
63 */
|
Chris@0
|
64 function contextual_page_attachments(array &$page) {
|
Chris@0
|
65 if (!\Drupal::currentUser()->hasPermission('access contextual links')) {
|
Chris@0
|
66 return;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 $page['#attached']['library'][] = 'contextual/drupal.contextual-links';
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Implements hook_help().
|
Chris@0
|
74 */
|
Chris@0
|
75 function contextual_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
76 switch ($route_name) {
|
Chris@0
|
77 case 'help.page.contextual':
|
Chris@0
|
78 $output = '';
|
Chris@0
|
79 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@0
|
80 $output .= '<p>' . t('The Contextual links module gives users with the <em>Use contextual links</em> permission quick access to tasks associated with certain areas of pages on your site. For example, a menu displayed as a block has links to edit the menu and configure the block. For more information, see the <a href=":contextual">online documentation for the Contextual Links module</a>.', [':contextual' => 'https://www.drupal.org/documentation/modules/contextual']) . '</p>';
|
Chris@0
|
81 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
82 $output .= '<dl>';
|
Chris@0
|
83 $output .= '<dt>' . t('Displaying contextual links') . '</dt>';
|
Chris@0
|
84 $output .= '<dd>';
|
Chris@0
|
85 $output .= t('Contextual links for an area on a page are displayed using a contextual links button. There are two ways to make the contextual links button visible:');
|
Chris@0
|
86 $output .= '<ol>';
|
Chris@0
|
87 $sample_picture = [
|
Chris@0
|
88 '#theme' => 'image',
|
Chris@0
|
89 '#uri' => 'core/misc/icons/bebebe/pencil.svg',
|
Chris@17
|
90 '#alt' => t('contextual links button'),
|
Chris@0
|
91 ];
|
Chris@0
|
92 $sample_picture = \Drupal::service('renderer')->render($sample_picture);
|
Chris@0
|
93 $output .= '<li>' . t('Hovering over the area of interest will temporarily make the contextual links button visible (which looks like a pencil in most themes, and is normally displayed in the upper right corner of the area). The icon typically looks like this: @picture', ['@picture' => $sample_picture]) . '</li>';
|
Chris@18
|
94 $output .= '<li>' . t('If you have the <a href=":toolbar">Toolbar module</a> enabled, clicking the contextual links button in the toolbar (which looks like a pencil) will make all contextual links buttons on the page visible. Clicking this button again will toggle them to invisible.', [':toolbar' => (\Drupal::moduleHandler()->moduleExists('toolbar')) ? Url::fromRoute('help.page', ['name' => 'toolbar'])->toString() : '#']) . '</li>';
|
Chris@0
|
95 $output .= '</ol>';
|
Chris@0
|
96 $output .= t('Once the contextual links button for the area of interest is visible, click the button to display the links.');
|
Chris@0
|
97 $output .= '</dd>';
|
Chris@0
|
98 $output .= '</dl>';
|
Chris@0
|
99 return $output;
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Implements hook_preprocess().
|
Chris@0
|
105 *
|
Chris@0
|
106 * @see contextual_pre_render_placeholder()
|
Chris@0
|
107 * @see contextual_page_attachments()
|
Chris@0
|
108 * @see \Drupal\contextual\ContextualController::render()
|
Chris@0
|
109 */
|
Chris@0
|
110 function contextual_preprocess(&$variables, $hook, $info) {
|
Chris@0
|
111 $variables['#cache']['contexts'][] = 'user.permissions';
|
Chris@0
|
112 if (!\Drupal::currentUser()->hasPermission('access contextual links')) {
|
Chris@0
|
113 return;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 // Determine the primary theme function argument.
|
Chris@0
|
117 if (!empty($info['variables'])) {
|
Chris@0
|
118 $keys = array_keys($info['variables']);
|
Chris@0
|
119 $key = $keys[0];
|
Chris@0
|
120 }
|
Chris@0
|
121 elseif (!empty($info['render element'])) {
|
Chris@0
|
122 $key = $info['render element'];
|
Chris@0
|
123 }
|
Chris@0
|
124 if (!empty($key) && isset($variables[$key])) {
|
Chris@0
|
125 $element = $variables[$key];
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) {
|
Chris@0
|
129 // Mark this element as potentially having contextual links attached to it.
|
Chris@0
|
130 $variables['attributes']['class'][] = 'contextual-region';
|
Chris@0
|
131
|
Chris@0
|
132 // Renders a contextual links placeholder unconditionally, thus not breaking
|
Chris@0
|
133 // the render cache. Although the empty placeholder is rendered for all
|
Chris@0
|
134 // users, contextual_page_attachments() only adds the asset library for
|
Chris@0
|
135 // users with the 'access contextual links' permission, thus preventing
|
Chris@0
|
136 // unnecessary HTTP requests for users without that permission.
|
Chris@0
|
137 $variables['title_suffix']['contextual_links'] = [
|
Chris@0
|
138 '#type' => 'contextual_links_placeholder',
|
Chris@0
|
139 '#id' => _contextual_links_to_id($element['#contextual_links']),
|
Chris@0
|
140 ];
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * Implements hook_contextual_links_view_alter().
|
Chris@0
|
146 *
|
Chris@0
|
147 * @see \Drupal\contextual\Plugin\views\field\ContextualLinks::render()
|
Chris@0
|
148 */
|
Chris@0
|
149 function contextual_contextual_links_view_alter(&$element, $items) {
|
Chris@0
|
150 if (isset($element['#contextual_links']['contextual'])) {
|
Chris@0
|
151 $encoded_links = $element['#contextual_links']['contextual']['metadata']['contextual-views-field-links'];
|
Chris@0
|
152 $element['#links'] = Json::decode(rawurldecode($encoded_links));
|
Chris@0
|
153 }
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * Serializes #contextual_links property value array to a string.
|
Chris@0
|
158 *
|
Chris@0
|
159 * Examples:
|
Chris@0
|
160 * - node:node=1:langcode=en
|
Chris@0
|
161 * - views_ui_edit:view=frontpage:location=page&view_name=frontpage&view_display_id=page_1&langcode=en
|
Chris@0
|
162 * - menu:menu=tools:langcode=en|block:block=bartik.tools:langcode=en
|
Chris@0
|
163 *
|
Chris@0
|
164 * So, expressed in a pattern:
|
Chris@0
|
165 * <group>:<route parameters>:<metadata>
|
Chris@0
|
166 *
|
Chris@0
|
167 * The route parameters and options are encoded as query strings.
|
Chris@0
|
168 *
|
Chris@0
|
169 * @param array $contextual_links
|
Chris@0
|
170 * The $element['#contextual_links'] value for some render element.
|
Chris@0
|
171 *
|
Chris@0
|
172 * @return string
|
Chris@0
|
173 * A serialized representation of a #contextual_links property value array for
|
Chris@0
|
174 * use in a data- attribute.
|
Chris@0
|
175 */
|
Chris@0
|
176 function _contextual_links_to_id($contextual_links) {
|
Chris@0
|
177 $ids = [];
|
Chris@0
|
178 $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId();
|
Chris@0
|
179 foreach ($contextual_links as $group => $args) {
|
Chris@0
|
180 $route_parameters = UrlHelper::buildQuery($args['route_parameters']);
|
Chris@0
|
181 $args += ['metadata' => []];
|
Chris@0
|
182 // Add the current URL language to metadata so a different ID will be
|
Chris@0
|
183 // computed when URLs vary by language. This allows to store different
|
Chris@0
|
184 // language-aware contextual links on the client side.
|
Chris@0
|
185 $args['metadata'] += ['langcode' => $langcode];
|
Chris@0
|
186 $metadata = UrlHelper::buildQuery($args['metadata']);
|
Chris@0
|
187 $ids[] = "{$group}:{$route_parameters}:{$metadata}";
|
Chris@0
|
188 }
|
Chris@0
|
189 return implode('|', $ids);
|
Chris@0
|
190 }
|
Chris@0
|
191
|
Chris@0
|
192 /**
|
Chris@0
|
193 * Unserializes the result of _contextual_links_to_id().
|
Chris@0
|
194 *
|
Chris@17
|
195 * Note that $id is user input. Before calling this method the ID should be
|
Chris@17
|
196 * checked against the token stored in the 'data-contextual-token' attribute
|
Chris@17
|
197 * which is passed via the 'tokens' request parameter to
|
Chris@17
|
198 * \Drupal\contextual\ContextualController::render().
|
Chris@0
|
199 *
|
Chris@0
|
200 * @param string $id
|
Chris@0
|
201 * A serialized representation of a #contextual_links property value array.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @return array
|
Chris@0
|
204 * The value for a #contextual_links property.
|
Chris@17
|
205 *
|
Chris@17
|
206 * @see _contextual_links_to_id()
|
Chris@17
|
207 * @see \Drupal\contextual\ContextualController::render()
|
Chris@0
|
208 */
|
Chris@0
|
209 function _contextual_id_to_links($id) {
|
Chris@0
|
210 $contextual_links = [];
|
Chris@0
|
211 $contexts = explode('|', $id);
|
Chris@0
|
212 foreach ($contexts as $context) {
|
Chris@0
|
213 list($group, $route_parameters_raw, $metadata_raw) = explode(':', $context);
|
Chris@0
|
214 parse_str($route_parameters_raw, $route_parameters);
|
Chris@0
|
215 $metadata = [];
|
Chris@0
|
216 parse_str($metadata_raw, $metadata);
|
Chris@0
|
217 $contextual_links[$group] = [
|
Chris@0
|
218 'route_parameters' => $route_parameters,
|
Chris@0
|
219 'metadata' => $metadata,
|
Chris@0
|
220 ];
|
Chris@0
|
221 }
|
Chris@0
|
222 return $contextual_links;
|
Chris@0
|
223 }
|