theme.inc 68 KB

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