Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * The theme system, which controls the output of Drupal.
|
Chris@0
|
6 *
|
Chris@0
|
7 * The theme system allows for nearly all output of the Drupal system to be
|
Chris@0
|
8 * customized by user themes.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 use Drupal\Component\Serialization\Json;
|
Chris@0
|
12 use Drupal\Component\Utility\Crypt;
|
Chris@0
|
13 use Drupal\Component\Utility\Html;
|
Chris@0
|
14 use Drupal\Component\Render\MarkupInterface;
|
Chris@0
|
15 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
16 use Drupal\Core\Cache\CacheableDependencyInterface;
|
Chris@0
|
17 use Drupal\Core\Config\Config;
|
Chris@0
|
18 use Drupal\Core\Config\StorageException;
|
Chris@0
|
19 use Drupal\Core\Render\AttachmentsInterface;
|
Chris@0
|
20 use Drupal\Core\Render\BubbleableMetadata;
|
Chris@0
|
21 use Drupal\Core\Render\RenderableInterface;
|
Chris@0
|
22 use Drupal\Core\Template\Attribute;
|
Chris@0
|
23 use Drupal\Core\Theme\ThemeSettings;
|
Chris@0
|
24 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
25 use Drupal\Core\Render\Element;
|
Chris@0
|
26 use Drupal\Core\Render\Markup;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * @defgroup content_flags Content markers
|
Chris@0
|
30 * @{
|
Chris@0
|
31 * Markers used by mark.html.twig and node_mark() to designate content.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @see mark.html.twig
|
Chris@0
|
34 * @see node_mark()
|
Chris@0
|
35 */
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Mark content as read.
|
Chris@0
|
39 */
|
Chris@0
|
40 const MARK_READ = 0;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Mark content as being new.
|
Chris@0
|
44 */
|
Chris@0
|
45 const MARK_NEW = 1;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Mark content as being updated.
|
Chris@0
|
49 */
|
Chris@0
|
50 const MARK_UPDATED = 2;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * A responsive table class; hide table cell on narrow devices.
|
Chris@0
|
54 *
|
Chris@0
|
55 * Indicates that a column has medium priority and thus can be hidden on narrow
|
Chris@0
|
56 * width devices and shown on medium+ width devices (i.e. tablets and desktops).
|
Chris@0
|
57 */
|
Chris@0
|
58 const RESPONSIVE_PRIORITY_MEDIUM = 'priority-medium';
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * A responsive table class; only show table cell on wide devices.
|
Chris@0
|
62 *
|
Chris@0
|
63 * Indicates that a column has low priority and thus can be hidden on narrow
|
Chris@0
|
64 * and medium viewports and shown on wide devices (i.e. desktops).
|
Chris@0
|
65 */
|
Chris@0
|
66 const RESPONSIVE_PRIORITY_LOW = 'priority-low';
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * @} End of "defgroup content_flags".
|
Chris@0
|
70 */
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Gets the theme registry.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @param bool $complete
|
Chris@0
|
76 * Optional boolean to indicate whether to return the complete theme registry
|
Chris@0
|
77 * array or an instance of the Drupal\Core\Utility\ThemeRegistry class.
|
Chris@0
|
78 * If TRUE, the complete theme registry array will be returned. This is useful
|
Chris@0
|
79 * if you want to foreach over the whole registry, use array_* functions or
|
Chris@0
|
80 * inspect it in a debugger. If FALSE, an instance of the
|
Chris@0
|
81 * Drupal\Core\Utility\ThemeRegistry class will be returned, this provides an
|
Chris@0
|
82 * ArrayObject which allows it to be accessed with array syntax and isset(),
|
Chris@0
|
83 * and should be more lightweight than the full registry. Defaults to TRUE.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return
|
Chris@0
|
86 * The complete theme registry array, or an instance of the
|
Chris@0
|
87 * Drupal\Core\Utility\ThemeRegistry class.
|
Chris@0
|
88 */
|
Chris@0
|
89 function theme_get_registry($complete = TRUE) {
|
Chris@0
|
90 $theme_registry = \Drupal::service('theme.registry');
|
Chris@0
|
91 if ($complete) {
|
Chris@0
|
92 return $theme_registry->get();
|
Chris@0
|
93 }
|
Chris@0
|
94 else {
|
Chris@0
|
95 return $theme_registry->getRuntime();
|
Chris@0
|
96 }
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * Returns an array of default theme features.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @see \Drupal\Core\Extension\ThemeHandler::$defaultFeatures
|
Chris@0
|
103 */
|
Chris@0
|
104 function _system_default_theme_features() {
|
Chris@0
|
105 return [
|
Chris@0
|
106 'favicon',
|
Chris@0
|
107 'logo',
|
Chris@0
|
108 'node_user_picture',
|
Chris@0
|
109 'comment_user_picture',
|
Chris@0
|
110 'comment_user_verification',
|
Chris@0
|
111 ];
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * Forces the system to rebuild the theme registry.
|
Chris@0
|
116 *
|
Chris@0
|
117 * This function should be called when modules are added to the system, or when
|
Chris@0
|
118 * a dynamic system needs to add more theme hooks.
|
Chris@0
|
119 */
|
Chris@0
|
120 function drupal_theme_rebuild() {
|
Chris@0
|
121 \Drupal::service('theme.registry')->reset();
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * Allows themes and/or theme engines to discover overridden theme functions.
|
Chris@0
|
126 *
|
Chris@0
|
127 * @param array $cache
|
Chris@0
|
128 * The existing cache of theme hooks to test against.
|
Chris@0
|
129 * @param array $prefixes
|
Chris@0
|
130 * An array of prefixes to test, in reverse order of importance.
|
Chris@0
|
131 *
|
Chris@0
|
132 * @return array
|
Chris@0
|
133 * The functions found, suitable for returning from hook_theme;
|
Chris@0
|
134 */
|
Chris@0
|
135 function drupal_find_theme_functions($cache, $prefixes) {
|
Chris@0
|
136 $implementations = [];
|
Chris@0
|
137 $grouped_functions = \Drupal::service('theme.registry')->getPrefixGroupedUserFunctions($prefixes);
|
Chris@0
|
138
|
Chris@0
|
139 foreach ($cache as $hook => $info) {
|
Chris@0
|
140 foreach ($prefixes as $prefix) {
|
Chris@0
|
141 // Find theme functions that implement possible "suggestion" variants of
|
Chris@0
|
142 // registered theme hooks and add those as new registered theme hooks.
|
Chris@0
|
143 // The 'pattern' key defines a common prefix that all suggestions must
|
Chris@0
|
144 // start with. The default is the name of the hook followed by '__'. An
|
Chris@0
|
145 // 'base hook' key is added to each entry made for a found suggestion,
|
Chris@0
|
146 // so that common functionality can be implemented for all suggestions of
|
Chris@0
|
147 // the same base hook. To keep things simple, deep hierarchy of
|
Chris@0
|
148 // suggestions is not supported: each suggestion's 'base hook' key
|
Chris@0
|
149 // refers to a base hook, not to another suggestion, and all suggestions
|
Chris@0
|
150 // are found using the base hook's pattern, not a pattern from an
|
Chris@0
|
151 // intermediary suggestion.
|
Chris@0
|
152 $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
|
Chris@0
|
153 // Grep only the functions which are within the prefix group.
|
Chris@0
|
154 list($first_prefix,) = explode('_', $prefix, 2);
|
Chris@0
|
155 if (!isset($info['base hook']) && !empty($pattern) && isset($grouped_functions[$first_prefix])) {
|
Chris@0
|
156 $matches = preg_grep('/^' . $prefix . '_' . $pattern . '/', $grouped_functions[$first_prefix]);
|
Chris@0
|
157 if ($matches) {
|
Chris@0
|
158 foreach ($matches as $match) {
|
Chris@0
|
159 $new_hook = substr($match, strlen($prefix) + 1);
|
Chris@0
|
160 $arg_name = isset($info['variables']) ? 'variables' : 'render element';
|
Chris@0
|
161 $implementations[$new_hook] = [
|
Chris@0
|
162 'function' => $match,
|
Chris@0
|
163 $arg_name => $info[$arg_name],
|
Chris@0
|
164 'base hook' => $hook,
|
Chris@0
|
165 ];
|
Chris@0
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169 // Find theme functions that implement registered theme hooks and include
|
Chris@0
|
170 // that in what is returned so that the registry knows that the theme has
|
Chris@0
|
171 // this implementation.
|
Chris@0
|
172 if (function_exists($prefix . '_' . $hook)) {
|
Chris@0
|
173 $implementations[$hook] = [
|
Chris@0
|
174 'function' => $prefix . '_' . $hook,
|
Chris@0
|
175 ];
|
Chris@0
|
176 }
|
Chris@0
|
177 }
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 return $implementations;
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Allows themes and/or theme engines to easily discover overridden templates.
|
Chris@0
|
185 *
|
Chris@0
|
186 * @param $cache
|
Chris@0
|
187 * The existing cache of theme hooks to test against.
|
Chris@0
|
188 * @param $extension
|
Chris@0
|
189 * The extension that these templates will have.
|
Chris@0
|
190 * @param $path
|
Chris@0
|
191 * The path to search.
|
Chris@0
|
192 */
|
Chris@0
|
193 function drupal_find_theme_templates($cache, $extension, $path) {
|
Chris@0
|
194 $implementations = [];
|
Chris@0
|
195
|
Chris@0
|
196 // Collect paths to all sub-themes grouped by base themes. These will be
|
Chris@0
|
197 // used for filtering. This allows base themes to have sub-themes in its
|
Chris@0
|
198 // folder hierarchy without affecting the base themes template discovery.
|
Chris@0
|
199 $theme_paths = [];
|
Chris@0
|
200 foreach (\Drupal::service('theme_handler')->listInfo() as $theme_info) {
|
Chris@0
|
201 if (!empty($theme_info->base_theme)) {
|
Chris@0
|
202 $theme_paths[$theme_info->base_theme][$theme_info->getName()] = $theme_info->getPath();
|
Chris@0
|
203 }
|
Chris@0
|
204 }
|
Chris@0
|
205 foreach ($theme_paths as $basetheme => $subthemes) {
|
Chris@0
|
206 foreach ($subthemes as $subtheme => $subtheme_path) {
|
Chris@0
|
207 if (isset($theme_paths[$subtheme])) {
|
Chris@0
|
208 $theme_paths[$basetheme] = array_merge($theme_paths[$basetheme], $theme_paths[$subtheme]);
|
Chris@0
|
209 }
|
Chris@0
|
210 }
|
Chris@0
|
211 }
|
Chris@0
|
212 $theme = \Drupal::theme()->getActiveTheme()->getName();
|
Chris@0
|
213 $subtheme_paths = isset($theme_paths[$theme]) ? $theme_paths[$theme] : [];
|
Chris@0
|
214
|
Chris@0
|
215 // Escape the periods in the extension.
|
Chris@0
|
216 $regex = '/' . str_replace('.', '\.', $extension) . '$/';
|
Chris@0
|
217 // Get a listing of all template files in the path to search.
|
Chris@0
|
218 $files = file_scan_directory($path, $regex, ['key' => 'filename']);
|
Chris@0
|
219
|
Chris@0
|
220 // Find templates that implement registered theme hooks and include that in
|
Chris@0
|
221 // what is returned so that the registry knows that the theme has this
|
Chris@0
|
222 // implementation.
|
Chris@0
|
223 foreach ($files as $template => $file) {
|
Chris@0
|
224 // Ignore sub-theme templates for the current theme.
|
Chris@0
|
225 if (strpos($file->uri, str_replace($subtheme_paths, '', $file->uri)) !== 0) {
|
Chris@0
|
226 continue;
|
Chris@0
|
227 }
|
Chris@0
|
228 // Remove the extension from the filename.
|
Chris@0
|
229 $template = str_replace($extension, '', $template);
|
Chris@0
|
230 // Transform - in filenames to _ to match function naming scheme
|
Chris@0
|
231 // for the purposes of searching.
|
Chris@0
|
232 $hook = strtr($template, '-', '_');
|
Chris@0
|
233 if (isset($cache[$hook])) {
|
Chris@0
|
234 $implementations[$hook] = [
|
Chris@0
|
235 'template' => $template,
|
Chris@0
|
236 'path' => dirname($file->uri),
|
Chris@0
|
237 ];
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 // Match templates based on the 'template' filename.
|
Chris@0
|
241 foreach ($cache as $hook => $info) {
|
Chris@0
|
242 if (isset($info['template'])) {
|
Chris@0
|
243 if ($template === $info['template']) {
|
Chris@0
|
244 $implementations[$hook] = [
|
Chris@0
|
245 'template' => $template,
|
Chris@0
|
246 'path' => dirname($file->uri),
|
Chris@0
|
247 ];
|
Chris@0
|
248 }
|
Chris@0
|
249 }
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 // Find templates that implement possible "suggestion" variants of registered
|
Chris@0
|
254 // theme hooks and add those as new registered theme hooks. See
|
Chris@0
|
255 // drupal_find_theme_functions() for more information about suggestions and
|
Chris@0
|
256 // the use of 'pattern' and 'base hook'.
|
Chris@0
|
257 $patterns = array_keys($files);
|
Chris@0
|
258 foreach ($cache as $hook => $info) {
|
Chris@0
|
259 $pattern = isset($info['pattern']) ? $info['pattern'] : ($hook . '__');
|
Chris@0
|
260 if (!isset($info['base hook']) && !empty($pattern)) {
|
Chris@0
|
261 // Transform _ in pattern to - to match file naming scheme
|
Chris@0
|
262 // for the purposes of searching.
|
Chris@0
|
263 $pattern = strtr($pattern, '_', '-');
|
Chris@0
|
264
|
Chris@0
|
265 $matches = preg_grep('/^' . $pattern . '/', $patterns);
|
Chris@0
|
266 if ($matches) {
|
Chris@0
|
267 foreach ($matches as $match) {
|
Chris@0
|
268 $file = $match;
|
Chris@0
|
269 // Remove the extension from the filename.
|
Chris@0
|
270 $file = str_replace($extension, '', $file);
|
Chris@0
|
271 // Put the underscores back in for the hook name and register this
|
Chris@0
|
272 // pattern.
|
Chris@0
|
273 $arg_name = isset($info['variables']) ? 'variables' : 'render element';
|
Chris@0
|
274 $implementations[strtr($file, '-', '_')] = [
|
Chris@0
|
275 'template' => $file,
|
Chris@0
|
276 'path' => dirname($files[$match]->uri),
|
Chris@0
|
277 $arg_name => $info[$arg_name],
|
Chris@0
|
278 'base hook' => $hook,
|
Chris@0
|
279 ];
|
Chris@0
|
280 }
|
Chris@0
|
281 }
|
Chris@0
|
282 }
|
Chris@0
|
283 }
|
Chris@0
|
284 return $implementations;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Retrieves a setting for the current theme or for a given theme.
|
Chris@0
|
289 *
|
Chris@0
|
290 * The final setting is obtained from the last value found in the following
|
Chris@0
|
291 * sources:
|
Chris@0
|
292 * - the saved values from the global theme settings form
|
Chris@0
|
293 * - the saved values from the theme's settings form
|
Chris@0
|
294 * To only retrieve the default global theme setting, an empty string should be
|
Chris@0
|
295 * given for $theme.
|
Chris@0
|
296 *
|
Chris@0
|
297 * @param $setting_name
|
Chris@0
|
298 * The name of the setting to be retrieved.
|
Chris@0
|
299 * @param $theme
|
Chris@0
|
300 * The name of a given theme; defaults to the current theme.
|
Chris@0
|
301 *
|
Chris@0
|
302 * @return
|
Chris@0
|
303 * The value of the requested setting, NULL if the setting does not exist.
|
Chris@0
|
304 */
|
Chris@0
|
305 function theme_get_setting($setting_name, $theme = NULL) {
|
Chris@0
|
306 /** @var \Drupal\Core\Theme\ThemeSettings[] $cache */
|
Chris@0
|
307 $cache = &drupal_static(__FUNCTION__, []);
|
Chris@0
|
308
|
Chris@0
|
309 // If no key is given, use the current theme if we can determine it.
|
Chris@0
|
310 if (!isset($theme)) {
|
Chris@0
|
311 $theme = \Drupal::theme()->getActiveTheme()->getName();
|
Chris@0
|
312 }
|
Chris@0
|
313
|
Chris@0
|
314 if (empty($cache[$theme])) {
|
Chris@0
|
315 // Create a theme settings object.
|
Chris@0
|
316 $cache[$theme] = new ThemeSettings($theme);
|
Chris@0
|
317 // Get the global settings from configuration.
|
Chris@0
|
318 $cache[$theme]->setData(\Drupal::config('system.theme.global')->get());
|
Chris@0
|
319
|
Chris@0
|
320 // Get the values for the theme-specific settings from the .info.yml files
|
Chris@0
|
321 // of the theme and all its base themes.
|
Chris@0
|
322 $themes = \Drupal::service('theme_handler')->listInfo();
|
Chris@0
|
323 if (isset($themes[$theme])) {
|
Chris@0
|
324 $theme_object = $themes[$theme];
|
Chris@0
|
325
|
Chris@0
|
326 // Retrieve configured theme-specific settings, if any.
|
Chris@0
|
327 try {
|
Chris@0
|
328 if ($theme_settings = \Drupal::config($theme . '.settings')->get()) {
|
Chris@0
|
329 $cache[$theme]->merge($theme_settings);
|
Chris@0
|
330 }
|
Chris@0
|
331 }
|
Chris@0
|
332 catch (StorageException $e) {
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 // If the theme does not support a particular feature, override the global
|
Chris@0
|
336 // setting and set the value to NULL.
|
Chris@0
|
337 if (!empty($theme_object->info['features'])) {
|
Chris@0
|
338 foreach (_system_default_theme_features() as $feature) {
|
Chris@0
|
339 if (!in_array($feature, $theme_object->info['features'])) {
|
Chris@0
|
340 $cache[$theme]->set('features.' . $feature, NULL);
|
Chris@0
|
341 }
|
Chris@0
|
342 }
|
Chris@0
|
343 }
|
Chris@0
|
344
|
Chris@0
|
345 // Generate the path to the logo image.
|
Chris@0
|
346 if ($cache[$theme]->get('logo.use_default')) {
|
Chris@0
|
347 $cache[$theme]->set('logo.url', file_url_transform_relative(file_create_url($theme_object->getPath() . '/logo.svg')));
|
Chris@0
|
348 }
|
Chris@0
|
349 elseif ($logo_path = $cache[$theme]->get('logo.path')) {
|
Chris@0
|
350 $cache[$theme]->set('logo.url', file_url_transform_relative(file_create_url($logo_path)));
|
Chris@0
|
351 }
|
Chris@0
|
352
|
Chris@0
|
353 // Generate the path to the favicon.
|
Chris@0
|
354 if ($cache[$theme]->get('features.favicon')) {
|
Chris@0
|
355 $favicon_path = $cache[$theme]->get('favicon.path');
|
Chris@0
|
356 if ($cache[$theme]->get('favicon.use_default')) {
|
Chris@0
|
357 if (file_exists($favicon = $theme_object->getPath() . '/favicon.ico')) {
|
Chris@0
|
358 $cache[$theme]->set('favicon.url', file_url_transform_relative(file_create_url($favicon)));
|
Chris@0
|
359 }
|
Chris@0
|
360 else {
|
Chris@0
|
361 $cache[$theme]->set('favicon.url', file_url_transform_relative(file_create_url('core/misc/favicon.ico')));
|
Chris@0
|
362 }
|
Chris@0
|
363 }
|
Chris@0
|
364 elseif ($favicon_path) {
|
Chris@0
|
365 $cache[$theme]->set('favicon.url', file_url_transform_relative(file_create_url($favicon_path)));
|
Chris@0
|
366 }
|
Chris@0
|
367 else {
|
Chris@0
|
368 $cache[$theme]->set('features.favicon', FALSE);
|
Chris@0
|
369 }
|
Chris@0
|
370 }
|
Chris@0
|
371 }
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 return $cache[$theme]->get($setting_name);
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 /**
|
Chris@0
|
378 * Escapes and renders variables for theme functions.
|
Chris@0
|
379 *
|
Chris@0
|
380 * This method is used in theme functions to ensure that the result is safe for
|
Chris@0
|
381 * output inside HTML fragments. This mimics the behavior of the auto-escape
|
Chris@0
|
382 * functionality in Twig.
|
Chris@0
|
383 *
|
Chris@0
|
384 * Note: This function should be kept in sync with
|
Chris@0
|
385 * \Drupal\Core\Template\TwigExtension::escapeFilter().
|
Chris@0
|
386 *
|
Chris@0
|
387 * @param mixed $arg
|
Chris@0
|
388 * The string, object, or render array to escape if needed.
|
Chris@0
|
389 *
|
Chris@0
|
390 * @return string
|
Chris@0
|
391 * The rendered string, safe for use in HTML. The string is not safe when used
|
Chris@0
|
392 * as any part of an HTML attribute name or value.
|
Chris@0
|
393 *
|
Chris@0
|
394 * @throws \Exception
|
Chris@0
|
395 * Thrown when an object is passed in which cannot be printed.
|
Chris@0
|
396 *
|
Chris@0
|
397 * @see \Drupal\Core\Template\TwigExtension::escapeFilter()
|
Chris@0
|
398 *
|
Chris@0
|
399 * @todo Discuss deprecating this in https://www.drupal.org/node/2575081.
|
Chris@0
|
400 * @todo Refactor this to keep it in sync with Twig filtering in
|
Chris@0
|
401 * https://www.drupal.org/node/2575065
|
Chris@0
|
402 */
|
Chris@0
|
403 function theme_render_and_autoescape($arg) {
|
Chris@0
|
404 // If it's a renderable, then it'll be up to the generated render array it
|
Chris@0
|
405 // returns to contain the necessary cacheability & attachment metadata. If
|
Chris@0
|
406 // it doesn't implement CacheableDependencyInterface or AttachmentsInterface
|
Chris@0
|
407 // then there is nothing to do here.
|
Chris@0
|
408 if (!($arg instanceof RenderableInterface) && ($arg instanceof CacheableDependencyInterface || $arg instanceof AttachmentsInterface)) {
|
Chris@0
|
409 $arg_bubbleable = [];
|
Chris@0
|
410 BubbleableMetadata::createFromObject($arg)
|
Chris@0
|
411 ->applyTo($arg_bubbleable);
|
Chris@0
|
412 \Drupal::service('renderer')->render($arg_bubbleable);
|
Chris@0
|
413 }
|
Chris@0
|
414
|
Chris@0
|
415 if ($arg instanceof MarkupInterface) {
|
Chris@0
|
416 return (string) $arg;
|
Chris@0
|
417 }
|
Chris@0
|
418 $return = NULL;
|
Chris@0
|
419
|
Chris@0
|
420 if (is_scalar($arg)) {
|
Chris@0
|
421 $return = (string) $arg;
|
Chris@0
|
422 }
|
Chris@0
|
423 elseif (is_object($arg)) {
|
Chris@0
|
424 if ($arg instanceof RenderableInterface) {
|
Chris@0
|
425 $arg = $arg->toRenderable();
|
Chris@0
|
426 }
|
Chris@0
|
427 elseif (method_exists($arg, '__toString')) {
|
Chris@0
|
428 $return = (string) $arg;
|
Chris@0
|
429 }
|
Chris@0
|
430 // You can't throw exceptions in the magic PHP __toString methods, see
|
Chris@0
|
431 // http://php.net/manual/language.oop5.magic.php#object.tostring so
|
Chris@0
|
432 // we also support a toString method.
|
Chris@0
|
433 elseif (method_exists($arg, 'toString')) {
|
Chris@0
|
434 $return = $arg->toString();
|
Chris@0
|
435 }
|
Chris@0
|
436 else {
|
Chris@0
|
437 throw new \Exception('Object of type ' . get_class($arg) . ' cannot be printed.');
|
Chris@0
|
438 }
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 // We have a string or an object converted to a string: Escape it!
|
Chris@0
|
442 if (isset($return)) {
|
Chris@0
|
443 return $return instanceof MarkupInterface ? $return : Html::escape($return);
|
Chris@0
|
444 }
|
Chris@0
|
445
|
Chris@0
|
446 // This is a normal render array, which is safe by definition, with special
|
Chris@0
|
447 // simple cases already handled.
|
Chris@0
|
448
|
Chris@0
|
449 // Early return if this element was pre-rendered (no need to re-render).
|
Chris@0
|
450 if (isset($arg['#printed']) && $arg['#printed'] == TRUE && isset($arg['#markup']) && strlen($arg['#markup']) > 0) {
|
Chris@0
|
451 return (string) $arg['#markup'];
|
Chris@0
|
452 }
|
Chris@0
|
453 $arg['#printed'] = FALSE;
|
Chris@0
|
454 return (string) \Drupal::service('renderer')->render($arg);
|
Chris@0
|
455 }
|
Chris@0
|
456
|
Chris@0
|
457 /**
|
Chris@0
|
458 * Converts theme settings to configuration.
|
Chris@0
|
459 *
|
Chris@0
|
460 * @see system_theme_settings_submit()
|
Chris@0
|
461 *
|
Chris@0
|
462 * @param array $theme_settings
|
Chris@0
|
463 * An array of theme settings from system setting form or a Drupal 7 variable.
|
Chris@0
|
464 * @param Config $config
|
Chris@0
|
465 * The configuration object to update.
|
Chris@0
|
466 *
|
Chris@0
|
467 * @return
|
Chris@0
|
468 * The Config object with updated data.
|
Chris@0
|
469 */
|
Chris@0
|
470 function theme_settings_convert_to_config(array $theme_settings, Config $config) {
|
Chris@0
|
471 foreach ($theme_settings as $key => $value) {
|
Chris@0
|
472 if ($key == 'default_logo') {
|
Chris@0
|
473 $config->set('logo.use_default', $value);
|
Chris@0
|
474 }
|
Chris@0
|
475 elseif ($key == 'logo_path') {
|
Chris@0
|
476 $config->set('logo.path', $value);
|
Chris@0
|
477 }
|
Chris@0
|
478 elseif ($key == 'default_favicon') {
|
Chris@0
|
479 $config->set('favicon.use_default', $value);
|
Chris@0
|
480 }
|
Chris@0
|
481 elseif ($key == 'favicon_path') {
|
Chris@0
|
482 $config->set('favicon.path', $value);
|
Chris@0
|
483 }
|
Chris@0
|
484 elseif ($key == 'favicon_mimetype') {
|
Chris@0
|
485 $config->set('favicon.mimetype', $value);
|
Chris@0
|
486 }
|
Chris@0
|
487 elseif (substr($key, 0, 7) == 'toggle_') {
|
Chris@0
|
488 $config->set('features.' . Unicode::substr($key, 7), $value);
|
Chris@0
|
489 }
|
Chris@0
|
490 elseif (!in_array($key, ['theme', 'logo_upload'])) {
|
Chris@0
|
491 $config->set($key, $value);
|
Chris@0
|
492 }
|
Chris@0
|
493 }
|
Chris@0
|
494 return $config;
|
Chris@0
|
495 }
|
Chris@0
|
496
|
Chris@0
|
497 /**
|
Chris@0
|
498 * Prepares variables for time templates.
|
Chris@0
|
499 *
|
Chris@0
|
500 * Default template: time.html.twig.
|
Chris@0
|
501 *
|
Chris@0
|
502 * @param array $variables
|
Chris@0
|
503 * An associative array possibly containing:
|
Chris@0
|
504 * - attributes['timestamp']:
|
Chris@0
|
505 * - timestamp:
|
Chris@0
|
506 * - text:
|
Chris@0
|
507 */
|
Chris@0
|
508 function template_preprocess_time(&$variables) {
|
Chris@0
|
509 // Format the 'datetime' attribute based on the timestamp.
|
Chris@0
|
510 // @see http://www.w3.org/TR/html5-author/the-time-element.html#attr-time-datetime
|
Chris@0
|
511 if (!isset($variables['attributes']['datetime']) && isset($variables['timestamp'])) {
|
Chris@0
|
512 $variables['attributes']['datetime'] = format_date($variables['timestamp'], 'html_datetime', '', 'UTC');
|
Chris@0
|
513 }
|
Chris@0
|
514
|
Chris@0
|
515 // If no text was provided, try to auto-generate it.
|
Chris@0
|
516 if (!isset($variables['text'])) {
|
Chris@0
|
517 // Format and use a human-readable version of the timestamp, if any.
|
Chris@0
|
518 if (isset($variables['timestamp'])) {
|
Chris@0
|
519 $variables['text'] = format_date($variables['timestamp']);
|
Chris@0
|
520 }
|
Chris@0
|
521 // Otherwise, use the literal datetime attribute.
|
Chris@0
|
522 elseif (isset($variables['attributes']['datetime'])) {
|
Chris@0
|
523 $variables['text'] = $variables['attributes']['datetime'];
|
Chris@0
|
524 }
|
Chris@0
|
525 }
|
Chris@0
|
526 }
|
Chris@0
|
527
|
Chris@0
|
528 /**
|
Chris@0
|
529 * Prepares variables for datetime form element templates.
|
Chris@0
|
530 *
|
Chris@0
|
531 * The datetime form element serves as a wrapper around the date element type,
|
Chris@0
|
532 * which creates a date and a time component for a date.
|
Chris@0
|
533 *
|
Chris@0
|
534 * Default template: datetime-form.html.twig.
|
Chris@0
|
535 *
|
Chris@0
|
536 * @param array $variables
|
Chris@0
|
537 * An associative array containing:
|
Chris@0
|
538 * - element: An associative array containing the properties of the element.
|
Chris@0
|
539 * Properties used: #title, #value, #options, #description, #required,
|
Chris@0
|
540 * #attributes.
|
Chris@0
|
541 *
|
Chris@0
|
542 * @see form_process_datetime()
|
Chris@0
|
543 */
|
Chris@0
|
544 function template_preprocess_datetime_form(&$variables) {
|
Chris@0
|
545 $element = $variables['element'];
|
Chris@0
|
546
|
Chris@0
|
547 $variables['attributes'] = [];
|
Chris@0
|
548 if (isset($element['#id'])) {
|
Chris@0
|
549 $variables['attributes']['id'] = $element['#id'];
|
Chris@0
|
550 }
|
Chris@0
|
551 if (!empty($element['#attributes']['class'])) {
|
Chris@0
|
552 $variables['attributes']['class'] = (array) $element['#attributes']['class'];
|
Chris@0
|
553 }
|
Chris@0
|
554
|
Chris@0
|
555 $variables['content'] = $element;
|
Chris@0
|
556 }
|
Chris@0
|
557
|
Chris@0
|
558 /**
|
Chris@0
|
559 * Prepares variables for datetime form wrapper templates.
|
Chris@0
|
560 *
|
Chris@0
|
561 * Default template: datetime-wrapper.html.twig.
|
Chris@0
|
562 *
|
Chris@0
|
563 * @param array $variables
|
Chris@0
|
564 * An associative array containing:
|
Chris@0
|
565 * - element: An associative array containing the properties of the element.
|
Chris@0
|
566 * Properties used: #title, #children, #required, #attributes.
|
Chris@0
|
567 */
|
Chris@0
|
568 function template_preprocess_datetime_wrapper(&$variables) {
|
Chris@0
|
569 $element = $variables['element'];
|
Chris@0
|
570
|
Chris@0
|
571 if (!empty($element['#title'])) {
|
Chris@0
|
572 $variables['title'] = $element['#title'];
|
Chris@0
|
573 }
|
Chris@0
|
574
|
Chris@0
|
575 // Suppress error messages.
|
Chris@0
|
576 $variables['errors'] = NULL;
|
Chris@0
|
577
|
Chris@0
|
578 $variables['description'] = NULL;
|
Chris@0
|
579 if (!empty($element['#description'])) {
|
Chris@0
|
580 $description_attributes = [];
|
Chris@0
|
581 if (!empty($element['#id'])) {
|
Chris@0
|
582 $description_attributes['id'] = $element['#id'] . '--description';
|
Chris@0
|
583 }
|
Chris@0
|
584 $variables['description'] = $element['#description'];
|
Chris@0
|
585 $variables['description_attributes'] = new Attribute($description_attributes);
|
Chris@0
|
586 }
|
Chris@0
|
587
|
Chris@0
|
588 $variables['required'] = FALSE;
|
Chris@0
|
589 // For required datetime fields 'form-required' & 'js-form-required' classes
|
Chris@0
|
590 // are appended to the label attributes.
|
Chris@0
|
591 if (!empty($element['#required'])) {
|
Chris@0
|
592 $variables['required'] = TRUE;
|
Chris@0
|
593 }
|
Chris@0
|
594 $variables['content'] = $element['#children'];
|
Chris@0
|
595 }
|
Chris@0
|
596
|
Chris@0
|
597 /**
|
Chris@0
|
598 * Prepares variables for links templates.
|
Chris@0
|
599 *
|
Chris@0
|
600 * Default template: links.html.twig.
|
Chris@0
|
601 *
|
Chris@0
|
602 * Unfortunately links templates duplicate the "active" class handling of l()
|
Chris@0
|
603 * and LinkGenerator::generate() because it needs to be able to set the "active"
|
Chris@0
|
604 * class not on the links themselves (<a> tags), but on the list items (<li>
|
Chris@0
|
605 * tags) that contain the links. This is necessary for CSS to be able to style
|
Chris@0
|
606 * list items differently when the link is active, since CSS does not yet allow
|
Chris@0
|
607 * one to style list items only if it contains a certain element with a certain
|
Chris@0
|
608 * class. I.e. we cannot yet convert this jQuery selector to a CSS selector:
|
Chris@0
|
609 * jQuery('li:has("a.is-active")')
|
Chris@0
|
610 *
|
Chris@0
|
611 * @param array $variables
|
Chris@0
|
612 * An associative array containing:
|
Chris@0
|
613 * - links: An array of links to be themed. Each link should be itself an
|
Chris@0
|
614 * array, with the following elements:
|
Chris@0
|
615 * - title: The link text.
|
Chris@0
|
616 * - url: (optional) The \Drupal\Core\Url object to link to. If omitted, no
|
Chris@0
|
617 * anchor tag is printed out.
|
Chris@0
|
618 * - attributes: (optional) Attributes for the anchor, or for the <span>
|
Chris@0
|
619 * tag used in its place if no 'href' is supplied. If element 'class' is
|
Chris@0
|
620 * included, it must be an array of one or more class names.
|
Chris@0
|
621 * If the 'href' element is supplied, the entire link array is passed to
|
Chris@0
|
622 * l() as its $options parameter.
|
Chris@0
|
623 * - attributes: A keyed array of attributes for the <ul> containing the list
|
Chris@0
|
624 * of links.
|
Chris@0
|
625 * - set_active_class: (optional) Whether each link should compare the
|
Chris@0
|
626 * route_name + route_parameters or href (path), language and query options
|
Chris@0
|
627 * to the current URL, to determine whether the link is "active". If so, an
|
Chris@0
|
628 * "active" class will be applied to the list item containing the link, as
|
Chris@0
|
629 * well as the link itself. It is important to use this sparingly since it
|
Chris@0
|
630 * is usually unnecessary and requires extra processing.
|
Chris@0
|
631 * For anonymous users, the "active" class will be calculated on the server,
|
Chris@0
|
632 * because most sites serve each anonymous user the same cached page anyway.
|
Chris@0
|
633 * For authenticated users, the "active" class will be calculated on the
|
Chris@0
|
634 * client (through JavaScript), only data- attributes are added to list
|
Chris@0
|
635 * items and contained links, to prevent breaking the render cache. The
|
Chris@0
|
636 * JavaScript is added in system_page_attachments().
|
Chris@0
|
637 * - heading: (optional) A heading to precede the links. May be an
|
Chris@0
|
638 * associative array or a string. If it's an array, it can have the
|
Chris@0
|
639 * following elements:
|
Chris@0
|
640 * - text: The heading text.
|
Chris@0
|
641 * - level: The heading level (e.g. 'h2', 'h3').
|
Chris@0
|
642 * - attributes: (optional) An array of the CSS attributes for the heading.
|
Chris@0
|
643 * When using a string it will be used as the text of the heading and the
|
Chris@0
|
644 * level will default to 'h2'. Headings should be used on navigation menus
|
Chris@0
|
645 * and any list of links that consistently appears on multiple pages. To
|
Chris@0
|
646 * make the heading invisible use the 'visually-hidden' CSS class. Do not
|
Chris@0
|
647 * use 'display:none', which removes it from screen readers and assistive
|
Chris@0
|
648 * technology. Headings allow screen reader and keyboard only users to
|
Chris@0
|
649 * navigate to or skip the links. See
|
Chris@0
|
650 * http://juicystudio.com/article/screen-readers-display-none.php and
|
Chris@0
|
651 * http://www.w3.org/TR/WCAG-TECHS/H42.html for more information.
|
Chris@0
|
652 *
|
Chris@0
|
653 * @see \Drupal\Core\Utility\LinkGenerator
|
Chris@0
|
654 * @see \Drupal\Core\Utility\LinkGenerator::generate()
|
Chris@0
|
655 * @see system_page_attachments()
|
Chris@0
|
656 */
|
Chris@0
|
657 function template_preprocess_links(&$variables) {
|
Chris@0
|
658 $links = $variables['links'];
|
Chris@0
|
659 $heading = &$variables['heading'];
|
Chris@0
|
660
|
Chris@0
|
661 if (!empty($links)) {
|
Chris@0
|
662 // Prepend the heading to the list, if any.
|
Chris@0
|
663 if (!empty($heading)) {
|
Chris@0
|
664 // Convert a string heading into an array, using a <h2> tag by default.
|
Chris@0
|
665 if (is_string($heading)) {
|
Chris@0
|
666 $heading = ['text' => $heading];
|
Chris@0
|
667 }
|
Chris@0
|
668 // Merge in default array properties into $heading.
|
Chris@0
|
669 $heading += [
|
Chris@0
|
670 'level' => 'h2',
|
Chris@0
|
671 'attributes' => [],
|
Chris@0
|
672 ];
|
Chris@0
|
673 // Convert the attributes array into an Attribute object.
|
Chris@0
|
674 $heading['attributes'] = new Attribute($heading['attributes']);
|
Chris@0
|
675 }
|
Chris@0
|
676
|
Chris@0
|
677 $variables['links'] = [];
|
Chris@0
|
678 foreach ($links as $key => $link) {
|
Chris@0
|
679 $item = [];
|
Chris@0
|
680 $link += [
|
Chris@0
|
681 'ajax' => NULL,
|
Chris@0
|
682 'url' => NULL,
|
Chris@0
|
683 ];
|
Chris@0
|
684
|
Chris@0
|
685 $li_attributes = [];
|
Chris@0
|
686 $keys = ['title', 'url'];
|
Chris@0
|
687 $link_element = [
|
Chris@0
|
688 '#type' => 'link',
|
Chris@0
|
689 '#title' => $link['title'],
|
Chris@0
|
690 '#options' => array_diff_key($link, array_combine($keys, $keys)),
|
Chris@0
|
691 '#url' => $link['url'],
|
Chris@0
|
692 '#ajax' => $link['ajax'],
|
Chris@0
|
693 ];
|
Chris@0
|
694
|
Chris@0
|
695 // Handle links and ensure that the active class is added on the LIs, but
|
Chris@0
|
696 // only if the 'set_active_class' option is not empty.
|
Chris@0
|
697 if (isset($link['url'])) {
|
Chris@0
|
698 if (!empty($variables['set_active_class'])) {
|
Chris@0
|
699
|
Chris@0
|
700 // Also enable set_active_class for the contained link.
|
Chris@0
|
701 $link_element['#options']['set_active_class'] = TRUE;
|
Chris@0
|
702
|
Chris@0
|
703 if (!empty($link['language'])) {
|
Chris@0
|
704 $li_attributes['hreflang'] = $link['language']->getId();
|
Chris@0
|
705 }
|
Chris@0
|
706
|
Chris@0
|
707 // Add a "data-drupal-link-query" attribute to let the
|
Chris@0
|
708 // drupal.active-link library know the query in a standardized manner.
|
Chris@0
|
709 if (!empty($link['query'])) {
|
Chris@0
|
710 $query = $link['query'];
|
Chris@0
|
711 ksort($query);
|
Chris@0
|
712 $li_attributes['data-drupal-link-query'] = Json::encode($query);
|
Chris@0
|
713 }
|
Chris@0
|
714
|
Chris@0
|
715 /** @var \Drupal\Core\Url $url */
|
Chris@0
|
716 $url = $link['url'];
|
Chris@0
|
717 if ($url->isRouted()) {
|
Chris@0
|
718 // Add a "data-drupal-link-system-path" attribute to let the
|
Chris@0
|
719 // drupal.active-link library know the path in a standardized manner.
|
Chris@0
|
720 $system_path = $url->getInternalPath();
|
Chris@0
|
721 // @todo System path is deprecated - use the route name and parameters.
|
Chris@0
|
722 // Special case for the front page.
|
Chris@0
|
723 $li_attributes['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
|
Chris@0
|
724 }
|
Chris@0
|
725 }
|
Chris@0
|
726
|
Chris@0
|
727 $item['link'] = $link_element;
|
Chris@0
|
728 }
|
Chris@0
|
729
|
Chris@0
|
730 // Handle title-only text items.
|
Chris@0
|
731 $item['text'] = $link['title'];
|
Chris@0
|
732 if (isset($link['attributes'])) {
|
Chris@0
|
733 $item['text_attributes'] = new Attribute($link['attributes']);
|
Chris@0
|
734 }
|
Chris@0
|
735
|
Chris@0
|
736 // Handle list item attributes.
|
Chris@0
|
737 $item['attributes'] = new Attribute($li_attributes);
|
Chris@0
|
738
|
Chris@0
|
739 // Add the item to the list of links.
|
Chris@0
|
740 $variables['links'][$key] = $item;
|
Chris@0
|
741 }
|
Chris@0
|
742 }
|
Chris@0
|
743 }
|
Chris@0
|
744
|
Chris@0
|
745 /**
|
Chris@0
|
746 * Prepares variables for image templates.
|
Chris@0
|
747 *
|
Chris@0
|
748 * Default template: image.html.twig.
|
Chris@0
|
749 *
|
Chris@0
|
750 * @param array $variables
|
Chris@0
|
751 * An associative array containing:
|
Chris@0
|
752 * - uri: Either the path of the image file (relative to base_path()) or a
|
Chris@0
|
753 * full URL.
|
Chris@0
|
754 * - width: The width of the image (if known).
|
Chris@0
|
755 * - height: The height of the image (if known).
|
Chris@0
|
756 * - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0
|
Chris@0
|
757 * always require an alt attribute. The HTML 5 draft allows the alt
|
Chris@0
|
758 * attribute to be omitted in some cases. Therefore, this variable defaults
|
Chris@0
|
759 * to an empty string, but can be set to NULL for the attribute to be
|
Chris@0
|
760 * omitted. Usually, neither omission nor an empty string satisfies
|
Chris@0
|
761 * accessibility requirements, so it is strongly encouraged for code
|
Chris@0
|
762 * building variables for image.html.twig templates to pass a meaningful
|
Chris@0
|
763 * value for this variable.
|
Chris@0
|
764 * - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
|
Chris@0
|
765 * - http://www.w3.org/TR/xhtml1/dtds.html
|
Chris@0
|
766 * - http://dev.w3.org/html5/spec/Overview.html#alt
|
Chris@0
|
767 * - title: The title text is displayed when the image is hovered in some
|
Chris@0
|
768 * popular browsers.
|
Chris@0
|
769 * - attributes: Associative array of attributes to be placed in the img tag.
|
Chris@0
|
770 * - srcset: Array of multiple URIs and sizes/multipliers.
|
Chris@0
|
771 * - sizes: The sizes attribute for viewport-based selection of images.
|
Chris@0
|
772 * - http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content.html#introduction-3:viewport-based-selection-2
|
Chris@0
|
773 */
|
Chris@0
|
774 function template_preprocess_image(&$variables) {
|
Chris@0
|
775 if (!empty($variables['uri'])) {
|
Chris@0
|
776 $variables['attributes']['src'] = file_url_transform_relative(file_create_url($variables['uri']));
|
Chris@0
|
777 }
|
Chris@0
|
778 // Generate a srcset attribute conforming to the spec at
|
Chris@0
|
779 // http://www.w3.org/html/wg/drafts/html/master/embedded-content.html#attr-img-srcset
|
Chris@0
|
780 if (!empty($variables['srcset'])) {
|
Chris@0
|
781 $srcset = [];
|
Chris@0
|
782 foreach ($variables['srcset'] as $src) {
|
Chris@0
|
783 // URI is mandatory.
|
Chris@0
|
784 $source = file_url_transform_relative(file_create_url($src['uri']));
|
Chris@0
|
785 if (isset($src['width']) && !empty($src['width'])) {
|
Chris@0
|
786 $source .= ' ' . $src['width'];
|
Chris@0
|
787 }
|
Chris@0
|
788 elseif (isset($src['multiplier']) && !empty($src['multiplier'])) {
|
Chris@0
|
789 $source .= ' ' . $src['multiplier'];
|
Chris@0
|
790 }
|
Chris@0
|
791 $srcset[] = $source;
|
Chris@0
|
792 }
|
Chris@0
|
793 $variables['attributes']['srcset'] = implode(', ', $srcset);
|
Chris@0
|
794 }
|
Chris@0
|
795
|
Chris@0
|
796 foreach (['width', 'height', 'alt', 'title', 'sizes'] as $key) {
|
Chris@0
|
797 if (isset($variables[$key])) {
|
Chris@0
|
798 // If the property has already been defined in the attributes,
|
Chris@0
|
799 // do not override, including NULL.
|
Chris@0
|
800 if (array_key_exists($key, $variables['attributes'])) {
|
Chris@0
|
801 continue;
|
Chris@0
|
802 }
|
Chris@0
|
803 $variables['attributes'][$key] = $variables[$key];
|
Chris@0
|
804 }
|
Chris@0
|
805 }
|
Chris@0
|
806 }
|
Chris@0
|
807
|
Chris@0
|
808 /**
|
Chris@0
|
809 * Prepares variables for table templates.
|
Chris@0
|
810 *
|
Chris@0
|
811 * Default template: table.html.twig.
|
Chris@0
|
812 *
|
Chris@0
|
813 * @param array $variables
|
Chris@0
|
814 * An associative array containing:
|
Chris@0
|
815 * - header: An array containing the table headers. Each element of the array
|
Chris@0
|
816 * can be either a localized string or an associative array with the
|
Chris@0
|
817 * following keys:
|
Chris@0
|
818 * - data: The localized title of the table column, as a string or render
|
Chris@0
|
819 * array.
|
Chris@0
|
820 * - field: The database field represented in the table column (required
|
Chris@0
|
821 * if user is to be able to sort on this column).
|
Chris@0
|
822 * - sort: A default sort order for this column ("asc" or "desc"). Only
|
Chris@0
|
823 * one column should be given a default sort order because table sorting
|
Chris@0
|
824 * only applies to one column at a time.
|
Chris@0
|
825 * - class: An array of values for the 'class' attribute. In particular,
|
Chris@0
|
826 * the least important columns that can be hidden on narrow and medium
|
Chris@0
|
827 * width screens should have a 'priority-low' class, referenced with the
|
Chris@0
|
828 * RESPONSIVE_PRIORITY_LOW constant. Columns that should be shown on
|
Chris@0
|
829 * medium+ wide screens should be marked up with a class of
|
Chris@0
|
830 * 'priority-medium', referenced by with the RESPONSIVE_PRIORITY_MEDIUM
|
Chris@0
|
831 * constant. Themes may hide columns with one of these two classes on
|
Chris@0
|
832 * narrow viewports to save horizontal space.
|
Chris@0
|
833 * - Any HTML attributes, such as "colspan", to apply to the column header
|
Chris@0
|
834 * cell.
|
Chris@0
|
835 * - rows: An array of table rows. Every row is an array of cells, or an
|
Chris@0
|
836 * associative array with the following keys:
|
Chris@0
|
837 * - data: An array of cells.
|
Chris@0
|
838 * - Any HTML attributes, such as "class", to apply to the table row.
|
Chris@0
|
839 * - no_striping: A Boolean indicating that the row should receive no
|
Chris@0
|
840 * 'even / odd' styling. Defaults to FALSE.
|
Chris@0
|
841 * Each cell can be either a string or an associative array with the
|
Chris@0
|
842 * following keys:
|
Chris@0
|
843 * - data: The string or render array to display in the table cell.
|
Chris@0
|
844 * - header: Indicates this cell is a header.
|
Chris@0
|
845 * - Any HTML attributes, such as "colspan", to apply to the table cell.
|
Chris@0
|
846 * Here's an example for $rows:
|
Chris@0
|
847 * @code
|
Chris@0
|
848 * $rows = array(
|
Chris@0
|
849 * // Simple row
|
Chris@0
|
850 * array(
|
Chris@0
|
851 * 'Cell 1', 'Cell 2', 'Cell 3'
|
Chris@0
|
852 * ),
|
Chris@0
|
853 * // Row with attributes on the row and some of its cells.
|
Chris@0
|
854 * array(
|
Chris@0
|
855 * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => array('funky')
|
Chris@0
|
856 * ),
|
Chris@0
|
857 * );
|
Chris@0
|
858 * @endcode
|
Chris@0
|
859 * - footer: An array of table rows which will be printed within a <tfoot>
|
Chris@0
|
860 * tag, in the same format as the rows element (see above).
|
Chris@0
|
861 * - attributes: An array of HTML attributes to apply to the table tag.
|
Chris@0
|
862 * - caption: A localized string to use for the <caption> tag.
|
Chris@0
|
863 * - colgroups: An array of column groups. Each element of the array can be
|
Chris@0
|
864 * either:
|
Chris@0
|
865 * - An array of columns, each of which is an associative array of HTML
|
Chris@0
|
866 * attributes applied to the <col> element.
|
Chris@0
|
867 * - An array of attributes applied to the <colgroup> element, which must
|
Chris@0
|
868 * include a "data" attribute. To add attributes to <col> elements,
|
Chris@0
|
869 * set the "data" attribute with an array of columns, each of which is an
|
Chris@0
|
870 * associative array of HTML attributes.
|
Chris@0
|
871 * Here's an example for $colgroup:
|
Chris@0
|
872 * @code
|
Chris@0
|
873 * $colgroup = array(
|
Chris@0
|
874 * // <colgroup> with one <col> element.
|
Chris@0
|
875 * array(
|
Chris@0
|
876 * array(
|
Chris@0
|
877 * 'class' => array('funky'), // Attribute for the <col> element.
|
Chris@0
|
878 * ),
|
Chris@0
|
879 * ),
|
Chris@0
|
880 * // <colgroup> with attributes and inner <col> elements.
|
Chris@0
|
881 * array(
|
Chris@0
|
882 * 'data' => array(
|
Chris@0
|
883 * array(
|
Chris@0
|
884 * 'class' => array('funky'), // Attribute for the <col> element.
|
Chris@0
|
885 * ),
|
Chris@0
|
886 * ),
|
Chris@0
|
887 * 'class' => array('jazzy'), // Attribute for the <colgroup> element.
|
Chris@0
|
888 * ),
|
Chris@0
|
889 * );
|
Chris@0
|
890 * @endcode
|
Chris@0
|
891 * These optional tags are used to group and set properties on columns
|
Chris@0
|
892 * within a table. For example, one may easily group three columns and
|
Chris@0
|
893 * apply same background style to all.
|
Chris@0
|
894 * - sticky: Use a "sticky" table header.
|
Chris@0
|
895 * - empty: The message to display in an extra row if table does not have any
|
Chris@0
|
896 * rows.
|
Chris@0
|
897 */
|
Chris@0
|
898 function template_preprocess_table(&$variables) {
|
Chris@0
|
899 // Format the table columns:
|
Chris@0
|
900 if (!empty($variables['colgroups'])) {
|
Chris@0
|
901 foreach ($variables['colgroups'] as &$colgroup) {
|
Chris@0
|
902 // Check if we're dealing with a simple or complex column
|
Chris@0
|
903 if (isset($colgroup['data'])) {
|
Chris@0
|
904 $cols = $colgroup['data'];
|
Chris@0
|
905 unset($colgroup['data']);
|
Chris@0
|
906 $colgroup_attributes = $colgroup;
|
Chris@0
|
907 }
|
Chris@0
|
908 else {
|
Chris@0
|
909 $cols = $colgroup;
|
Chris@0
|
910 $colgroup_attributes = [];
|
Chris@0
|
911 }
|
Chris@0
|
912 $colgroup = [];
|
Chris@0
|
913 $colgroup['attributes'] = new Attribute($colgroup_attributes);
|
Chris@0
|
914 $colgroup['cols'] = [];
|
Chris@0
|
915
|
Chris@0
|
916 // Build columns.
|
Chris@0
|
917 if (is_array($cols) && !empty($cols)) {
|
Chris@0
|
918 foreach ($cols as $col_key => $col) {
|
Chris@0
|
919 $colgroup['cols'][$col_key]['attributes'] = new Attribute($col);
|
Chris@0
|
920 }
|
Chris@0
|
921 }
|
Chris@0
|
922 }
|
Chris@0
|
923 }
|
Chris@0
|
924
|
Chris@0
|
925 // Build an associative array of responsive classes keyed by column.
|
Chris@0
|
926 $responsive_classes = [];
|
Chris@0
|
927
|
Chris@0
|
928 // Format the table header:
|
Chris@0
|
929 $ts = [];
|
Chris@0
|
930 $header_columns = 0;
|
Chris@0
|
931 if (!empty($variables['header'])) {
|
Chris@0
|
932 $ts = tablesort_init($variables['header']);
|
Chris@0
|
933
|
Chris@0
|
934 // Use a separate index with responsive classes as headers
|
Chris@0
|
935 // may be associative.
|
Chris@0
|
936 $responsive_index = -1;
|
Chris@0
|
937 foreach ($variables['header'] as $col_key => $cell) {
|
Chris@0
|
938 // Increase the responsive index.
|
Chris@0
|
939 $responsive_index++;
|
Chris@0
|
940
|
Chris@0
|
941 if (!is_array($cell)) {
|
Chris@0
|
942 $header_columns++;
|
Chris@0
|
943 $cell_content = $cell;
|
Chris@0
|
944 $cell_attributes = new Attribute();
|
Chris@0
|
945 $is_header = TRUE;
|
Chris@0
|
946 }
|
Chris@0
|
947 else {
|
Chris@0
|
948 if (isset($cell['colspan'])) {
|
Chris@0
|
949 $header_columns += $cell['colspan'];
|
Chris@0
|
950 }
|
Chris@0
|
951 else {
|
Chris@0
|
952 $header_columns++;
|
Chris@0
|
953 }
|
Chris@0
|
954 $cell_content = '';
|
Chris@0
|
955 if (isset($cell['data'])) {
|
Chris@0
|
956 $cell_content = $cell['data'];
|
Chris@0
|
957 unset($cell['data']);
|
Chris@0
|
958 }
|
Chris@0
|
959 // Flag the cell as a header or not and remove the flag.
|
Chris@0
|
960 $is_header = isset($cell['header']) ? $cell['header'] : TRUE;
|
Chris@0
|
961 unset($cell['header']);
|
Chris@0
|
962
|
Chris@0
|
963 // Track responsive classes for each column as needed. Only the header
|
Chris@0
|
964 // cells for a column are marked up with the responsive classes by a
|
Chris@0
|
965 // module developer or themer. The responsive classes on the header cells
|
Chris@0
|
966 // must be transferred to the content cells.
|
Chris@0
|
967 if (!empty($cell['class']) && is_array($cell['class'])) {
|
Chris@0
|
968 if (in_array(RESPONSIVE_PRIORITY_MEDIUM, $cell['class'])) {
|
Chris@0
|
969 $responsive_classes[$responsive_index] = RESPONSIVE_PRIORITY_MEDIUM;
|
Chris@0
|
970 }
|
Chris@0
|
971 elseif (in_array(RESPONSIVE_PRIORITY_LOW, $cell['class'])) {
|
Chris@0
|
972 $responsive_classes[$responsive_index] = RESPONSIVE_PRIORITY_LOW;
|
Chris@0
|
973 }
|
Chris@0
|
974 }
|
Chris@0
|
975
|
Chris@0
|
976 tablesort_header($cell_content, $cell, $variables['header'], $ts);
|
Chris@0
|
977
|
Chris@0
|
978 // tablesort_header() removes the 'sort' and 'field' keys.
|
Chris@0
|
979 $cell_attributes = new Attribute($cell);
|
Chris@0
|
980 }
|
Chris@0
|
981 $variables['header'][$col_key] = [];
|
Chris@0
|
982 $variables['header'][$col_key]['tag'] = $is_header ? 'th' : 'td';
|
Chris@0
|
983 $variables['header'][$col_key]['attributes'] = $cell_attributes;
|
Chris@0
|
984 $variables['header'][$col_key]['content'] = $cell_content;
|
Chris@0
|
985 }
|
Chris@0
|
986 }
|
Chris@0
|
987 $variables['header_columns'] = $header_columns;
|
Chris@0
|
988
|
Chris@0
|
989 // Rows and footer have the same structure.
|
Chris@0
|
990 $sections = ['rows' , 'footer'];
|
Chris@0
|
991 foreach ($sections as $section) {
|
Chris@0
|
992 if (!empty($variables[$section])) {
|
Chris@0
|
993 foreach ($variables[$section] as $row_key => $row) {
|
Chris@0
|
994 $cells = $row;
|
Chris@0
|
995 $row_attributes = [];
|
Chris@0
|
996
|
Chris@0
|
997 // Check if we're dealing with a simple or complex row
|
Chris@0
|
998 if (isset($row['data'])) {
|
Chris@0
|
999 $cells = $row['data'];
|
Chris@0
|
1000 $variables['no_striping'] = isset($row['no_striping']) ? $row['no_striping'] : FALSE;
|
Chris@0
|
1001
|
Chris@0
|
1002 // Set the attributes array and exclude 'data' and 'no_striping'.
|
Chris@0
|
1003 $row_attributes = $row;
|
Chris@0
|
1004 unset($row_attributes['data']);
|
Chris@0
|
1005 unset($row_attributes['no_striping']);
|
Chris@0
|
1006 }
|
Chris@0
|
1007
|
Chris@0
|
1008 // Build row.
|
Chris@0
|
1009 $variables[$section][$row_key] = [];
|
Chris@0
|
1010 $variables[$section][$row_key]['attributes'] = new Attribute($row_attributes);
|
Chris@0
|
1011 $variables[$section][$row_key]['cells'] = [];
|
Chris@0
|
1012 if (!empty($cells)) {
|
Chris@0
|
1013 // Reset the responsive index.
|
Chris@0
|
1014 $responsive_index = -1;
|
Chris@0
|
1015 foreach ($cells as $col_key => $cell) {
|
Chris@0
|
1016 // Increase the responsive index.
|
Chris@0
|
1017 $responsive_index++;
|
Chris@0
|
1018
|
Chris@0
|
1019 if (!is_array($cell)) {
|
Chris@0
|
1020 $cell_content = $cell;
|
Chris@0
|
1021 $cell_attributes = [];
|
Chris@0
|
1022 $is_header = FALSE;
|
Chris@0
|
1023 }
|
Chris@0
|
1024 else {
|
Chris@0
|
1025 $cell_content = '';
|
Chris@0
|
1026 if (isset($cell['data'])) {
|
Chris@0
|
1027 $cell_content = $cell['data'];
|
Chris@0
|
1028 unset($cell['data']);
|
Chris@0
|
1029 }
|
Chris@0
|
1030
|
Chris@0
|
1031 // Flag the cell as a header or not and remove the flag.
|
Chris@0
|
1032 $is_header = !empty($cell['header']);
|
Chris@0
|
1033 unset($cell['header']);
|
Chris@0
|
1034
|
Chris@0
|
1035 $cell_attributes = $cell;
|
Chris@0
|
1036 }
|
Chris@0
|
1037 // Active table sort information.
|
Chris@0
|
1038 if (isset($variables['header'][$col_key]['data']) && $variables['header'][$col_key]['data'] == $ts['name'] && !empty($variables['header'][$col_key]['field'])) {
|
Chris@0
|
1039 $variables[$section][$row_key]['cells'][$col_key]['active_table_sort'] = TRUE;
|
Chris@0
|
1040 }
|
Chris@0
|
1041 // Copy RESPONSIVE_PRIORITY_LOW/RESPONSIVE_PRIORITY_MEDIUM
|
Chris@0
|
1042 // class from header to cell as needed.
|
Chris@0
|
1043 if (isset($responsive_classes[$responsive_index])) {
|
Chris@0
|
1044 $cell_attributes['class'][] = $responsive_classes[$responsive_index];
|
Chris@0
|
1045 }
|
Chris@0
|
1046 $variables[$section][$row_key]['cells'][$col_key]['tag'] = $is_header ? 'th' : 'td';
|
Chris@0
|
1047 $variables[$section][$row_key]['cells'][$col_key]['attributes'] = new Attribute($cell_attributes);
|
Chris@0
|
1048 $variables[$section][$row_key]['cells'][$col_key]['content'] = $cell_content;
|
Chris@0
|
1049 }
|
Chris@0
|
1050 }
|
Chris@0
|
1051 }
|
Chris@0
|
1052 }
|
Chris@0
|
1053 }
|
Chris@0
|
1054 if (empty($variables['no_striping'])) {
|
Chris@0
|
1055 $variables['attributes']['data-striping'] = 1;
|
Chris@0
|
1056 }
|
Chris@0
|
1057 }
|
Chris@0
|
1058
|
Chris@0
|
1059 /**
|
Chris@0
|
1060 * Prepares variables for item list templates.
|
Chris@0
|
1061 *
|
Chris@0
|
1062 * Default template: item-list.html.twig.
|
Chris@0
|
1063 *
|
Chris@0
|
1064 * @param array $variables
|
Chris@0
|
1065 * An associative array containing:
|
Chris@0
|
1066 * - items: An array of items to be displayed in the list. Each item can be
|
Chris@0
|
1067 * either a string or a render array. If #type, #theme, or #markup
|
Chris@0
|
1068 * properties are not specified for child render arrays, they will be
|
Chris@0
|
1069 * inherited from the parent list, allowing callers to specify larger
|
Chris@0
|
1070 * nested lists without having to explicitly specify and repeat the
|
Chris@0
|
1071 * render properties for all nested child lists.
|
Chris@0
|
1072 * - title: A title to be prepended to the list.
|
Chris@0
|
1073 * - list_type: The type of list to return (e.g. "ul", "ol").
|
Chris@0
|
1074 * - wrapper_attributes: HTML attributes to be applied to the list wrapper.
|
Chris@0
|
1075 *
|
Chris@0
|
1076 * @see https://www.drupal.org/node/1842756
|
Chris@0
|
1077 */
|
Chris@0
|
1078 function template_preprocess_item_list(&$variables) {
|
Chris@0
|
1079 $variables['wrapper_attributes'] = new Attribute($variables['wrapper_attributes']);
|
Chris@0
|
1080 foreach ($variables['items'] as &$item) {
|
Chris@0
|
1081 $attributes = [];
|
Chris@0
|
1082 // If the item value is an array, then it is a render array.
|
Chris@0
|
1083 if (is_array($item)) {
|
Chris@0
|
1084 // List items support attributes via the '#wrapper_attributes' property.
|
Chris@0
|
1085 if (isset($item['#wrapper_attributes'])) {
|
Chris@0
|
1086 $attributes = $item['#wrapper_attributes'];
|
Chris@0
|
1087 }
|
Chris@0
|
1088 // Determine whether there are any child elements in the item that are not
|
Chris@0
|
1089 // fully-specified render arrays. If there are any, then the child
|
Chris@0
|
1090 // elements present nested lists and we automatically inherit the render
|
Chris@0
|
1091 // array properties of the current list to them.
|
Chris@0
|
1092 foreach (Element::children($item) as $key) {
|
Chris@0
|
1093 $child = &$item[$key];
|
Chris@0
|
1094 // If this child element does not specify how it can be rendered, then
|
Chris@0
|
1095 // we need to inherit the render properties of the current list.
|
Chris@0
|
1096 if (!isset($child['#type']) && !isset($child['#theme']) && !isset($child['#markup'])) {
|
Chris@0
|
1097 // Since item-list.html.twig supports both strings and render arrays
|
Chris@0
|
1098 // as items, the items of the nested list may have been specified as
|
Chris@0
|
1099 // the child elements of the nested list, instead of #items. For
|
Chris@0
|
1100 // convenience, we automatically move them into #items.
|
Chris@0
|
1101 if (!isset($child['#items'])) {
|
Chris@0
|
1102 // This is the same condition as in
|
Chris@0
|
1103 // \Drupal\Core\Render\Element::children(), which cannot be used
|
Chris@0
|
1104 // here, since it triggers an error on string values.
|
Chris@0
|
1105 foreach ($child as $child_key => $child_value) {
|
Chris@0
|
1106 if ($child_key[0] !== '#') {
|
Chris@0
|
1107 $child['#items'][$child_key] = $child_value;
|
Chris@0
|
1108 unset($child[$child_key]);
|
Chris@0
|
1109 }
|
Chris@0
|
1110 }
|
Chris@0
|
1111 }
|
Chris@0
|
1112 // Lastly, inherit the original theme variables of the current list.
|
Chris@0
|
1113 $child['#theme'] = $variables['theme_hook_original'];
|
Chris@0
|
1114 $child['#list_type'] = $variables['list_type'];
|
Chris@0
|
1115 }
|
Chris@0
|
1116 }
|
Chris@0
|
1117 }
|
Chris@0
|
1118
|
Chris@0
|
1119 // Set the item's value and attributes for the template.
|
Chris@0
|
1120 $item = [
|
Chris@0
|
1121 'value' => $item,
|
Chris@0
|
1122 'attributes' => new Attribute($attributes),
|
Chris@0
|
1123 ];
|
Chris@0
|
1124 }
|
Chris@0
|
1125 }
|
Chris@0
|
1126
|
Chris@0
|
1127 /**
|
Chris@0
|
1128 * Prepares variables for container templates.
|
Chris@0
|
1129 *
|
Chris@0
|
1130 * Default template: container.html.twig.
|
Chris@0
|
1131 *
|
Chris@0
|
1132 * @param array $variables
|
Chris@0
|
1133 * An associative array containing:
|
Chris@0
|
1134 * - element: An associative array containing the properties of the element.
|
Chris@0
|
1135 * Properties used: #id, #attributes, #children.
|
Chris@0
|
1136 */
|
Chris@0
|
1137 function template_preprocess_container(&$variables) {
|
Chris@0
|
1138 $variables['has_parent'] = FALSE;
|
Chris@0
|
1139 $element = $variables['element'];
|
Chris@0
|
1140 // Ensure #attributes is set.
|
Chris@0
|
1141 $element += ['#attributes' => []];
|
Chris@0
|
1142
|
Chris@0
|
1143 // Special handling for form elements.
|
Chris@0
|
1144 if (isset($element['#array_parents'])) {
|
Chris@0
|
1145 // Assign an html ID.
|
Chris@0
|
1146 if (!isset($element['#attributes']['id'])) {
|
Chris@0
|
1147 $element['#attributes']['id'] = $element['#id'];
|
Chris@0
|
1148 }
|
Chris@0
|
1149 $variables['has_parent'] = TRUE;
|
Chris@0
|
1150 }
|
Chris@0
|
1151
|
Chris@0
|
1152 $variables['children'] = $element['#children'];
|
Chris@0
|
1153 $variables['attributes'] = $element['#attributes'];
|
Chris@0
|
1154 }
|
Chris@0
|
1155
|
Chris@0
|
1156 /**
|
Chris@0
|
1157 * Prepares variables for maintenance task list templates.
|
Chris@0
|
1158 *
|
Chris@0
|
1159 * Default template: maintenance-task-list.html.twig.
|
Chris@0
|
1160 *
|
Chris@0
|
1161 * @param array $variables
|
Chris@0
|
1162 * An associative array containing:
|
Chris@0
|
1163 * - items: An associative array of maintenance tasks.
|
Chris@0
|
1164 * It's the caller's responsibility to ensure this array's items contain no
|
Chris@0
|
1165 * dangerous HTML such as <script> tags.
|
Chris@0
|
1166 * - active: The key for the currently active maintenance task.
|
Chris@0
|
1167 */
|
Chris@0
|
1168 function template_preprocess_maintenance_task_list(&$variables) {
|
Chris@0
|
1169 $items = $variables['items'];
|
Chris@0
|
1170 $active = $variables['active'];
|
Chris@0
|
1171
|
Chris@0
|
1172 $done = isset($items[$active]) || $active == NULL;
|
Chris@0
|
1173 foreach ($items as $k => $item) {
|
Chris@0
|
1174 $variables['tasks'][$k]['item'] = $item;
|
Chris@0
|
1175 $variables['tasks'][$k]['attributes'] = new Attribute();
|
Chris@0
|
1176 if ($active == $k) {
|
Chris@0
|
1177 $variables['tasks'][$k]['attributes']->addClass('is-active');
|
Chris@0
|
1178 $variables['tasks'][$k]['status'] = t('active');
|
Chris@0
|
1179 $done = FALSE;
|
Chris@0
|
1180 }
|
Chris@0
|
1181 else {
|
Chris@0
|
1182 if ($done) {
|
Chris@0
|
1183 $variables['tasks'][$k]['attributes']->addClass('done');
|
Chris@0
|
1184 $variables['tasks'][$k]['status'] = t('done');
|
Chris@0
|
1185 }
|
Chris@0
|
1186 }
|
Chris@0
|
1187 }
|
Chris@0
|
1188 }
|
Chris@0
|
1189
|
Chris@0
|
1190 /**
|
Chris@0
|
1191 * Adds a default set of helper variables for preprocessors and templates.
|
Chris@0
|
1192 *
|
Chris@0
|
1193 * This function is called for theme hooks implemented as templates only, not
|
Chris@0
|
1194 * for theme hooks implemented as functions. This preprocess function is the
|
Chris@0
|
1195 * first in the sequence of preprocessing functions that are called when
|
Chris@0
|
1196 * preparing variables for a template.
|
Chris@0
|
1197 *
|
Chris@0
|
1198 * See the @link themeable Default theme implementations topic @endlink for
|
Chris@0
|
1199 * details.
|
Chris@0
|
1200 */
|
Chris@0
|
1201 function template_preprocess(&$variables, $hook, $info) {
|
Chris@0
|
1202 // Merge in variables that don't depend on hook and don't change during a
|
Chris@0
|
1203 // single page request.
|
Chris@0
|
1204 // Use the advanced drupal_static() pattern, since this is called very often.
|
Chris@0
|
1205 static $drupal_static_fast;
|
Chris@0
|
1206 if (!isset($drupal_static_fast)) {
|
Chris@0
|
1207 $drupal_static_fast['default_variables'] = &drupal_static(__FUNCTION__);
|
Chris@0
|
1208 }
|
Chris@0
|
1209 $default_variables = &$drupal_static_fast['default_variables'];
|
Chris@0
|
1210 if (!isset($default_variables)) {
|
Chris@0
|
1211 $default_variables = _template_preprocess_default_variables();
|
Chris@0
|
1212 }
|
Chris@0
|
1213 $variables += $default_variables;
|
Chris@0
|
1214
|
Chris@0
|
1215 // When theming a render element, merge its #attributes into
|
Chris@0
|
1216 // $variables['attributes'].
|
Chris@0
|
1217 if (isset($info['render element'])) {
|
Chris@0
|
1218 $key = $info['render element'];
|
Chris@0
|
1219 if (isset($variables[$key]['#attributes'])) {
|
Chris@0
|
1220 $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], $variables[$key]['#attributes']);
|
Chris@0
|
1221 }
|
Chris@0
|
1222 }
|
Chris@0
|
1223 }
|
Chris@0
|
1224
|
Chris@0
|
1225 /**
|
Chris@0
|
1226 * Returns hook-independent variables to template_preprocess().
|
Chris@0
|
1227 */
|
Chris@0
|
1228 function _template_preprocess_default_variables() {
|
Chris@0
|
1229 // Variables that don't depend on a database connection.
|
Chris@0
|
1230 $variables = [
|
Chris@0
|
1231 'attributes' => [],
|
Chris@0
|
1232 'title_attributes' => [],
|
Chris@0
|
1233 'content_attributes' => [],
|
Chris@0
|
1234 'title_prefix' => [],
|
Chris@0
|
1235 'title_suffix' => [],
|
Chris@0
|
1236 'db_is_active' => !defined('MAINTENANCE_MODE'),
|
Chris@0
|
1237 'is_admin' => FALSE,
|
Chris@0
|
1238 'logged_in' => FALSE,
|
Chris@0
|
1239 ];
|
Chris@0
|
1240
|
Chris@0
|
1241 // Give modules a chance to alter the default template variables.
|
Chris@0
|
1242 \Drupal::moduleHandler()->alter('template_preprocess_default_variables', $variables);
|
Chris@0
|
1243
|
Chris@0
|
1244 // Tell all templates where they are located.
|
Chris@0
|
1245 $variables['directory'] = \Drupal::theme()->getActiveTheme()->getPath();
|
Chris@0
|
1246
|
Chris@0
|
1247 return $variables;
|
Chris@0
|
1248 }
|
Chris@0
|
1249
|
Chris@0
|
1250 /**
|
Chris@0
|
1251 * Prepares variables for HTML document templates.
|
Chris@0
|
1252 *
|
Chris@0
|
1253 * Default template: html.html.twig.
|
Chris@0
|
1254 *
|
Chris@0
|
1255 * @param array $variables
|
Chris@0
|
1256 * An associative array containing:
|
Chris@0
|
1257 * - page: A render element representing the page.
|
Chris@0
|
1258 */
|
Chris@0
|
1259 function template_preprocess_html(&$variables) {
|
Chris@0
|
1260 $variables['page'] = $variables['html']['page'];
|
Chris@0
|
1261 unset($variables['html']['page']);
|
Chris@0
|
1262 $variables['page_top'] = NULL;
|
Chris@0
|
1263 if (isset($variables['html']['page_top'])) {
|
Chris@0
|
1264 $variables['page_top'] = $variables['html']['page_top'];
|
Chris@0
|
1265 unset($variables['html']['page_top']);
|
Chris@0
|
1266 }
|
Chris@0
|
1267 $variables['page_bottom'] = NULL;
|
Chris@0
|
1268 if (isset($variables['html']['page_bottom'])) {
|
Chris@0
|
1269 $variables['page_bottom'] = $variables['html']['page_bottom'];
|
Chris@0
|
1270 unset($variables['html']['page_bottom']);
|
Chris@0
|
1271 }
|
Chris@0
|
1272
|
Chris@0
|
1273 $variables['html_attributes'] = new Attribute();
|
Chris@0
|
1274
|
Chris@0
|
1275 // <html> element attributes.
|
Chris@0
|
1276 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
Chris@0
|
1277 $variables['html_attributes']['lang'] = $language_interface->getId();
|
Chris@0
|
1278 $variables['html_attributes']['dir'] = $language_interface->getDirection();
|
Chris@0
|
1279
|
Chris@0
|
1280 if (isset($variables['db_is_active']) && !$variables['db_is_active']) {
|
Chris@0
|
1281 $variables['db_offline'] = TRUE;
|
Chris@0
|
1282 }
|
Chris@0
|
1283
|
Chris@0
|
1284 // Add a variable for the root path. This can be used to create a class and
|
Chris@0
|
1285 // theme the page depending on the current path (e.g. node, admin, user) as
|
Chris@0
|
1286 // well as more specific data like path-frontpage.
|
Chris@0
|
1287 $is_front_page = \Drupal::service('path.matcher')->isFrontPage();
|
Chris@0
|
1288
|
Chris@0
|
1289 if ($is_front_page) {
|
Chris@0
|
1290 $variables['root_path'] = FALSE;
|
Chris@0
|
1291 }
|
Chris@0
|
1292 else {
|
Chris@0
|
1293 $system_path = \Drupal::service('path.current')->getPath();
|
Chris@0
|
1294 $variables['root_path'] = explode('/', $system_path)[1];
|
Chris@0
|
1295 }
|
Chris@0
|
1296
|
Chris@0
|
1297 $site_config = \Drupal::config('system.site');
|
Chris@0
|
1298 // Construct page title.
|
Chris@0
|
1299 if (isset($variables['page']['#title']) && is_array($variables['page']['#title'])) {
|
Chris@0
|
1300 // Do an early render if the title is a render array.
|
Chris@0
|
1301 $variables['page']['#title'] = (string) \Drupal::service('renderer')->render($variables['page']['#title']);
|
Chris@0
|
1302 }
|
Chris@0
|
1303 if (!empty($variables['page']['#title'])) {
|
Chris@0
|
1304 $head_title = [
|
Chris@0
|
1305 // Marking the title as safe since it has had the tags stripped.
|
Chris@0
|
1306 'title' => Markup::create(trim(strip_tags($variables['page']['#title']))),
|
Chris@0
|
1307 'name' => $site_config->get('name'),
|
Chris@0
|
1308 ];
|
Chris@0
|
1309 }
|
Chris@0
|
1310 // @todo Remove once views is not bypassing the view subscriber anymore.
|
Chris@0
|
1311 // @see https://www.drupal.org/node/2068471
|
Chris@0
|
1312 elseif ($is_front_page) {
|
Chris@0
|
1313 $head_title = [
|
Chris@0
|
1314 'title' => t('Home'),
|
Chris@0
|
1315 'name' => $site_config->get('name'),
|
Chris@0
|
1316 ];
|
Chris@0
|
1317 }
|
Chris@0
|
1318 else {
|
Chris@0
|
1319 $head_title = ['name' => $site_config->get('name')];
|
Chris@0
|
1320 if ($site_config->get('slogan')) {
|
Chris@0
|
1321 $head_title['slogan'] = strip_tags($site_config->get('slogan'));
|
Chris@0
|
1322 }
|
Chris@0
|
1323 }
|
Chris@0
|
1324
|
Chris@0
|
1325 $variables['head_title'] = $head_title;
|
Chris@0
|
1326 // @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
|
Chris@0
|
1327 $variables['head_title_array'] = $head_title;
|
Chris@0
|
1328
|
Chris@0
|
1329 // Create placeholder strings for these keys.
|
Chris@0
|
1330 // @see \Drupal\Core\Render\HtmlResponseSubscriber
|
Chris@0
|
1331 $types = [
|
Chris@0
|
1332 'styles' => 'css',
|
Chris@0
|
1333 'scripts' => 'js',
|
Chris@0
|
1334 'scripts_bottom' => 'js-bottom',
|
Chris@0
|
1335 'head' => 'head',
|
Chris@0
|
1336 ];
|
Chris@0
|
1337 $variables['placeholder_token'] = Crypt::randomBytesBase64(55);
|
Chris@0
|
1338 foreach ($types as $type => $placeholder_name) {
|
Chris@0
|
1339 $placeholder = '<' . $placeholder_name . '-placeholder token="' . $variables['placeholder_token'] . '">';
|
Chris@0
|
1340 $variables['#attached']['html_response_attachment_placeholders'][$type] = $placeholder;
|
Chris@0
|
1341 }
|
Chris@0
|
1342 }
|
Chris@0
|
1343
|
Chris@0
|
1344 /**
|
Chris@0
|
1345 * Prepares variables for the page template.
|
Chris@0
|
1346 *
|
Chris@0
|
1347 * Default template: page.html.twig.
|
Chris@0
|
1348 *
|
Chris@0
|
1349 * See the page.html.twig template for the list of variables.
|
Chris@0
|
1350 */
|
Chris@0
|
1351 function template_preprocess_page(&$variables) {
|
Chris@0
|
1352 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
Chris@0
|
1353
|
Chris@0
|
1354 foreach (\Drupal::theme()->getActiveTheme()->getRegions() as $region) {
|
Chris@0
|
1355 if (!isset($variables['page'][$region])) {
|
Chris@0
|
1356 $variables['page'][$region] = [];
|
Chris@0
|
1357 }
|
Chris@0
|
1358 }
|
Chris@0
|
1359
|
Chris@0
|
1360 $variables['base_path'] = base_path();
|
Chris@0
|
1361 $variables['front_page'] = \Drupal::url('<front>');
|
Chris@0
|
1362 $variables['language'] = $language_interface;
|
Chris@0
|
1363
|
Chris@0
|
1364 // An exception might be thrown.
|
Chris@0
|
1365 try {
|
Chris@0
|
1366 $variables['is_front'] = \Drupal::service('path.matcher')->isFrontPage();
|
Chris@0
|
1367 }
|
Chris@0
|
1368 catch (Exception $e) {
|
Chris@0
|
1369 // If the database is not yet available, set default values for these
|
Chris@0
|
1370 // variables.
|
Chris@0
|
1371 $variables['is_front'] = FALSE;
|
Chris@0
|
1372 $variables['db_is_active'] = FALSE;
|
Chris@0
|
1373 }
|
Chris@0
|
1374
|
Chris@0
|
1375 if ($node = \Drupal::routeMatch()->getParameter('node')) {
|
Chris@0
|
1376 $variables['node'] = $node;
|
Chris@0
|
1377 }
|
Chris@0
|
1378 }
|
Chris@0
|
1379
|
Chris@0
|
1380 /**
|
Chris@0
|
1381 * Generate an array of suggestions from path arguments.
|
Chris@0
|
1382 *
|
Chris@0
|
1383 * This is typically called for adding to the suggestions in
|
Chris@0
|
1384 * hook_theme_suggestions_HOOK_alter() or adding to 'attributes' class key
|
Chris@0
|
1385 * variables from within preprocess functions, when wanting to base the
|
Chris@0
|
1386 * additional suggestions or classes on the path of the current page.
|
Chris@0
|
1387 *
|
Chris@0
|
1388 * @param $args
|
Chris@0
|
1389 * An array of path arguments.
|
Chris@0
|
1390 * @param $base
|
Chris@0
|
1391 * A string identifying the base 'thing' from which more specific suggestions
|
Chris@0
|
1392 * are derived. For example, 'page' or 'html'.
|
Chris@0
|
1393 * @param $delimiter
|
Chris@0
|
1394 * The string used to delimit increasingly specific information. The default
|
Chris@0
|
1395 * of '__' is appropriate for theme hook suggestions. '-' is appropriate for
|
Chris@0
|
1396 * extra classes.
|
Chris@0
|
1397 *
|
Chris@0
|
1398 * @return
|
Chris@0
|
1399 * An array of suggestions, suitable for adding to
|
Chris@0
|
1400 * hook_theme_suggestions_HOOK_alter() or to $variables['attributes']['class']
|
Chris@0
|
1401 * if the suggestions represent extra CSS classes.
|
Chris@0
|
1402 */
|
Chris@0
|
1403 function theme_get_suggestions($args, $base, $delimiter = '__') {
|
Chris@0
|
1404
|
Chris@0
|
1405 // Build a list of suggested theme hooks in order of
|
Chris@0
|
1406 // specificity. One suggestion is made for every element of the current path,
|
Chris@0
|
1407 // though numeric elements are not carried to subsequent suggestions. For
|
Chris@0
|
1408 // example, for $base='page', http://www.example.com/node/1/edit would result
|
Chris@0
|
1409 // in the following suggestions:
|
Chris@0
|
1410 //
|
Chris@0
|
1411 // page__node
|
Chris@0
|
1412 // page__node__%
|
Chris@0
|
1413 // page__node__1
|
Chris@0
|
1414 // page__node__edit
|
Chris@0
|
1415
|
Chris@0
|
1416 $suggestions = [];
|
Chris@0
|
1417 $prefix = $base;
|
Chris@0
|
1418 foreach ($args as $arg) {
|
Chris@0
|
1419 // Remove slashes or null per SA-CORE-2009-003 and change - (hyphen) to _
|
Chris@0
|
1420 // (underscore).
|
Chris@0
|
1421 //
|
Chris@0
|
1422 // When we discover templates in @see drupal_find_theme_templates,
|
Chris@0
|
1423 // hyphens (-) are converted to underscores (_) before the theme hook
|
Chris@0
|
1424 // is registered. We do this because the hyphens used for delimiters
|
Chris@0
|
1425 // in hook suggestions cannot be used in the function names of the
|
Chris@0
|
1426 // associated preprocess functions. Any page templates designed to be used
|
Chris@0
|
1427 // on paths that contain a hyphen are also registered with these hyphens
|
Chris@0
|
1428 // converted to underscores so here we must convert any hyphens in path
|
Chris@0
|
1429 // arguments to underscores here before fetching theme hook suggestions
|
Chris@0
|
1430 // to ensure the templates are appropriately recognized.
|
Chris@0
|
1431 $arg = str_replace(["/", "\\", "\0", '-'], ['', '', '', '_'], $arg);
|
Chris@0
|
1432 // The percent acts as a wildcard for numeric arguments since
|
Chris@0
|
1433 // asterisks are not valid filename characters on many filesystems.
|
Chris@0
|
1434 if (is_numeric($arg)) {
|
Chris@0
|
1435 $suggestions[] = $prefix . $delimiter . '%';
|
Chris@0
|
1436 }
|
Chris@0
|
1437 $suggestions[] = $prefix . $delimiter . $arg;
|
Chris@0
|
1438 if (!is_numeric($arg)) {
|
Chris@0
|
1439 $prefix .= $delimiter . $arg;
|
Chris@0
|
1440 }
|
Chris@0
|
1441 }
|
Chris@0
|
1442 if (\Drupal::service('path.matcher')->isFrontPage()) {
|
Chris@0
|
1443 // Front templates should be based on root only, not prefixed arguments.
|
Chris@0
|
1444 $suggestions[] = $base . $delimiter . 'front';
|
Chris@0
|
1445 }
|
Chris@0
|
1446
|
Chris@0
|
1447 return $suggestions;
|
Chris@0
|
1448 }
|
Chris@0
|
1449
|
Chris@0
|
1450 /**
|
Chris@0
|
1451 * Prepares variables for maintenance page templates.
|
Chris@0
|
1452 *
|
Chris@0
|
1453 * Default template: maintenance-page.html.twig.
|
Chris@0
|
1454 *
|
Chris@0
|
1455 * @param array $variables
|
Chris@0
|
1456 * An associative array containing:
|
Chris@0
|
1457 * - content - An array of page content.
|
Chris@0
|
1458 *
|
Chris@0
|
1459 * @see system_page_attachments()
|
Chris@0
|
1460 */
|
Chris@0
|
1461 function template_preprocess_maintenance_page(&$variables) {
|
Chris@0
|
1462 // @todo Rename the templates to page--maintenance + page--install.
|
Chris@0
|
1463 template_preprocess_page($variables);
|
Chris@0
|
1464
|
Chris@0
|
1465 // @see system_page_attachments()
|
Chris@0
|
1466 $variables['#attached']['library'][] = 'system/maintenance';
|
Chris@0
|
1467
|
Chris@0
|
1468 // Maintenance page and install page need branding info in variables because
|
Chris@0
|
1469 // there is no blocks.
|
Chris@0
|
1470 $site_config = \Drupal::config('system.site');
|
Chris@0
|
1471 $variables['logo'] = theme_get_setting('logo.url');
|
Chris@0
|
1472 $variables['site_name'] = $site_config->get('name');
|
Chris@0
|
1473 $variables['site_slogan'] = $site_config->get('slogan');
|
Chris@0
|
1474
|
Chris@0
|
1475 // Maintenance page and install page need page title in variable because there
|
Chris@0
|
1476 // are no blocks.
|
Chris@0
|
1477 $variables['title'] = $variables['page']['#title'];
|
Chris@0
|
1478 }
|
Chris@0
|
1479
|
Chris@0
|
1480 /**
|
Chris@0
|
1481 * Prepares variables for install page templates.
|
Chris@0
|
1482 *
|
Chris@0
|
1483 * Default template: install-page.html.twig.
|
Chris@0
|
1484 *
|
Chris@0
|
1485 * @param array $variables
|
Chris@0
|
1486 * An associative array containing:
|
Chris@0
|
1487 * - content - An array of page content.
|
Chris@0
|
1488 *
|
Chris@0
|
1489 * @see template_preprocess_maintenance_page()
|
Chris@0
|
1490 */
|
Chris@0
|
1491 function template_preprocess_install_page(&$variables) {
|
Chris@0
|
1492 template_preprocess_maintenance_page($variables);
|
Chris@0
|
1493
|
Chris@0
|
1494 // Override the site name that is displayed on the page, since Drupal is
|
Chris@0
|
1495 // still in the process of being installed.
|
Chris@0
|
1496 $distribution_name = drupal_install_profile_distribution_name();
|
Chris@0
|
1497 $variables['site_name'] = $distribution_name;
|
Chris@0
|
1498 $variables['site_version'] = drupal_install_profile_distribution_version();
|
Chris@0
|
1499 }
|
Chris@0
|
1500
|
Chris@0
|
1501 /**
|
Chris@0
|
1502 * Prepares variables for region templates.
|
Chris@0
|
1503 *
|
Chris@0
|
1504 * Default template: region.html.twig.
|
Chris@0
|
1505 *
|
Chris@0
|
1506 * Prepares the values passed to the theme_region function to be passed into a
|
Chris@0
|
1507 * pluggable template engine. Uses the region name to generate a template file
|
Chris@0
|
1508 * suggestions.
|
Chris@0
|
1509 *
|
Chris@0
|
1510 * @param array $variables
|
Chris@0
|
1511 * An associative array containing:
|
Chris@0
|
1512 * - elements: An associative array containing properties of the region.
|
Chris@0
|
1513 */
|
Chris@0
|
1514 function template_preprocess_region(&$variables) {
|
Chris@0
|
1515 // Create the $content variable that templates expect.
|
Chris@0
|
1516 $variables['content'] = $variables['elements']['#children'];
|
Chris@0
|
1517 $variables['region'] = $variables['elements']['#region'];
|
Chris@0
|
1518 }
|
Chris@0
|
1519
|
Chris@0
|
1520 /**
|
Chris@0
|
1521 * Prepares variables for field templates.
|
Chris@0
|
1522 *
|
Chris@0
|
1523 * Default template: field.html.twig.
|
Chris@0
|
1524 *
|
Chris@0
|
1525 * @param array $variables
|
Chris@0
|
1526 * An associative array containing:
|
Chris@0
|
1527 * - element: A render element representing the field.
|
Chris@0
|
1528 * - attributes: A string containing the attributes for the wrapping div.
|
Chris@0
|
1529 * - title_attributes: A string containing the attributes for the title.
|
Chris@0
|
1530 */
|
Chris@0
|
1531 function template_preprocess_field(&$variables, $hook) {
|
Chris@0
|
1532 $element = $variables['element'];
|
Chris@0
|
1533
|
Chris@0
|
1534 // Creating variables for the template.
|
Chris@0
|
1535 $variables['entity_type'] = $element['#entity_type'];
|
Chris@0
|
1536 $variables['field_name'] = $element['#field_name'];
|
Chris@0
|
1537 $variables['field_type'] = $element['#field_type'];
|
Chris@0
|
1538 $variables['label_display'] = $element['#label_display'];
|
Chris@0
|
1539
|
Chris@0
|
1540 $variables['label_hidden'] = ($element['#label_display'] == 'hidden');
|
Chris@0
|
1541 // Always set the field label - allow themes to decide whether to display it.
|
Chris@0
|
1542 // In addition the label should be rendered but hidden to support screen
|
Chris@0
|
1543 // readers.
|
Chris@0
|
1544 $variables['label'] = $element['#title'];
|
Chris@0
|
1545
|
Chris@0
|
1546 $variables['multiple'] = $element['#is_multiple'];
|
Chris@0
|
1547
|
Chris@0
|
1548 static $default_attributes;
|
Chris@0
|
1549 if (!isset($default_attributes)) {
|
Chris@0
|
1550 $default_attributes = new Attribute();
|
Chris@0
|
1551 }
|
Chris@0
|
1552
|
Chris@0
|
1553 // Merge attributes when a single-value field has a hidden label.
|
Chris@0
|
1554 if ($element['#label_display'] == 'hidden' && !$variables['multiple'] && !empty($element['#items'][0]->_attributes)) {
|
Chris@0
|
1555 $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], (array) $element['#items'][0]->_attributes);
|
Chris@0
|
1556 }
|
Chris@0
|
1557
|
Chris@0
|
1558 // We want other preprocess functions and the theme implementation to have
|
Chris@0
|
1559 // fast access to the field item render arrays. The item render array keys
|
Chris@0
|
1560 // (deltas) should always be numerically indexed starting from 0, and looping
|
Chris@0
|
1561 // on those keys is faster than calling Element::children() or looping on all
|
Chris@0
|
1562 // keys within $element, since that requires traversal of all element
|
Chris@0
|
1563 // properties.
|
Chris@0
|
1564 $variables['items'] = [];
|
Chris@0
|
1565 $delta = 0;
|
Chris@0
|
1566 while (!empty($element[$delta])) {
|
Chris@0
|
1567 $variables['items'][$delta]['content'] = $element[$delta];
|
Chris@0
|
1568
|
Chris@0
|
1569 // Modules (e.g., rdf.module) can add field item attributes (to
|
Chris@0
|
1570 // $item->_attributes) within hook_entity_prepare_view(). Some field
|
Chris@0
|
1571 // formatters move those attributes into some nested formatter-specific
|
Chris@0
|
1572 // element in order have them rendered on the desired HTML element (e.g., on
|
Chris@0
|
1573 // the <a> element of a field item being rendered as a link). Other field
|
Chris@0
|
1574 // formatters leave them within $element['#items'][$delta]['_attributes'] to
|
Chris@0
|
1575 // be rendered on the item wrappers provided by field.html.twig.
|
Chris@0
|
1576 $variables['items'][$delta]['attributes'] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : clone($default_attributes);
|
Chris@0
|
1577 $delta++;
|
Chris@0
|
1578 }
|
Chris@0
|
1579 }
|
Chris@0
|
1580
|
Chris@0
|
1581 /**
|
Chris@0
|
1582 * Prepares variables for individual form element templates.
|
Chris@0
|
1583 *
|
Chris@0
|
1584 * Default template: field-multiple-value-form.html.twig.
|
Chris@0
|
1585 *
|
Chris@0
|
1586 * Combines multiple values into a table with drag-n-drop reordering.
|
Chris@0
|
1587 *
|
Chris@0
|
1588 * @param array $variables
|
Chris@0
|
1589 * An associative array containing:
|
Chris@0
|
1590 * - element: A render element representing the form element.
|
Chris@0
|
1591 */
|
Chris@0
|
1592 function template_preprocess_field_multiple_value_form(&$variables) {
|
Chris@0
|
1593 $element = $variables['element'];
|
Chris@0
|
1594 $variables['multiple'] = $element['#cardinality_multiple'];
|
Chris@0
|
1595
|
Chris@0
|
1596 if ($variables['multiple']) {
|
Chris@0
|
1597 $table_id = Html::getUniqueId($element['#field_name'] . '_values');
|
Chris@0
|
1598 $order_class = $element['#field_name'] . '-delta-order';
|
Chris@0
|
1599 $header_attributes = new Attribute(['class' => ['label']]);
|
Chris@0
|
1600 if (!empty($element['#required'])) {
|
Chris@0
|
1601 $header_attributes['class'][] = 'js-form-required';
|
Chris@0
|
1602 $header_attributes['class'][] = 'form-required';
|
Chris@0
|
1603 }
|
Chris@0
|
1604 $header = [
|
Chris@0
|
1605 [
|
Chris@0
|
1606 'data' => [
|
Chris@0
|
1607 '#prefix' => '<h4' . $header_attributes . '>',
|
Chris@0
|
1608 '#markup' => $element['#title'],
|
Chris@0
|
1609 '#suffix' => '</h4>',
|
Chris@0
|
1610 ],
|
Chris@0
|
1611 'colspan' => 2,
|
Chris@0
|
1612 'class' => ['field-label'],
|
Chris@0
|
1613 ],
|
Chris@0
|
1614 t('Order', [], ['context' => 'Sort order']),
|
Chris@0
|
1615 ];
|
Chris@0
|
1616 $rows = [];
|
Chris@0
|
1617
|
Chris@0
|
1618 // Sort items according to '_weight' (needed when the form comes back after
|
Chris@0
|
1619 // preview or failed validation).
|
Chris@0
|
1620 $items = [];
|
Chris@0
|
1621 $variables['button'] = [];
|
Chris@0
|
1622 foreach (Element::children($element) as $key) {
|
Chris@0
|
1623 if ($key === 'add_more') {
|
Chris@0
|
1624 $variables['button'] = &$element[$key];
|
Chris@0
|
1625 }
|
Chris@0
|
1626 else {
|
Chris@0
|
1627 $items[] = &$element[$key];
|
Chris@0
|
1628 }
|
Chris@0
|
1629 }
|
Chris@0
|
1630 usort($items, '_field_multiple_value_form_sort_helper');
|
Chris@0
|
1631
|
Chris@0
|
1632 // Add the items as table rows.
|
Chris@0
|
1633 foreach ($items as $item) {
|
Chris@0
|
1634 $item['_weight']['#attributes']['class'] = [$order_class];
|
Chris@0
|
1635
|
Chris@0
|
1636 // Remove weight form element from item render array so it can be rendered
|
Chris@0
|
1637 // in a separate table column.
|
Chris@0
|
1638 $delta_element = $item['_weight'];
|
Chris@0
|
1639 unset($item['_weight']);
|
Chris@0
|
1640
|
Chris@0
|
1641 $cells = [
|
Chris@0
|
1642 ['data' => '', 'class' => ['field-multiple-drag']],
|
Chris@0
|
1643 ['data' => $item],
|
Chris@0
|
1644 ['data' => $delta_element, 'class' => ['delta-order']],
|
Chris@0
|
1645 ];
|
Chris@0
|
1646 $rows[] = [
|
Chris@0
|
1647 'data' => $cells,
|
Chris@0
|
1648 'class' => ['draggable'],
|
Chris@0
|
1649 ];
|
Chris@0
|
1650 }
|
Chris@0
|
1651
|
Chris@0
|
1652 $variables['table'] = [
|
Chris@0
|
1653 '#type' => 'table',
|
Chris@0
|
1654 '#header' => $header,
|
Chris@0
|
1655 '#rows' => $rows,
|
Chris@0
|
1656 '#attributes' => [
|
Chris@0
|
1657 'id' => $table_id,
|
Chris@0
|
1658 'class' => ['field-multiple-table'],
|
Chris@0
|
1659 ],
|
Chris@0
|
1660 '#tabledrag' => [
|
Chris@0
|
1661 [
|
Chris@0
|
1662 'action' => 'order',
|
Chris@0
|
1663 'relationship' => 'sibling',
|
Chris@0
|
1664 'group' => $order_class,
|
Chris@0
|
1665 ],
|
Chris@0
|
1666 ],
|
Chris@0
|
1667 ];
|
Chris@0
|
1668
|
Chris@0
|
1669 if (!empty($element['#description'])) {
|
Chris@0
|
1670 $description_id = $element['#attributes']['aria-describedby'];
|
Chris@0
|
1671 $description_attributes['id'] = $description_id;
|
Chris@0
|
1672 $variables['description']['attributes'] = new Attribute($description_attributes);
|
Chris@0
|
1673 $variables['description']['content'] = $element['#description'];
|
Chris@0
|
1674
|
Chris@0
|
1675 // Add the description's id to the table aria attributes.
|
Chris@0
|
1676 $variables['table']['#attributes']['aria-describedby'] = $element['#attributes']['aria-describedby'];
|
Chris@0
|
1677 }
|
Chris@0
|
1678 }
|
Chris@0
|
1679 else {
|
Chris@0
|
1680 $variables['elements'] = [];
|
Chris@0
|
1681 foreach (Element::children($element) as $key) {
|
Chris@0
|
1682 $variables['elements'][] = $element[$key];
|
Chris@0
|
1683 }
|
Chris@0
|
1684 }
|
Chris@0
|
1685 }
|
Chris@0
|
1686
|
Chris@0
|
1687 /**
|
Chris@0
|
1688 * Prepares variables for breadcrumb templates.
|
Chris@0
|
1689 *
|
Chris@0
|
1690 * Default template: breadcrumb.html.twig.
|
Chris@0
|
1691 *
|
Chris@0
|
1692 * @param array $variables
|
Chris@0
|
1693 * An associative array containing:
|
Chris@0
|
1694 * - links: A list of \Drupal\Core\Link objects which should be rendered.
|
Chris@0
|
1695 */
|
Chris@0
|
1696 function template_preprocess_breadcrumb(&$variables) {
|
Chris@0
|
1697 $variables['breadcrumb'] = [];
|
Chris@0
|
1698 /** @var \Drupal\Core\Link $link */
|
Chris@0
|
1699 foreach ($variables['links'] as $key => $link) {
|
Chris@0
|
1700 $variables['breadcrumb'][$key] = ['text' => $link->getText(), 'url' => $link->getUrl()->toString()];
|
Chris@0
|
1701 }
|
Chris@0
|
1702 }
|
Chris@0
|
1703
|
Chris@0
|
1704 /**
|
Chris@0
|
1705 * Callback for usort() within template_preprocess_field_multiple_value_form().
|
Chris@0
|
1706 *
|
Chris@0
|
1707 * Sorts using ['_weight']['#value']
|
Chris@0
|
1708 */
|
Chris@0
|
1709 function _field_multiple_value_form_sort_helper($a, $b) {
|
Chris@0
|
1710 $a_weight = (is_array($a) && isset($a['_weight']['#value']) ? $a['_weight']['#value'] : 0);
|
Chris@0
|
1711 $b_weight = (is_array($b) && isset($b['_weight']['#value']) ? $b['_weight']['#value'] : 0);
|
Chris@0
|
1712 return $a_weight - $b_weight;
|
Chris@0
|
1713 }
|
Chris@0
|
1714
|
Chris@0
|
1715 /**
|
Chris@0
|
1716 * Provides theme registration for themes across .inc files.
|
Chris@0
|
1717 */
|
Chris@0
|
1718 function drupal_common_theme() {
|
Chris@0
|
1719 return [
|
Chris@0
|
1720 // From theme.inc.
|
Chris@0
|
1721 'html' => [
|
Chris@0
|
1722 'render element' => 'html',
|
Chris@0
|
1723 ],
|
Chris@0
|
1724 'page' => [
|
Chris@0
|
1725 'render element' => 'page',
|
Chris@0
|
1726 ],
|
Chris@0
|
1727 'page_title' => [
|
Chris@0
|
1728 'variables' => ['title' => NULL],
|
Chris@0
|
1729 ],
|
Chris@0
|
1730 'region' => [
|
Chris@0
|
1731 'render element' => 'elements',
|
Chris@0
|
1732 ],
|
Chris@0
|
1733 'time' => [
|
Chris@0
|
1734 'variables' => ['timestamp' => NULL, 'text' => NULL, 'attributes' => []],
|
Chris@0
|
1735 ],
|
Chris@0
|
1736 'datetime_form' => [
|
Chris@0
|
1737 'render element' => 'element',
|
Chris@0
|
1738 ],
|
Chris@0
|
1739 'datetime_wrapper' => [
|
Chris@0
|
1740 'render element' => 'element',
|
Chris@0
|
1741 ],
|
Chris@0
|
1742 'status_messages' => [
|
Chris@0
|
1743 'variables' => ['status_headings' => [], 'message_list' => NULL],
|
Chris@0
|
1744 ],
|
Chris@0
|
1745 'links' => [
|
Chris@0
|
1746 'variables' => ['links' => [], 'attributes' => ['class' => ['links']], 'heading' => [], 'set_active_class' => FALSE],
|
Chris@0
|
1747 ],
|
Chris@0
|
1748 'dropbutton_wrapper' => [
|
Chris@0
|
1749 'variables' => ['children' => NULL],
|
Chris@0
|
1750 ],
|
Chris@0
|
1751 'image' => [
|
Chris@0
|
1752 // HTML 4 and XHTML 1.0 always require an alt attribute. The HTML 5 draft
|
Chris@0
|
1753 // allows the alt attribute to be omitted in some cases. Therefore,
|
Chris@0
|
1754 // default the alt attribute to an empty string, but allow code providing
|
Chris@0
|
1755 // variables to image.html.twig templates to pass explicit NULL for it to
|
Chris@0
|
1756 // be omitted. Usually, neither omission nor an empty string satisfies
|
Chris@0
|
1757 // accessibility requirements, so it is strongly encouraged for code
|
Chris@0
|
1758 // building variables for image.html.twig templates to pass a meaningful
|
Chris@0
|
1759 // value for the alt variable.
|
Chris@0
|
1760 // - http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
|
Chris@0
|
1761 // - http://www.w3.org/TR/xhtml1/dtds.html
|
Chris@0
|
1762 // - http://dev.w3.org/html5/spec/Overview.html#alt
|
Chris@0
|
1763 // The title attribute is optional in all cases, so it is omitted by
|
Chris@0
|
1764 // default.
|
Chris@0
|
1765 'variables' => ['uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => [], 'sizes' => NULL, 'srcset' => [], 'style_name' => NULL],
|
Chris@0
|
1766 ],
|
Chris@0
|
1767 'breadcrumb' => [
|
Chris@0
|
1768 'variables' => ['links' => []],
|
Chris@0
|
1769 ],
|
Chris@0
|
1770 'table' => [
|
Chris@0
|
1771 'variables' => ['header' => NULL, 'rows' => NULL, 'footer' => NULL, 'attributes' => [], 'caption' => NULL, 'colgroups' => [], 'sticky' => FALSE, 'responsive' => TRUE, 'empty' => ''],
|
Chris@0
|
1772 ],
|
Chris@0
|
1773 'tablesort_indicator' => [
|
Chris@0
|
1774 'variables' => ['style' => NULL],
|
Chris@0
|
1775 ],
|
Chris@0
|
1776 'mark' => [
|
Chris@0
|
1777 'variables' => ['status' => MARK_NEW],
|
Chris@0
|
1778 ],
|
Chris@0
|
1779 'item_list' => [
|
Chris@0
|
1780 'variables' => ['items' => [], 'title' => '', 'list_type' => 'ul', 'wrapper_attributes' => [], 'attributes' => [], 'empty' => NULL, 'context' => []],
|
Chris@0
|
1781 ],
|
Chris@0
|
1782 'feed_icon' => [
|
Chris@0
|
1783 'variables' => ['url' => NULL, 'title' => NULL],
|
Chris@0
|
1784 ],
|
Chris@0
|
1785 'progress_bar' => [
|
Chris@0
|
1786 'variables' => ['label' => NULL, 'percent' => NULL, 'message' => NULL],
|
Chris@0
|
1787 ],
|
Chris@0
|
1788 'indentation' => [
|
Chris@0
|
1789 'variables' => ['size' => 1],
|
Chris@0
|
1790 ],
|
Chris@0
|
1791 // From theme.maintenance.inc.
|
Chris@0
|
1792 'maintenance_page' => [
|
Chris@0
|
1793 'render element' => 'page',
|
Chris@0
|
1794 ],
|
Chris@0
|
1795 'install_page' => [
|
Chris@0
|
1796 'render element' => 'page',
|
Chris@0
|
1797 ],
|
Chris@0
|
1798 'maintenance_task_list' => [
|
Chris@0
|
1799 'variables' => ['items' => NULL, 'active' => NULL, 'variant' => NULL],
|
Chris@0
|
1800 ],
|
Chris@0
|
1801 'authorize_report' => [
|
Chris@0
|
1802 'variables' => ['messages' => [], 'attributes' => []],
|
Chris@0
|
1803 'includes' => ['core/includes/theme.maintenance.inc'],
|
Chris@0
|
1804 'template' => 'authorize-report',
|
Chris@0
|
1805 ],
|
Chris@0
|
1806 // From pager.inc.
|
Chris@0
|
1807 'pager' => [
|
Chris@0
|
1808 'render element' => 'pager',
|
Chris@0
|
1809 ],
|
Chris@0
|
1810 // From menu.inc.
|
Chris@0
|
1811 'menu' => [
|
Chris@0
|
1812 'variables' => ['menu_name' => NULL, 'items' => [], 'attributes' => []],
|
Chris@0
|
1813 ],
|
Chris@0
|
1814 'menu_local_task' => [
|
Chris@0
|
1815 'render element' => 'element',
|
Chris@0
|
1816 ],
|
Chris@0
|
1817 'menu_local_action' => [
|
Chris@0
|
1818 'render element' => 'element',
|
Chris@0
|
1819 ],
|
Chris@0
|
1820 'menu_local_tasks' => [
|
Chris@0
|
1821 'variables' => ['primary' => [], 'secondary' => []],
|
Chris@0
|
1822 ],
|
Chris@0
|
1823 // From form.inc.
|
Chris@0
|
1824 'input' => [
|
Chris@0
|
1825 'render element' => 'element',
|
Chris@0
|
1826 ],
|
Chris@0
|
1827 'select' => [
|
Chris@0
|
1828 'render element' => 'element',
|
Chris@0
|
1829 ],
|
Chris@0
|
1830 'fieldset' => [
|
Chris@0
|
1831 'render element' => 'element',
|
Chris@0
|
1832 ],
|
Chris@0
|
1833 'details' => [
|
Chris@0
|
1834 'render element' => 'element',
|
Chris@0
|
1835 ],
|
Chris@0
|
1836 'radios' => [
|
Chris@0
|
1837 'render element' => 'element',
|
Chris@0
|
1838 ],
|
Chris@0
|
1839 'checkboxes' => [
|
Chris@0
|
1840 'render element' => 'element',
|
Chris@0
|
1841 ],
|
Chris@0
|
1842 'form' => [
|
Chris@0
|
1843 'render element' => 'element',
|
Chris@0
|
1844 ],
|
Chris@0
|
1845 'textarea' => [
|
Chris@0
|
1846 'render element' => 'element',
|
Chris@0
|
1847 ],
|
Chris@0
|
1848 'form_element' => [
|
Chris@0
|
1849 'render element' => 'element',
|
Chris@0
|
1850 ],
|
Chris@0
|
1851 'form_element_label' => [
|
Chris@0
|
1852 'render element' => 'element',
|
Chris@0
|
1853 ],
|
Chris@0
|
1854 'vertical_tabs' => [
|
Chris@0
|
1855 'render element' => 'element',
|
Chris@0
|
1856 ],
|
Chris@0
|
1857 'container' => [
|
Chris@0
|
1858 'render element' => 'element',
|
Chris@0
|
1859 ],
|
Chris@0
|
1860 // From field system.
|
Chris@0
|
1861 'field' => [
|
Chris@0
|
1862 'render element' => 'element',
|
Chris@0
|
1863 ],
|
Chris@0
|
1864 'field_multiple_value_form' => [
|
Chris@0
|
1865 'render element' => 'element',
|
Chris@0
|
1866 ],
|
Chris@0
|
1867 ];
|
Chris@0
|
1868 }
|