annotate core/includes/theme.inc @ 19:fa3358dc1485 tip

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