Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Render/theme.api.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /** | |
4 * @file | |
5 * Hooks and documentation related to the theme and render system. | |
6 */ | |
7 | |
8 /** | |
9 * @defgroup themeable Theme system overview | |
10 * @{ | |
11 * Functions and templates for the user interface that themes can override. | |
12 * | |
13 * Drupal's theme system allows a theme to have nearly complete control over | |
14 * the appearance of the site, which includes both the markup and the CSS used | |
15 * to style the markup. For this system to work, modules, instead of writing | |
16 * HTML markup directly, need to return "render arrays", which are structured | |
17 * hierarchical arrays that include the data to be rendered into HTML (or XML or | |
18 * another output format), and options that affect the markup. Render arrays | |
19 * are ultimately rendered into HTML or other output formats by recursive calls | |
20 * to drupal_render(), traversing the depth of the render array hierarchy. At | |
21 * each level, the theme system is invoked to do the actual rendering. See the | |
22 * documentation of drupal_render() and the | |
23 * @link theme_render Theme system and Render API topic @endlink for more | |
24 * information about render arrays and rendering. | |
25 * | |
26 * @section sec_twig_theme Twig Templating Engine | |
27 * Drupal 8 uses the templating engine Twig. Twig offers developers a fast, | |
28 * secure, and flexible method for building templates for Drupal 8 sites. Twig | |
29 * also offers substantial usability improvements over PHPTemplate, and does | |
30 * not require front-end developers to know PHP to build and manipulate Drupal | |
31 * 8 themes. | |
32 * | |
33 * For further information on theming in Drupal 8 see | |
34 * https://www.drupal.org/docs/8/theming | |
35 * | |
36 * For further Twig documentation see | |
37 * http://twig.sensiolabs.org/doc/templates.html | |
38 * | |
39 * @section sec_theme_hooks Theme Hooks | |
40 * The theme system is invoked in \Drupal\Core\Render\Renderer::doRender() by | |
41 * calling the \Drupal\Core\Theme\ThemeManagerInterface::render() function, | |
42 * which operates on the concept of "theme hooks". Theme hooks define how a | |
43 * particular type of data should be rendered. They are registered by modules by | |
44 * implementing hook_theme(), which specifies the name of the hook, the input | |
45 * "variables" used to provide data and options, and other information. Modules | |
46 * implementing hook_theme() also need to provide a default implementation for | |
47 * each of their theme hooks, normally in a Twig file, and they may also provide | |
48 * preprocessing functions. For example, the core Search module defines a theme | |
49 * hook for a search result item in search_theme(): | |
50 * @code | |
51 * return array( | |
52 * 'search_result' => array( | |
53 * 'variables' => array( | |
54 * 'result' => NULL, | |
55 * 'plugin_id' => NULL, | |
56 * ), | |
57 * 'file' => 'search.pages.inc', | |
58 * ), | |
59 * ); | |
60 * @endcode | |
61 * Given this definition, the template file with the default implementation is | |
62 * search-result.html.twig, which can be found in the | |
63 * core/modules/search/templates directory, and the variables for rendering are | |
64 * the search result and the plugin ID. In addition, there is a function | |
65 * template_preprocess_search_result(), located in file search.pages.inc, which | |
66 * preprocesses the information from the input variables so that it can be | |
67 * rendered by the Twig template; the processed variables that the Twig template | |
68 * receives are documented in the header of the default Twig template file. | |
69 * | |
70 * hook_theme() implementations can also specify that a theme hook | |
71 * implementation is a theme function, but that is uncommon and not recommended. | |
72 * Note that while Twig templates will auto-escape variables, theme functions | |
73 * must explicitly escape any variables by using theme_render_and_autoescape(). | |
74 * Failure to do so is likely to result in security vulnerabilities. Theme | |
75 * functions are deprecated in Drupal 8.0.x and will be removed before | |
76 * Drupal 9.0.x. Use Twig templates instead. | |
77 * | |
78 * @section sec_overriding_theme_hooks Overriding Theme Hooks | |
79 * Themes may register new theme hooks within a hook_theme() implementation, but | |
80 * it is more common for themes to override default implementations provided by | |
81 * modules than to register entirely new theme hooks. Themes can override a | |
82 * default implementation by creating a template file with the same name as the | |
83 * default implementation; for example, to override the display of search | |
84 * results, a theme would add a file called search-result.html.twig to its | |
85 * templates directory. A good starting point for doing this is normally to | |
86 * copy the default implementation template, and then modifying it as desired. | |
87 * | |
88 * In the uncommon case that a theme hook uses a theme function instead of a | |
89 * template file, a module would provide a default implementation function | |
90 * called theme_HOOK, where HOOK is the name of the theme hook (for example, | |
91 * theme_search_result() would be the name of the function for search result | |
92 * theming). In this case, a theme can override the default implementation by | |
93 * defining a function called THEME_HOOK() in its THEME.theme file, where THEME | |
94 * is the machine name of the theme (for example, 'bartik' is the machine name | |
95 * of the core Bartik theme, and it would define a function called | |
96 * bartik_search_result() in the bartik.theme file, if the search_result hook | |
97 * implementation was a function instead of a template). Normally, copying the | |
98 * default function is again a good starting point for overriding its behavior. | |
99 * Again, note that theme functions (unlike templates) must explicitly escape | |
100 * variables using theme_render_and_autoescape() or risk security | |
101 * vulnerabilities. Theme functions are deprecated in Drupal 8.0.x and will be | |
102 * removed before Drupal 9.0.x. Use Twig templates instead. | |
103 * | |
104 * @section sec_preprocess_templates Preprocessing for Template Files | |
105 * If the theme implementation is a template file, several functions are called | |
106 * before the template file is invoked to modify the variables that are passed | |
107 * to the template. These make up the "preprocessing" phase, and are executed | |
108 * (if they exist), in the following order (note that in the following list, | |
109 * HOOK indicates the hook being called or a less specific hook. For example, if | |
110 * '#theme' => 'node__article' is called, hook is node__article and node. MODULE | |
111 * indicates a module name, THEME indicates a theme name, and ENGINE indicates a | |
112 * theme engine name). Modules, themes, and theme engines can provide these | |
113 * functions to modify how the data is preprocessed, before it is passed to the | |
114 * theme template: | |
115 * - template_preprocess(&$variables, $hook): Creates a default set of variables | |
116 * for all theme hooks with template implementations. Provided by Drupal Core. | |
117 * - template_preprocess_HOOK(&$variables): Should be implemented by the module | |
118 * that registers the theme hook, to set up default variables. | |
119 * - MODULE_preprocess(&$variables, $hook): hook_preprocess() is invoked on all | |
120 * implementing modules. | |
121 * - MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on | |
122 * all implementing modules, so that modules that didn't define the theme hook | |
123 * can alter the variables. | |
124 * - ENGINE_engine_preprocess(&$variables, $hook): Allows the theme engine to | |
125 * set necessary variables for all theme hooks with template implementations. | |
126 * - ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set | |
127 * necessary variables for the particular theme hook. | |
128 * - THEME_preprocess(&$variables, $hook): Allows the theme to set necessary | |
129 * variables for all theme hooks with template implementations. | |
130 * - THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary | |
131 * variables specific to the particular theme hook. | |
132 * | |
133 * @section sec_preprocess_functions Preprocessing for Theme Functions | |
134 * If the theming implementation is a function, only the theme-hook-specific | |
135 * preprocess functions (the ones ending in _HOOK) are called from the list | |
136 * above. This is because theme hooks with function implementations need to be | |
137 * fast, and calling the non-theme-hook-specific preprocess functions for them | |
138 * would incur a noticeable performance penalty. | |
139 * | |
140 * @section sec_suggestions Theme hook suggestions | |
141 * In some cases, instead of calling the base theme hook implementation (either | |
142 * the default provided by the module that defined the hook, or the override | |
143 * provided by the theme), the theme system will instead look for "suggestions" | |
144 * of other hook names to look for. Suggestions can be specified in several | |
145 * ways: | |
146 * - In a render array, the '#theme' property (which gives the name of the hook | |
147 * to use) can be an array of theme hook names instead of a single hook name. | |
148 * In this case, the render system will look first for the highest-priority | |
149 * hook name, and if no implementation is found, look for the second, and so | |
150 * on. Note that the highest-priority suggestion is at the end of the array. | |
151 * - In a render array, the '#theme' property can be set to the name of a hook | |
152 * with a '__SUGGESTION' suffix. For example, in search results theming, the | |
153 * hook 'item_list__search_results' is given. In this case, the render system | |
154 * will look for theme templates called item-list--search-results.html.twig, | |
155 * which would only be used for rendering item lists containing search | |
156 * results, and if this template is not found, it will fall back to using the | |
157 * base item-list.html.twig template. This type of suggestion can also be | |
158 * combined with providing an array of theme hook names as described above. | |
159 * - A module can implement hook_theme_suggestions_HOOK(). This allows the | |
160 * module that defines the theme template to dynamically return an array | |
161 * containing specific theme hook names (presumably with '__' suffixes as | |
162 * defined above) to use as suggestions. For example, the Search module | |
163 * does this in search_theme_suggestions_search_result() to suggest | |
164 * search_result__PLUGIN as the theme hook for search result items, where | |
165 * PLUGIN is the machine name of the particular search plugin type that was | |
166 * used for the search (such as node_search or user_search). | |
167 * | |
168 * For further information on overriding theme hooks see | |
169 * https://www.drupal.org/node/2186401 | |
170 * | |
171 * @section sec_alternate_suggestions Altering theme hook suggestions | |
172 * Modules can also alter the theme suggestions provided using the mechanisms | |
173 * of the previous section. There are two hooks for this: the | |
174 * theme-hook-specific hook_theme_suggestions_HOOK_alter() and the generic | |
175 * hook_theme_suggestions_alter(). These hooks get the current list of | |
176 * suggestions as input, and can change this array (adding suggestions and | |
177 * removing them). | |
178 * | |
179 * @section assets Assets | |
180 * We can distinguish between three types of assets: | |
181 * - Unconditional page-level assets (loaded on all pages where the theme is in | |
182 * use): these are defined in the theme's *.info.yml file. | |
183 * - Conditional page-level assets (loaded on all pages where the theme is in | |
184 * use and a certain condition is met): these are attached in | |
185 * hook_page_attachments_alter(), e.g.: | |
186 * @code | |
187 * function THEME_page_attachments_alter(array &$page) { | |
188 * if ($some_condition) { | |
189 * $page['#attached']['library'][] = 'mytheme/something'; | |
190 * } | |
191 * } | |
192 * @endcode | |
193 * - Template-specific assets (loaded on all pages where a specific template is | |
194 * in use): these can be added by in preprocessing functions, using @code | |
195 * $variables['#attached'] @endcode, e.g.: | |
196 * @code | |
197 * function THEME_preprocess_menu_local_action(array &$variables) { | |
198 * // We require Modernizr's touch test for button styling. | |
199 * $variables['#attached']['library'][] = 'core/modernizr'; | |
200 * } | |
201 * @endcode | |
202 * | |
203 * @see hooks | |
204 * @see callbacks | |
205 * @see theme_render | |
206 * | |
207 * @} | |
208 */ | |
209 | |
210 /** | |
211 * @defgroup theme_render Render API overview | |
212 * @{ | |
213 * Overview of the Theme system and Render API. | |
214 * | |
215 * The main purpose of Drupal's Theme system is to give themes complete control | |
216 * over the appearance of the site, which includes the markup returned from HTTP | |
217 * requests and the CSS files used to style that markup. In order to ensure that | |
218 * a theme can completely customize the markup, module developers should avoid | |
219 * directly writing HTML markup for pages, blocks, and other user-visible output | |
220 * in their modules, and instead return structured "render arrays" (see | |
221 * @ref arrays below). Doing this also increases usability, by ensuring that the | |
222 * markup used for similar functionality on different areas of the site is the | |
223 * same, which gives users fewer user interface patterns to learn. | |
224 * | |
225 * For further information on the Theme and Render APIs, see: | |
226 * - https://www.drupal.org/docs/8/theming | |
227 * - https://www.drupal.org/developing/api/8/render | |
228 * - @link themeable Theme system overview @endlink. | |
229 * | |
230 * @section arrays Render arrays | |
231 * The core structure of the Render API is the render array, which is a | |
232 * hierarchical associative array containing data to be rendered and properties | |
233 * describing how the data should be rendered. A render array that is returned | |
234 * by a function to specify markup to be sent to the web browser or other | |
235 * services will eventually be rendered by a call to drupal_render(), which will | |
236 * recurse through the render array hierarchy if appropriate, making calls into | |
237 * the theme system to do the actual rendering. If a function or method actually | |
238 * needs to return rendered output rather than a render array, the best practice | |
239 * would be to create a render array, render it by calling drupal_render(), and | |
240 * return that result, rather than writing the markup directly. See the | |
241 * documentation of drupal_render() for more details of the rendering process. | |
242 * | |
243 * Each level in the hierarchy of a render array (including the outermost array) | |
244 * has one or more array elements. Array elements whose names start with '#' are | |
245 * known as "properties", and the array elements with other names are "children" | |
246 * (constituting the next level of the hierarchy); the names of children are | |
247 * flexible, while property names are specific to the Render API and the | |
248 * particular type of data being rendered. A special case of render arrays is a | |
249 * form array, which specifies the form elements for an HTML form; see the | |
250 * @link form_api Form generation topic @endlink for more information on forms. | |
251 * | |
252 * Render arrays (at any level of the hierarchy) will usually have one of the | |
253 * following properties defined: | |
254 * - #type: Specifies that the array contains data and options for a particular | |
255 * type of "render element" (for example, 'form', for an HTML form; | |
256 * 'textfield', 'submit', for HTML form element types; 'table', for a table | |
257 * with rows, columns, and headers). See @ref elements below for more on | |
258 * render element types. | |
259 * - #theme: Specifies that the array contains data to be themed by a particular | |
260 * theme hook. Modules define theme hooks by implementing hook_theme(), which | |
261 * specifies the input "variables" used to provide data and options; if a | |
262 * hook_theme() implementation specifies variable 'foo', then in a render | |
263 * array, you would provide this data using property '#foo'. Modules | |
264 * implementing hook_theme() also need to provide a default implementation for | |
265 * each of their theme hooks, normally in a Twig file. For more information | |
266 * and to discover available theme hooks, see the documentation of | |
267 * hook_theme() and the | |
268 * @link themeable Default theme implementations topic. @endlink | |
269 * - #markup: Specifies that the array provides HTML markup directly. Unless | |
270 * the markup is very simple, such as an explanation in a paragraph tag, it | |
271 * is normally preferable to use #theme or #type instead, so that the theme | |
272 * can customize the markup. Note that the value is passed through | |
273 * \Drupal\Component\Utility\Xss::filterAdmin(), which strips known XSS | |
274 * vectors while allowing a permissive list of HTML tags that are not XSS | |
275 * vectors. (For example, <script> and <style> are not allowed.) See | |
276 * \Drupal\Component\Utility\Xss::$adminTags for the list of allowed tags. If | |
277 * your markup needs any of the tags not in this whitelist, then you can | |
278 * implement a theme hook and/or an asset library. Alternatively, you can use | |
279 * the key #allowed_tags to alter which tags are filtered. | |
280 * - #plain_text: Specifies that the array provides text that needs to be | |
281 * escaped. This value takes precedence over #markup. | |
282 * - #allowed_tags: If #markup is supplied, this can be used to change which | |
283 * tags are allowed in the markup. The value is an array of tags that | |
284 * Xss::filter() would accept. If #plain_text is set, this value is ignored. | |
285 * | |
286 * Usage example: | |
287 * @code | |
288 * $output['admin_filtered_string'] = [ | |
289 * '#markup' => '<em>This is filtered using the admin tag list</em>', | |
290 * ]; | |
291 * $output['filtered_string'] = [ | |
292 * '#markup' => '<video><source src="v.webm" type="video/webm"></video>', | |
293 * '#allowed_tags' => ['video', 'source'], | |
294 * ]; | |
295 * $output['escaped_string'] = [ | |
296 * '#plain_text' => '<em>This is escaped</em>', | |
297 * ]; | |
298 * @endcode | |
299 * | |
300 * @see core.libraries.yml | |
301 * @see hook_theme() | |
302 * | |
303 * JavaScript and CSS assets are specified in the render array using the | |
304 * #attached property (see @ref sec_attached). | |
305 * | |
306 * @section elements Render elements | |
307 * Render elements are defined by Drupal core and modules. The primary way to | |
308 * define a render element is to create a render element plugin. There are | |
309 * two types of render element plugins: | |
310 * - Generic elements: Generic render element plugins implement | |
311 * \Drupal\Core\Render\Element\ElementInterface, are annotated with | |
312 * \Drupal\Core\Render\Annotation\RenderElement annotation, go in plugin | |
313 * namespace Element, and generally extend the | |
314 * \Drupal\Core\Render\Element\RenderElement base class. | |
315 * - Form input elements: Render elements representing form input elements | |
316 * implement \Drupal\Core\Render\Element\FormElementInterface, are annotated | |
317 * with \Drupal\Core\Render\Annotation\FormElement annotation, go in plugin | |
318 * namespace Element, and generally extend the | |
319 * \Drupal\Core\Render\Element\FormElement base class. | |
320 * See the @link plugin_api Plugin API topic @endlink for general information | |
321 * on plugins. You can search for classes with the RenderElement or FormElement | |
322 * annotation to discover what render elements are available. API reference | |
323 * sites (such as https://api.drupal.org) generate lists of all existing | |
324 * elements from these classes. Look for the Elements link in the API Navigation | |
325 * block. | |
326 * | |
327 * Modules can define render elements by defining an element plugin. | |
328 * | |
329 * @section sec_caching Caching | |
330 * The Drupal rendering process has the ability to cache rendered output at any | |
331 * level in a render array hierarchy. This allows expensive calculations to be | |
332 * done infrequently, and speeds up page loading. See the | |
333 * @link cache Cache API topic @endlink for general information about the cache | |
334 * system. | |
335 * | |
336 * In order to make caching possible, the following information needs to be | |
337 * present: | |
338 * - Cache keys: Identifiers for cacheable portions of render arrays. These | |
339 * should be created and added for portions of a render array that | |
340 * involve expensive calculations in the rendering process. | |
341 * - Cache contexts: Contexts that may affect rendering, such as user role and | |
342 * language. When no context is specified, it means that the render array | |
343 * does not vary by any context. | |
344 * - Cache tags: Tags for data that rendering depends on, such as for | |
345 * individual nodes or user accounts, so that when these change the cache | |
346 * can be automatically invalidated. If the data consists of entities, you | |
347 * can use \Drupal\Core\Entity\EntityInterface::getCacheTags() to generate | |
348 * appropriate tags; configuration objects have a similar method. | |
349 * - Cache max-age: The maximum duration for which a render array may be cached. | |
350 * Defaults to \Drupal\Core\Cache\Cache::PERMANENT (permanently cacheable). | |
351 * | |
352 * Cache information is provided in the #cache property in a render array. In | |
353 * this property, always supply the cache contexts, tags, and max-age if a | |
354 * render array varies by context, depends on some modifiable data, or depends | |
355 * on information that's only valid for a limited time, respectively. Cache keys | |
356 * should only be set on the portions of a render array that should be cached. | |
357 * Contexts are automatically replaced with the value for the current request | |
358 * (e.g. the current language) and combined with the keys to form a cache ID. | |
359 * The cache contexts, tags, and max-age will be propagated up the render array | |
360 * hierarchy to determine cacheability for containing render array sections. | |
361 * | |
362 * Here's an example of what a #cache property might contain: | |
363 * @code | |
364 * '#cache' => [ | |
365 * 'keys' => ['entity_view', 'node', $node->id()], | |
366 * 'contexts' => ['languages'], | |
367 * 'tags' => $node->getCacheTags(), | |
368 * 'max-age' => Cache::PERMANENT, | |
369 * ], | |
370 * @endcode | |
371 * | |
372 * At the response level, you'll see X-Drupal-Cache-Contexts and | |
373 * X-Drupal-Cache-Tags headers. | |
374 * | |
375 * See https://www.drupal.org/developing/api/8/render/arrays/cacheability for | |
376 * details. | |
377 * | |
378 * @section sec_attached Attaching libraries in render arrays | |
379 * Libraries, JavaScript settings, feeds, HTML <head> tags and HTML <head> links | |
380 * are attached to elements using the #attached property. The #attached property | |
381 * is an associative array, where the keys are the attachment types and the | |
382 * values are the attached data. | |
383 * | |
384 * The #attached property can also be used to specify HTTP headers and the | |
385 * response status code. | |
386 * | |
387 * The #attached property allows loading of asset libraries (which may contain | |
388 * CSS assets, JavaScript assets, and JavaScript setting assets), JavaScript | |
389 * settings, feeds, HTML <head> tags and HTML <head> links. Specify an array of | |
390 * type => value pairs, where the type (most often 'library' — for libraries, or | |
391 * 'drupalSettings' — for JavaScript settings) to attach these response-level | |
392 * values. Example: | |
393 * @code | |
394 * $build['#attached']['library'][] = 'core/jquery'; | |
395 * $build['#attached']['drupalSettings']['foo'] = 'bar'; | |
396 * $build['#attached']['feed'][] = [$url, $this->t('Feed title')]; | |
397 * @endcode | |
398 * | |
399 * See \Drupal\Core\Render\AttachmentsResponseProcessorInterface for additional | |
400 * information. | |
401 * | |
402 * See \Drupal\Core\Asset\LibraryDiscoveryParser::parseLibraryInfo() for more | |
403 * information on how to define libraries. | |
404 * | |
405 * @section sec_placeholders Placeholders in render arrays | |
406 * Render arrays have a placeholder mechanism, which can be used to add data | |
407 * into the render array late in the rendering process. This works in a similar | |
408 * manner to \Drupal\Component\Render\FormattableMarkup::placeholderFormat(), | |
409 * with the text that ends up in the #markup property of the element at the | |
410 * end of the rendering process getting substitutions from placeholders that | |
411 * are stored in the 'placeholders' element of the #attached property. | |
412 * | |
413 * For example, after the rest of the rendering process was done, if your | |
414 * render array contained: | |
415 * @code | |
416 * $build['my_element'] = [ | |
417 * '#attached' => ['placeholders' => ['@foo' => 'replacement']], | |
418 * '#markup' => ['Something about @foo'], | |
419 * ]; | |
420 * @endcode | |
421 * then #markup would end up containing 'Something about replacement'. | |
422 * | |
423 * Note that each placeholder value can itself be a render array, which will be | |
424 * rendered, and any cache tags generated during rendering will be added to the | |
425 * cache tags for the markup. | |
426 * | |
427 * @section render_pipeline The render pipeline | |
428 * The term "render pipeline" refers to the process Drupal uses to take | |
429 * information provided by modules and render it into a response. See | |
430 * https://www.drupal.org/developing/api/8/render for more details on this | |
431 * process. For background on routing concepts, see | |
432 * @link routing Routing API. @endlink | |
433 * | |
434 * There are in fact multiple render pipelines: | |
435 * - Drupal always uses the Symfony render pipeline. See | |
436 * http://symfony.com/doc/2.7/components/http_kernel/introduction.html | |
437 * - Within the Symfony render pipeline, there is a Drupal render pipeline, | |
438 * which handles controllers that return render arrays. (Symfony's render | |
439 * pipeline only knows how to deal with Response objects; this pipeline | |
440 * converts render arrays into Response objects.) These render arrays are | |
441 * considered the main content, and can be rendered into multiple formats: | |
442 * HTML, Ajax, dialog, and modal. Modules can add support for more formats, by | |
443 * implementing a main content renderer, which is a service tagged with | |
444 * 'render.main_content_renderer'. | |
445 * - Finally, within the HTML main content renderer, there is another pipeline, | |
446 * to allow for rendering the page containing the main content in multiple | |
447 * ways: no decoration at all (just a page showing the main content) or blocks | |
448 * (a page with regions, with blocks positioned in regions around the main | |
449 * content). Modules can provide additional options, by implementing a page | |
450 * variant, which is a plugin annotated with | |
451 * \Drupal\Core\Display\Annotation\PageDisplayVariant. | |
452 * | |
453 * Routes whose controllers return a \Symfony\Component\HttpFoundation\Response | |
454 * object are fully handled by the Symfony render pipeline. | |
455 * | |
456 * Routes whose controllers return the "main content" as a render array can be | |
457 * requested in multiple formats (HTML, JSON, etc.) and/or in a "decorated" | |
458 * manner, as described above. | |
459 * | |
460 * @see themeable | |
461 * @see \Symfony\Component\HttpKernel\KernelEvents::VIEW | |
462 * @see \Drupal\Core\EventSubscriber\MainContentViewSubscriber | |
463 * @see \Drupal\Core\Render\MainContent\MainContentRendererInterface | |
464 * @see \Drupal\Core\Render\MainContent\HtmlRenderer | |
465 * @see \Drupal\Core\Render\RenderEvents::SELECT_PAGE_DISPLAY_VARIANT | |
466 * @see \Drupal\Core\Render\Plugin\DisplayVariant\SimplePageVariant | |
467 * @see \Drupal\block\Plugin\DisplayVariant\BlockPageVariant | |
468 * @see \Drupal\Core\Render\BareHtmlPageRenderer | |
469 * | |
470 * @} | |
471 */ | |
472 | |
473 /** | |
474 * @defgroup listing_page_element Page header for Elements page | |
475 * @{ | |
476 * Introduction to form and render elements | |
477 * | |
478 * Render elements are referenced in render arrays. Render arrays contain data | |
479 * to be rendered, along with meta-data and attributes that specify how to | |
480 * render the data into markup; see the | |
481 * @link theme_render Render API topic @endlink for an overview of render | |
482 * arrays and render elements. Form arrays are a subset of render arrays, | |
483 * representing HTML forms; form elements are a subset of render elements, | |
484 * representing HTML elements for forms. See the | |
485 * @link form_api Form API topic @endlink for an overview of forms, form | |
486 * processing, and form arrays. | |
487 * | |
488 * Each form and render element type corresponds to an element plugin class; | |
489 * each of them either extends \Drupal\Core\Render\Element\RenderElement | |
490 * (render elements) or \Drupal\Core\Render\Element\FormElement (form | |
491 * elements). Usage and properties are documented on the individual classes, | |
492 * and the two base classes list common properties shared by all render | |
493 * elements and the form element subset, respectively. | |
494 * | |
495 * @see theme_render | |
496 * @see form_api | |
497 * @see \Drupal\Core\Render\Element\RenderElement | |
498 * @see \Drupal\Core\Render\Element\FormElement | |
499 * | |
500 * @} | |
501 */ | |
502 | |
503 /** | |
504 * @addtogroup hooks | |
505 * @{ | |
506 */ | |
507 | |
508 /** | |
509 * Allow themes to alter the theme-specific settings form. | |
510 * | |
511 * With this hook, themes can alter the theme-specific settings form in any way | |
512 * allowable by Drupal's Form API, such as adding form elements, changing | |
513 * default values and removing form elements. See the Form API documentation on | |
514 * api.drupal.org for detailed information. | |
515 * | |
516 * Note that the base theme's form alterations will be run before any sub-theme | |
517 * alterations. | |
518 * | |
519 * @param $form | |
520 * Nested array of form elements that comprise the form. | |
521 * @param $form_state | |
522 * The current state of the form. | |
523 */ | |
524 function hook_form_system_theme_settings_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) { | |
525 // Add a checkbox to toggle the breadcrumb trail. | |
526 $form['toggle_breadcrumb'] = [ | |
527 '#type' => 'checkbox', | |
528 '#title' => t('Display the breadcrumb'), | |
529 '#default_value' => theme_get_setting('features.breadcrumb'), | |
530 '#description' => t('Show a trail of links from the homepage to the current page.'), | |
531 ]; | |
532 } | |
533 | |
534 /** | |
535 * Preprocess theme variables for templates. | |
536 * | |
537 * This hook allows modules to preprocess theme variables for theme templates. | |
538 * It is called for all theme hooks implemented as templates, but not for theme | |
539 * hooks implemented as functions. hook_preprocess_HOOK() can be used to | |
540 * preprocess variables for a specific theme hook, whether implemented as a | |
541 * template or function. | |
542 * | |
543 * For more detailed information, see the | |
544 * @link themeable Theme system overview topic @endlink. | |
545 * | |
546 * @param $variables | |
547 * The variables array (modify in place). | |
548 * @param $hook | |
549 * The name of the theme hook. | |
550 */ | |
551 function hook_preprocess(&$variables, $hook) { | |
552 static $hooks; | |
553 | |
554 // Add contextual links to the variables, if the user has permission. | |
555 | |
556 if (!\Drupal::currentUser()->hasPermission('access contextual links')) { | |
557 return; | |
558 } | |
559 | |
560 if (!isset($hooks)) { | |
561 $hooks = theme_get_registry(); | |
562 } | |
563 | |
564 // Determine the primary theme function argument. | |
565 if (isset($hooks[$hook]['variables'])) { | |
566 $keys = array_keys($hooks[$hook]['variables']); | |
567 $key = $keys[0]; | |
568 } | |
569 else { | |
570 $key = $hooks[$hook]['render element']; | |
571 } | |
572 | |
573 if (isset($variables[$key])) { | |
574 $element = $variables[$key]; | |
575 } | |
576 | |
577 if (isset($element) && is_array($element) && !empty($element['#contextual_links'])) { | |
578 $variables['title_suffix']['contextual_links'] = contextual_links_view($element); | |
579 if (!empty($variables['title_suffix']['contextual_links'])) { | |
580 $variables['attributes']['class'][] = 'contextual-links-region'; | |
581 } | |
582 } | |
583 } | |
584 | |
585 /** | |
586 * Preprocess theme variables for a specific theme hook. | |
587 * | |
588 * This hook allows modules to preprocess theme variables for a specific theme | |
589 * hook. It should only be used if a module needs to override or add to the | |
590 * theme preprocessing for a theme hook it didn't define. | |
591 * | |
592 * For more detailed information, see the | |
593 * @link themeable Theme system overview topic @endlink. | |
594 * | |
595 * @param $variables | |
596 * The variables array (modify in place). | |
597 */ | |
598 function hook_preprocess_HOOK(&$variables) { | |
599 // This example is from rdf_preprocess_image(). It adds an RDF attribute | |
600 // to the image hook's variables. | |
601 $variables['attributes']['typeof'] = ['foaf:Image']; | |
602 } | |
603 | |
604 /** | |
605 * Provides alternate named suggestions for a specific theme hook. | |
606 * | |
607 * This hook allows modules to provide alternative theme function or template | |
608 * name suggestions. | |
609 * | |
610 * HOOK is the least-specific version of the hook being called. For example, if | |
611 * '#theme' => 'node__article' is called, then hook_theme_suggestions_node() | |
612 * will be invoked, not hook_theme_suggestions_node__article(). The specific | |
613 * hook called (in this case 'node__article') is available in | |
614 * $variables['theme_hook_original']. | |
615 * | |
616 * @todo Add @code sample. | |
617 * | |
618 * @param array $variables | |
619 * An array of variables passed to the theme hook. Note that this hook is | |
620 * invoked before any preprocessing. | |
621 * | |
622 * @return array | |
623 * An array of theme suggestions. | |
624 * | |
625 * @see hook_theme_suggestions_HOOK_alter() | |
626 */ | |
627 function hook_theme_suggestions_HOOK(array $variables) { | |
628 $suggestions = []; | |
629 | |
630 $suggestions[] = 'hookname__' . $variables['elements']['#langcode']; | |
631 | |
632 return $suggestions; | |
633 } | |
634 | |
635 /** | |
636 * Alters named suggestions for all theme hooks. | |
637 * | |
638 * This hook is invoked for all theme hooks, if you are targeting a specific | |
639 * theme hook it's best to use hook_theme_suggestions_HOOK_alter(). | |
640 * | |
641 * The call order is as follows: all existing suggestion alter functions are | |
642 * called for module A, then all for module B, etc., followed by all for any | |
643 * base theme(s), and finally for the active theme. The order is | |
644 * determined by system weight, then by extension (module or theme) name. | |
645 * | |
646 * Within each module or theme, suggestion alter hooks are called in the | |
647 * following order: first, hook_theme_suggestions_alter(); second, | |
648 * hook_theme_suggestions_HOOK_alter(). So, for each module or theme, the more | |
649 * general hooks are called first followed by the more specific. | |
650 * | |
651 * In the following example, we provide an alternative template suggestion to | |
652 * node and taxonomy term templates based on the user being logged in. | |
653 * @code | |
654 * function MYMODULE_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { | |
655 * if (\Drupal::currentUser()->isAuthenticated() && in_array($hook, array('node', 'taxonomy_term'))) { | |
656 * $suggestions[] = $hook . '__' . 'logged_in'; | |
657 * } | |
658 * } | |
659 * | |
660 * @endcode | |
661 * | |
662 * @param array $suggestions | |
663 * An array of alternate, more specific names for template files or theme | |
664 * functions. | |
665 * @param array $variables | |
666 * An array of variables passed to the theme hook. Note that this hook is | |
667 * invoked before any variable preprocessing. | |
668 * @param string $hook | |
669 * The base hook name. For example, if '#theme' => 'node__article' is called, | |
670 * then $hook will be 'node', not 'node__article'. The specific hook called | |
671 * (in this case 'node__article') is available in | |
672 * $variables['theme_hook_original']. | |
673 * | |
674 * @return array | |
675 * An array of theme suggestions. | |
676 * | |
677 * @see hook_theme_suggestions_HOOK_alter() | |
678 */ | |
679 function hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook) { | |
680 // Add an interface-language specific suggestion to all theme hooks. | |
681 $suggestions[] = $hook . '__' . \Drupal::languageManager()->getCurrentLanguage()->getId(); | |
682 } | |
683 | |
684 /** | |
685 * Alters named suggestions for a specific theme hook. | |
686 * | |
687 * This hook allows any module or theme to provide alternative theme function or | |
688 * template name suggestions and reorder or remove suggestions provided by | |
689 * hook_theme_suggestions_HOOK() or by earlier invocations of this hook. | |
690 * | |
691 * HOOK is the least-specific version of the hook being called. For example, if | |
692 * '#theme' => 'node__article' is called, then node_theme_suggestions_node() | |
693 * will be invoked, not node_theme_suggestions_node__article(). The specific | |
694 * hook called (in this case 'node__article') is available in | |
695 * $variables['theme_hook_original']. | |
696 * | |
697 * @todo Add @code sample. | |
698 * | |
699 * @param array $suggestions | |
700 * An array of theme suggestions. | |
701 * @param array $variables | |
702 * An array of variables passed to the theme hook. Note that this hook is | |
703 * invoked before any preprocessing. | |
704 * | |
705 * @see hook_theme_suggestions_alter() | |
706 * @see hook_theme_suggestions_HOOK() | |
707 */ | |
708 function hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) { | |
709 if (empty($variables['header'])) { | |
710 $suggestions[] = 'hookname__' . 'no_header'; | |
711 } | |
712 } | |
713 | |
714 /** | |
715 * Respond to themes being installed. | |
716 * | |
717 * @param array $theme_list | |
718 * Array containing the names of the themes being installed. | |
719 * | |
720 * @see \Drupal\Core\Extension\ThemeHandler::install() | |
721 */ | |
722 function hook_themes_installed($theme_list) { | |
723 foreach ($theme_list as $theme) { | |
724 block_theme_initialize($theme); | |
725 } | |
726 } | |
727 | |
728 /** | |
729 * Respond to themes being uninstalled. | |
730 * | |
731 * @param array $themes | |
732 * Array containing the names of the themes being uninstalled. | |
733 * | |
734 * @see \Drupal\Core\Extension\ThemeHandler::uninstall() | |
735 */ | |
736 function hook_themes_uninstalled(array $themes) { | |
737 // Remove some state entries depending on the theme. | |
738 foreach ($themes as $theme) { | |
739 \Drupal::state()->delete('example.' . $theme); | |
740 } | |
741 } | |
742 | |
743 /** | |
744 * Declare a template file extension to be used with a theme engine. | |
745 * | |
746 * This hook is used in a theme engine implementation in the format of | |
747 * ENGINE_extension(). | |
748 * | |
749 * @return string | |
750 * The file extension the theme engine will recognize. | |
751 */ | |
752 function hook_extension() { | |
753 // Extension for template base names in Twig. | |
754 return '.html.twig'; | |
755 } | |
756 | |
757 /** | |
758 * Render a template using the theme engine. | |
759 * | |
760 * @param string $template_file | |
761 * The path (relative to the Drupal root directory) to the template to be | |
762 * rendered including its extension in the format 'path/to/TEMPLATE_NAME.EXT'. | |
763 * @param array $variables | |
764 * A keyed array of variables that are available for composing the output. The | |
765 * theme engine is responsible for passing all the variables to the template. | |
766 * Depending on the code in the template, all or just a subset of the | |
767 * variables might be used in the template. | |
768 * | |
769 * @return string | |
770 * The output generated from the template. In most cases this will be a string | |
771 * containing HTML markup. | |
772 */ | |
773 function hook_render_template($template_file, $variables) { | |
774 $twig_service = \Drupal::service('twig'); | |
775 | |
776 return $twig_service->loadTemplate($template_file)->render($variables); | |
777 } | |
778 | |
779 /** | |
780 * Alter the element type information returned from modules. | |
781 * | |
782 * A module may implement this hook in order to alter the element type defaults | |
783 * defined by a module. | |
784 * | |
785 * @param array $info | |
786 * An associative array with structure identical to that of the return value | |
787 * of \Drupal\Core\Render\ElementInfoManagerInterface::getInfo(). | |
788 * | |
789 * @see \Drupal\Core\Render\ElementInfoManager | |
790 * @see \Drupal\Core\Render\Element\ElementInterface | |
791 */ | |
792 function hook_element_info_alter(array &$info) { | |
793 // Decrease the default size of textfields. | |
794 if (isset($info['textfield']['#size'])) { | |
795 $info['textfield']['#size'] = 40; | |
796 } | |
797 } | |
798 | |
799 /** | |
800 * Perform necessary alterations to the JavaScript before it is presented on | |
801 * the page. | |
802 * | |
803 * @param $javascript | |
804 * An array of all JavaScript being presented on the page. | |
805 * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets | |
806 * The assets attached to the current response. | |
807 * | |
808 * @see drupal_js_defaults() | |
809 * @see \Drupal\Core\Asset\AssetResolver | |
810 */ | |
811 function hook_js_alter(&$javascript, \Drupal\Core\Asset\AttachedAssetsInterface $assets) { | |
812 // Swap out jQuery to use an updated version of the library. | |
813 $javascript['core/assets/vendor/jquery/jquery.min.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js'; | |
814 } | |
815 | |
816 /** | |
817 * Add dynamic library definitions. | |
818 * | |
819 * Modules may implement this hook to add dynamic library definitions. Static | |
820 * libraries, which do not depend on any runtime information, should be declared | |
821 * in a modulename.libraries.yml file instead. | |
822 * | |
823 * @return array[] | |
824 * An array of library definitions to register, keyed by library ID. The | |
825 * library ID will be prefixed with the module name automatically. | |
826 * | |
827 * @see core.libraries.yml | |
828 * @see hook_library_info_alter() | |
829 */ | |
830 function hook_library_info_build() { | |
831 $libraries = []; | |
832 // Add a library whose information changes depending on certain conditions. | |
833 $libraries['mymodule.zombie'] = [ | |
834 'dependencies' => [ | |
835 'core/backbone', | |
836 ], | |
837 ]; | |
838 if (Drupal::moduleHandler()->moduleExists('minifyzombies')) { | |
839 $libraries['mymodule.zombie'] += [ | |
840 'js' => [ | |
841 'mymodule.zombie.min.js' => [], | |
842 ], | |
843 'css' => [ | |
844 'base' => [ | |
845 'mymodule.zombie.min.css' => [], | |
846 ], | |
847 ], | |
848 ]; | |
849 } | |
850 else { | |
851 $libraries['mymodule.zombie'] += [ | |
852 'js' => [ | |
853 'mymodule.zombie.js' => [], | |
854 ], | |
855 'css' => [ | |
856 'base' => [ | |
857 'mymodule.zombie.css' => [], | |
858 ], | |
859 ], | |
860 ]; | |
861 } | |
862 | |
863 // Add a library only if a certain condition is met. If code wants to | |
864 // integrate with this library it is safe to (try to) load it unconditionally | |
865 // without reproducing this check. If the library definition does not exist | |
866 // the library (of course) not be loaded but no notices or errors will be | |
867 // triggered. | |
868 if (Drupal::moduleHandler()->moduleExists('vampirize')) { | |
869 $libraries['mymodule.vampire'] = [ | |
870 'js' => [ | |
871 'js/vampire.js' => [], | |
872 ], | |
873 'css' => [ | |
874 'base' => [ | |
875 'css/vampire.css', | |
876 ], | |
877 ], | |
878 'dependencies' => [ | |
879 'core/jquery', | |
880 ], | |
881 ]; | |
882 } | |
883 return $libraries; | |
884 } | |
885 | |
886 /** | |
887 * Modify the JavaScript settings (drupalSettings). | |
888 * | |
889 * @param array &$settings | |
890 * An array of all JavaScript settings (drupalSettings) being presented on the | |
891 * page. | |
892 * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets | |
893 * The assets attached to the current response. | |
894 * | |
895 * @see \Drupal\Core\Asset\AssetResolver | |
896 * | |
897 * The results of this hook are cached, however modules may use | |
898 * hook_js_settings_alter() to dynamically alter settings. | |
899 */ | |
900 function hook_js_settings_build(array &$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets) { | |
901 // Manipulate settings. | |
902 if (isset($settings['dialog'])) { | |
903 $settings['dialog']['autoResize'] = FALSE; | |
904 } | |
905 } | |
906 | |
907 /** | |
908 * Perform necessary alterations to the JavaScript settings (drupalSettings). | |
909 * | |
910 * @param array &$settings | |
911 * An array of all JavaScript settings (drupalSettings) being presented on the | |
912 * page. | |
913 * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets | |
914 * The assets attached to the current response. | |
915 * | |
916 * @see \Drupal\Core\Asset\AssetResolver | |
917 */ | |
918 function hook_js_settings_alter(array &$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets) { | |
919 // Add settings. | |
920 $settings['user']['uid'] = \Drupal::currentUser(); | |
921 | |
922 // Manipulate settings. | |
923 if (isset($settings['dialog'])) { | |
924 $settings['dialog']['autoResize'] = FALSE; | |
925 } | |
926 } | |
927 | |
928 /** | |
929 * Alter libraries provided by an extension. | |
930 * | |
931 * Allows modules and themes to change libraries' definitions; mostly used to | |
932 * update a library to a newer version, while ensuring backward compatibility. | |
933 * In general, such manipulations should only be done to extend the library's | |
934 * functionality in a backward-compatible way, to avoid breaking other modules | |
935 * and themes that may be using the library. | |
936 * | |
937 * @param array $libraries | |
938 * An associative array of libraries registered by $extension. Keyed by | |
939 * internal library name and passed by reference. | |
940 * @param string $extension | |
941 * Can either be 'core' or the machine name of the extension that registered | |
942 * the libraries. | |
943 * | |
944 * @see \Drupal\Core\Asset\LibraryDiscoveryParser::parseLibraryInfo() | |
945 */ | |
946 function hook_library_info_alter(&$libraries, $extension) { | |
947 // Update Farbtastic to version 2.0. | |
948 if ($extension == 'core' && isset($libraries['jquery.farbtastic'])) { | |
949 // Verify existing version is older than the one we are updating to. | |
950 if (version_compare($libraries['jquery.farbtastic']['version'], '2.0', '<')) { | |
951 // Update the existing Farbtastic to version 2.0. | |
952 $libraries['jquery.farbtastic']['version'] = '2.0'; | |
953 // To accurately replace library files, the order of files and the options | |
954 // of each file have to be retained; e.g., like this: | |
955 $old_path = 'assets/vendor/farbtastic'; | |
956 // Since the replaced library files are no longer located in a directory | |
957 // relative to the original extension, specify an absolute path (relative | |
958 // to DRUPAL_ROOT / base_path()) to the new location. | |
959 $new_path = '/' . drupal_get_path('module', 'farbtastic_update') . '/js'; | |
960 $new_js = []; | |
961 $replacements = [ | |
962 $old_path . '/farbtastic.js' => $new_path . '/farbtastic-2.0.js', | |
963 ]; | |
964 foreach ($libraries['jquery.farbtastic']['js'] as $source => $options) { | |
965 if (isset($replacements[$source])) { | |
966 $new_js[$replacements[$source]] = $options; | |
967 } | |
968 else { | |
969 $new_js[$source] = $options; | |
970 } | |
971 } | |
972 $libraries['jquery.farbtastic']['js'] = $new_js; | |
973 } | |
974 } | |
975 } | |
976 | |
977 /** | |
978 * Alter CSS files before they are output on the page. | |
979 * | |
980 * @param $css | |
981 * An array of all CSS items (files and inline CSS) being requested on the page. | |
982 * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets | |
983 * The assets attached to the current response. | |
984 * | |
985 * @see Drupal\Core\Asset\LibraryResolverInterface::getCssAssets() | |
986 */ | |
987 function hook_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) { | |
988 // Remove defaults.css file. | |
989 unset($css[drupal_get_path('module', 'system') . '/defaults.css']); | |
990 } | |
991 | |
992 /** | |
993 * Add attachments (typically assets) to a page before it is rendered. | |
994 * | |
995 * Use this hook when you want to conditionally add attachments to a page. | |
996 * | |
997 * If you want to alter the attachments added by other modules or if your module | |
998 * depends on the elements of other modules, use hook_page_attachments_alter() | |
999 * instead, which runs after this hook. | |
1000 * | |
1001 * If you try to add anything but #attached and #cache to the array, an | |
1002 * exception is thrown. | |
1003 * | |
1004 * @param array &$attachments | |
1005 * An array that you can add attachments to. | |
1006 * | |
1007 * @see hook_page_attachments_alter() | |
1008 */ | |
1009 function hook_page_attachments(array &$attachments) { | |
1010 // Unconditionally attach an asset to the page. | |
1011 $attachments['#attached']['library'][] = 'core/domready'; | |
1012 | |
1013 // Conditionally attach an asset to the page. | |
1014 if (!\Drupal::currentUser()->hasPermission('may pet kittens')) { | |
1015 $attachments['#attached']['library'][] = 'core/jquery'; | |
1016 } | |
1017 } | |
1018 | |
1019 /** | |
1020 * Alter attachments (typically assets) to a page before it is rendered. | |
1021 * | |
1022 * Use this hook when you want to remove or alter attachments on the page, or | |
1023 * add attachments to the page that depend on another module's attachments (this | |
1024 * hook runs after hook_page_attachments(). | |
1025 * | |
1026 * If you try to add anything but #attached and #cache to the array, an | |
1027 * exception is thrown. | |
1028 * | |
1029 * @param array &$attachments | |
1030 * Array of all attachments provided by hook_page_attachments() implementations. | |
1031 * | |
1032 * @see hook_page_attachments() | |
1033 */ | |
1034 function hook_page_attachments_alter(array &$attachments) { | |
1035 // Conditionally remove an asset. | |
1036 if (in_array('core/jquery', $attachments['#attached']['library'])) { | |
1037 $index = array_search('core/jquery', $attachments['#attached']['library']); | |
1038 unset($attachments['#attached']['library'][$index]); | |
1039 } | |
1040 } | |
1041 | |
1042 /** | |
1043 * Add a renderable array to the top of the page. | |
1044 * | |
1045 * @param array $page_top | |
1046 * A renderable array representing the top of the page. | |
1047 */ | |
1048 function hook_page_top(array &$page_top) { | |
1049 $page_top['mymodule'] = ['#markup' => 'This is the top.']; | |
1050 } | |
1051 | |
1052 /** | |
1053 * Add a renderable array to the bottom of the page. | |
1054 * | |
1055 * @param array $page_bottom | |
1056 * A renderable array representing the bottom of the page. | |
1057 */ | |
1058 function hook_page_bottom(array &$page_bottom) { | |
1059 $page_bottom['mymodule'] = ['#markup' => 'This is the bottom.']; | |
1060 } | |
1061 | |
1062 /** | |
1063 * Register a module or theme's theme implementations. | |
1064 * | |
1065 * The implementations declared by this hook specify how a particular render | |
1066 * array is to be rendered as HTML. | |
1067 * | |
1068 * @param array $existing | |
1069 * An array of existing implementations that may be used for override | |
1070 * purposes. This is primarily useful for themes that may wish to examine | |
1071 * existing implementations to extract data (such as arguments) so that | |
1072 * it may properly register its own, higher priority implementations. | |
1073 * @param $type | |
1074 * Whether a theme, module, etc. is being processed. This is primarily useful | |
1075 * so that themes tell if they are the actual theme being called or a parent | |
1076 * theme. May be one of: | |
1077 * - 'module': A module is being checked for theme implementations. | |
1078 * - 'base_theme_engine': A theme engine is being checked for a theme that is | |
1079 * a parent of the actual theme being used. | |
1080 * - 'theme_engine': A theme engine is being checked for the actual theme | |
1081 * being used. | |
1082 * - 'base_theme': A base theme is being checked for theme implementations. | |
1083 * - 'theme': The actual theme in use is being checked. | |
1084 * @param $theme | |
1085 * The actual name of theme, module, etc. that is being being processed. | |
1086 * @param $path | |
1087 * The directory path of the theme or module, so that it doesn't need to be | |
1088 * looked up. | |
1089 * | |
1090 * @return array | |
1091 * An associative array of information about theme implementations. The keys | |
1092 * on the outer array are known as "theme hooks". For theme suggestions, | |
1093 * instead of the array key being the base theme hook, the key is a theme | |
1094 * suggestion name with the format 'base_hook_name__sub_hook_name'. | |
1095 * For render elements, the key is the machine name of the render element. | |
1096 * The array values are themselves arrays containing information about the | |
1097 * theme hook and its implementation. Each information array must contain | |
1098 * either a 'variables' element (for using a #theme element) or a | |
1099 * 'render element' element (for render elements), but not both. | |
1100 * The following elements may be part of each information array: | |
1101 * - variables: Only used for #theme in render array: an array of variables, | |
1102 * where the array keys are the names of the variables, and the array | |
1103 * values are the default values if they are not given in the render array. | |
1104 * Template implementations receive each array key as a variable in the | |
1105 * template file (so they must be legal PHP/Twig variable names). Function | |
1106 * implementations are passed the variables in a single $variables function | |
1107 * argument. If you are using these variables in a render array, prefix the | |
1108 * variable names defined here with a #. | |
1109 * - render element: Used for render element items only: the name of the | |
1110 * renderable element or element tree to pass to the theme function. This | |
1111 * name is used as the name of the variable that holds the renderable | |
1112 * element or tree in preprocess and process functions. | |
1113 * - file: The file the implementation resides in. This file will be included | |
1114 * prior to the theme being rendered, to make sure that the function or | |
1115 * preprocess function (as needed) is actually loaded. | |
1116 * - path: Override the path of the file to be used. Ordinarily the module or | |
1117 * theme path will be used, but if the file will not be in the default | |
1118 * path, include it here. This path should be relative to the Drupal root | |
1119 * directory. | |
1120 * - template: If specified, the theme implementation is a template file, and | |
1121 * this is the template name. Do not add 'html.twig' on the end of the | |
1122 * template name. The extension will be added automatically by the default | |
1123 * rendering engine (which is Twig.) If 'path' is specified, 'template' | |
1124 * should also be specified. If neither 'template' nor 'function' are | |
1125 * specified, a default template name will be assumed. For example, if a | |
1126 * module registers the 'search_result' theme hook, 'search-result' will be | |
1127 * assigned as its template name. | |
1128 * - function: (deprecated in Drupal 8.0.x, will be removed in Drupal 9.0.x) | |
1129 * If specified, this will be the function name to invoke for this | |
1130 * implementation. If neither 'template' nor 'function' are specified, a | |
1131 * default template name will be assumed. See above for more details. | |
1132 * - base hook: Used for theme suggestions only: the base theme hook name. | |
1133 * Instead of this suggestion's implementation being used directly, the base | |
1134 * hook will be invoked with this implementation as its first suggestion. | |
1135 * The base hook's files will be included and the base hook's preprocess | |
1136 * functions will be called in addition to any suggestion's preprocess | |
1137 * functions. If an implementation of hook_theme_suggestions_HOOK() (where | |
1138 * HOOK is the base hook) changes the suggestion order, a different | |
1139 * suggestion may be used in place of this suggestion. If after | |
1140 * hook_theme_suggestions_HOOK() this suggestion remains the first | |
1141 * suggestion, then this suggestion's function or template will be used to | |
1142 * generate the rendered output. | |
1143 * - pattern: A regular expression pattern to be used to allow this theme | |
1144 * implementation to have a dynamic name. The convention is to use __ to | |
1145 * differentiate the dynamic portion of the theme. For example, to allow | |
1146 * forums to be themed individually, the pattern might be: 'forum__'. Then, | |
1147 * when the forum is rendered, following render array can be used: | |
1148 * @code | |
1149 * $render_array = array( | |
1150 * '#theme' => array('forum__' . $tid, 'forum'), | |
1151 * '#forum' => $forum, | |
1152 * ); | |
1153 * @endcode | |
1154 * - preprocess functions: A list of functions used to preprocess this data. | |
1155 * Ordinarily this won't be used; it's automatically filled in. By default, | |
1156 * for a module this will be filled in as template_preprocess_HOOK. For | |
1157 * a theme this will be filled in as twig_preprocess and | |
1158 * twig_preprocess_HOOK as well as themename_preprocess and | |
1159 * themename_preprocess_HOOK. | |
1160 * - override preprocess functions: Set to TRUE when a theme does NOT want | |
1161 * the standard preprocess functions to run. This can be used to give a | |
1162 * theme FULL control over how variables are set. For example, if a theme | |
1163 * wants total control over how certain variables in the page.html.twig are | |
1164 * set, this can be set to true. Please keep in mind that when this is used | |
1165 * by a theme, that theme becomes responsible for making sure necessary | |
1166 * variables are set. | |
1167 * - type: (automatically derived) Where the theme hook is defined: | |
1168 * 'module', 'theme_engine', or 'theme'. | |
1169 * - theme path: (automatically derived) The directory path of the theme or | |
1170 * module, so that it doesn't need to be looked up. | |
1171 * | |
1172 * @see themeable | |
1173 * @see hook_theme_registry_alter() | |
1174 */ | |
1175 function hook_theme($existing, $type, $theme, $path) { | |
1176 return [ | |
1177 'forum_display' => [ | |
1178 'variables' => ['forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL], | |
1179 ], | |
1180 'forum_list' => [ | |
1181 'variables' => ['forums' => NULL, 'parents' => NULL, 'tid' => NULL], | |
1182 ], | |
1183 'forum_icon' => [ | |
1184 'variables' => ['new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0], | |
1185 ], | |
1186 'status_report' => [ | |
1187 'render element' => 'requirements', | |
1188 'file' => 'system.admin.inc', | |
1189 ], | |
1190 ]; | |
1191 } | |
1192 | |
1193 /** | |
1194 * Alter the theme registry information returned from hook_theme(). | |
1195 * | |
1196 * The theme registry stores information about all available theme hooks, | |
1197 * including which callback functions those hooks will call when triggered, | |
1198 * what template files are exposed by these hooks, and so on. | |
1199 * | |
1200 * Note that this hook is only executed as the theme cache is re-built. | |
1201 * Changes here will not be visible until the next cache clear. | |
1202 * | |
1203 * The $theme_registry array is keyed by theme hook name, and contains the | |
1204 * information returned from hook_theme(), as well as additional properties | |
1205 * added by \Drupal\Core\Theme\Registry::processExtension(). | |
1206 * | |
1207 * For example: | |
1208 * @code | |
1209 * $theme_registry['block_content_add_list'] = array ( | |
1210 * 'template' => 'block-content-add-list', | |
1211 * 'path' => 'core/themes/seven/templates', | |
1212 * 'type' => 'theme_engine', | |
1213 * 'theme path' => 'core/themes/seven', | |
1214 * 'includes' => array ( | |
1215 * 0 => 'core/modules/block_content/block_content.pages.inc', | |
1216 * ), | |
1217 * 'variables' => array ( | |
1218 * 'content' => NULL, | |
1219 * ), | |
1220 * 'preprocess functions' => array ( | |
1221 * 0 => 'template_preprocess', | |
1222 * 1 => 'template_preprocess_block_content_add_list', | |
1223 * 2 => 'contextual_preprocess', | |
1224 * 3 => 'seven_preprocess_block_content_add_list', | |
1225 * ), | |
1226 * ); | |
1227 * @endcode | |
1228 * | |
1229 * @param $theme_registry | |
1230 * The entire cache of theme registry information, post-processing. | |
1231 * | |
1232 * @see hook_theme() | |
1233 * @see \Drupal\Core\Theme\Registry::processExtension() | |
1234 */ | |
1235 function hook_theme_registry_alter(&$theme_registry) { | |
1236 // Kill the next/previous forum topic navigation links. | |
1237 foreach ($theme_registry['forum_topic_navigation']['preprocess functions'] as $key => $value) { | |
1238 if ($value == 'template_preprocess_forum_topic_navigation') { | |
1239 unset($theme_registry['forum_topic_navigation']['preprocess functions'][$key]); | |
1240 } | |
1241 } | |
1242 } | |
1243 | |
1244 /** | |
1245 * Alter the default, hook-independent variables for all templates. | |
1246 * | |
1247 * Allows modules to provide additional default template variables or manipulate | |
1248 * existing. This hook is invoked from template_preprocess() after basic default | |
1249 * template variables have been set up and before the next template preprocess | |
1250 * function is invoked. | |
1251 * | |
1252 * Note that the default template variables are statically cached within a | |
1253 * request. When adding a template variable that depends on other context, it is | |
1254 * your responsibility to appropriately reset the static cache in | |
1255 * template_preprocess() when needed: | |
1256 * @code | |
1257 * drupal_static_reset('template_preprocess'); | |
1258 * @endcode | |
1259 * | |
1260 * See user_template_preprocess_default_variables_alter() for an example. | |
1261 * | |
1262 * @param array $variables | |
1263 * An associative array of default template variables, as set up by | |
1264 * _template_preprocess_default_variables(). Passed by reference. | |
1265 * | |
1266 * @see template_preprocess() | |
1267 * @see _template_preprocess_default_variables() | |
1268 */ | |
1269 function hook_template_preprocess_default_variables_alter(&$variables) { | |
1270 $variables['is_admin'] = \Drupal::currentUser()->hasPermission('access administration pages'); | |
1271 } | |
1272 | |
1273 /** | |
1274 * @} End of "addtogroup hooks". | |
1275 */ |