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