theme.inc 69 KB

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