annotate core/includes/theme.inc @ 17:129ea1e6d783

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