754641cce75607868bbd686ed6bf1da7ec7d41f5
[yaffs-website] / web / core / lib / Drupal / Core / Render / theme.api.php
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  * Implementations of this hook must be placed in *.module or *.theme files, or
617  * must otherwise make sure that the hook implementation is available at
618  * any given time.
619  *
620  * @todo Add @code sample.
621  *
622  * @param array $variables
623  *   An array of variables passed to the theme hook. Note that this hook is
624  *   invoked before any preprocessing.
625  *
626  * @return array
627  *   An array of theme suggestions.
628  *
629  * @see hook_theme_suggestions_HOOK_alter()
630  */
631 function hook_theme_suggestions_HOOK(array $variables) {
632   $suggestions = [];
633
634   $suggestions[] = 'hookname__' . $variables['elements']['#langcode'];
635
636   return $suggestions;
637 }
638
639 /**
640  * Alters named suggestions for all theme hooks.
641  *
642  * This hook is invoked for all theme hooks, if you are targeting a specific
643  * theme hook it's best to use hook_theme_suggestions_HOOK_alter().
644  *
645  * The call order is as follows: all existing suggestion alter functions are
646  * called for module A, then all for module B, etc., followed by all for any
647  * base theme(s), and finally for the active theme. The order is
648  * determined by system weight, then by extension (module or theme) name.
649  *
650  * Within each module or theme, suggestion alter hooks are called in the
651  * following order: first, hook_theme_suggestions_alter(); second,
652  * hook_theme_suggestions_HOOK_alter(). So, for each module or theme, the more
653  * general hooks are called first followed by the more specific.
654  *
655  * In the following example, we provide an alternative template suggestion to
656  * node and taxonomy term templates based on the user being logged in.
657  * @code
658  * function MYMODULE_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
659  *   if (\Drupal::currentUser()->isAuthenticated() && in_array($hook, array('node', 'taxonomy_term'))) {
660  *     $suggestions[] = $hook . '__' . 'logged_in';
661  *   }
662  * }
663  *
664  * @endcode
665  *
666  * @param array $suggestions
667  *   An array of alternate, more specific names for template files or theme
668  *   functions.
669  * @param array $variables
670  *   An array of variables passed to the theme hook. Note that this hook is
671  *   invoked before any variable preprocessing.
672  * @param string $hook
673  *   The base hook name. For example, if '#theme' => 'node__article' is called,
674  *   then $hook will be 'node', not 'node__article'. The specific hook called
675  *   (in this case 'node__article') is available in
676  *   $variables['theme_hook_original'].
677  *
678  * @return array
679  *   An array of theme suggestions.
680  *
681  * @see hook_theme_suggestions_HOOK_alter()
682  */
683 function hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
684   // Add an interface-language specific suggestion to all theme hooks.
685   $suggestions[] = $hook . '__' . \Drupal::languageManager()->getCurrentLanguage()->getId();
686 }
687
688 /**
689  * Alters named suggestions for a specific theme hook.
690  *
691  * This hook allows any module or theme to provide alternative theme function or
692  * template name suggestions and reorder or remove suggestions provided by
693  * hook_theme_suggestions_HOOK() or by earlier invocations of this hook.
694  *
695  * HOOK is the least-specific version of the hook being called. For example, if
696  * '#theme' => 'node__article' is called, then node_theme_suggestions_node()
697  * will be invoked, not node_theme_suggestions_node__article(). The specific
698  * hook called (in this case 'node__article') is available in
699  * $variables['theme_hook_original'].
700  *
701  * Implementations of this hook must be placed in *.module or *.theme files, or
702  * must otherwise make sure that the hook implementation is available at
703  * any given time.
704  *
705  * @todo Add @code sample.
706  *
707  * @param array $suggestions
708  *   An array of theme suggestions.
709  * @param array $variables
710  *   An array of variables passed to the theme hook. Note that this hook is
711  *   invoked before any preprocessing.
712  *
713  * @see hook_theme_suggestions_alter()
714  * @see hook_theme_suggestions_HOOK()
715  */
716 function hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) {
717   if (empty($variables['header'])) {
718     $suggestions[] = 'hookname__' . 'no_header';
719   }
720 }
721
722 /**
723  * Respond to themes being installed.
724  *
725  * @param array $theme_list
726  *   Array containing the names of the themes being installed.
727  *
728  * @see \Drupal\Core\Extension\ThemeHandler::install()
729  */
730 function hook_themes_installed($theme_list) {
731   foreach ($theme_list as $theme) {
732     block_theme_initialize($theme);
733   }
734 }
735
736 /**
737  * Respond to themes being uninstalled.
738  *
739  * @param array $themes
740  *   Array containing the names of the themes being uninstalled.
741  *
742  * @see \Drupal\Core\Extension\ThemeHandler::uninstall()
743  */
744 function hook_themes_uninstalled(array $themes) {
745   // Remove some state entries depending on the theme.
746   foreach ($themes as $theme) {
747     \Drupal::state()->delete('example.' . $theme);
748   }
749 }
750
751 /**
752  * Declare a template file extension to be used with a theme engine.
753  *
754  * This hook is used in a theme engine implementation in the format of
755  * ENGINE_extension().
756  *
757  * @return string
758  *   The file extension the theme engine will recognize.
759  */
760 function hook_extension() {
761   // Extension for template base names in Twig.
762   return '.html.twig';
763 }
764
765 /**
766  * Render a template using the theme engine.
767  *
768  * @param string $template_file
769  *   The path (relative to the Drupal root directory) to the template to be
770  *   rendered including its extension in the format 'path/to/TEMPLATE_NAME.EXT'.
771  * @param array $variables
772  *   A keyed array of variables that are available for composing the output. The
773  *   theme engine is responsible for passing all the variables to the template.
774  *   Depending on the code in the template, all or just a subset of the
775  *   variables might be used in the template.
776  *
777  * @return string
778  *   The output generated from the template. In most cases this will be a string
779  *   containing HTML markup.
780  */
781 function hook_render_template($template_file, $variables) {
782   $twig_service = \Drupal::service('twig');
783
784   return $twig_service->loadTemplate($template_file)->render($variables);
785 }
786
787 /**
788  * Alter the element type information returned from modules.
789  *
790  * A module may implement this hook in order to alter the element type defaults
791  * defined by a module.
792  *
793  * @param array $info
794  *   An associative array with structure identical to that of the return value
795  *   of \Drupal\Core\Render\ElementInfoManagerInterface::getInfo().
796  *
797  * @see \Drupal\Core\Render\ElementInfoManager
798  * @see \Drupal\Core\Render\Element\ElementInterface
799  */
800 function hook_element_info_alter(array &$info) {
801   // Decrease the default size of textfields.
802   if (isset($info['textfield']['#size'])) {
803     $info['textfield']['#size'] = 40;
804   }
805 }
806
807 /**
808  * Perform necessary alterations to the JavaScript before it is presented on
809  * the page.
810  *
811  * @param $javascript
812  *   An array of all JavaScript being presented on the page.
813  * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
814  *   The assets attached to the current response.
815  *
816  * @see drupal_js_defaults()
817  * @see \Drupal\Core\Asset\AssetResolver
818  */
819 function hook_js_alter(&$javascript, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
820   // Swap out jQuery to use an updated version of the library.
821   $javascript['core/assets/vendor/jquery/jquery.min.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js';
822 }
823
824 /**
825  * Add dynamic library definitions.
826  *
827  * Modules may implement this hook to add dynamic library definitions. Static
828  * libraries, which do not depend on any runtime information, should be declared
829  * in a modulename.libraries.yml file instead.
830  *
831  * @return array[]
832  *   An array of library definitions to register, keyed by library ID. The
833  *   library ID will be prefixed with the module name automatically.
834  *
835  * @see core.libraries.yml
836  * @see hook_library_info_alter()
837  */
838 function hook_library_info_build() {
839   $libraries = [];
840   // Add a library whose information changes depending on certain conditions.
841   $libraries['mymodule.zombie'] = [
842     'dependencies' => [
843       'core/backbone',
844     ],
845   ];
846   if (Drupal::moduleHandler()->moduleExists('minifyzombies')) {
847     $libraries['mymodule.zombie'] += [
848       'js' => [
849         'mymodule.zombie.min.js' => [],
850       ],
851       'css' => [
852         'base' => [
853           'mymodule.zombie.min.css' => [],
854         ],
855       ],
856     ];
857   }
858   else {
859     $libraries['mymodule.zombie'] += [
860       'js' => [
861         'mymodule.zombie.js' => [],
862       ],
863       'css' => [
864         'base' => [
865           'mymodule.zombie.css' => [],
866         ],
867       ],
868     ];
869   }
870
871   // Add a library only if a certain condition is met. If code wants to
872   // integrate with this library it is safe to (try to) load it unconditionally
873   // without reproducing this check. If the library definition does not exist
874   // the library (of course) not be loaded but no notices or errors will be
875   // triggered.
876   if (Drupal::moduleHandler()->moduleExists('vampirize')) {
877     $libraries['mymodule.vampire'] = [
878       'js' => [
879         'js/vampire.js' => [],
880       ],
881       'css' => [
882         'base' => [
883           'css/vampire.css',
884         ],
885       ],
886       'dependencies' => [
887         'core/jquery',
888       ],
889     ];
890   }
891   return $libraries;
892 }
893
894 /**
895  * Modify the JavaScript settings (drupalSettings).
896  *
897  * @param array &$settings
898  *   An array of all JavaScript settings (drupalSettings) being presented on the
899  *   page.
900  * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
901  *   The assets attached to the current response.
902  *
903  * @see \Drupal\Core\Asset\AssetResolver
904  *
905  * The results of this hook are cached, however modules may use
906  * hook_js_settings_alter() to dynamically alter settings.
907  */
908 function hook_js_settings_build(array &$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
909   // Manipulate settings.
910   if (isset($settings['dialog'])) {
911     $settings['dialog']['autoResize'] = FALSE;
912   }
913 }
914
915 /**
916  * Perform necessary alterations to the JavaScript settings (drupalSettings).
917  *
918  * @param array &$settings
919  *   An array of all JavaScript settings (drupalSettings) being presented on the
920  *   page.
921  * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
922  *   The assets attached to the current response.
923  *
924  * @see \Drupal\Core\Asset\AssetResolver
925  */
926 function hook_js_settings_alter(array &$settings, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
927   // Add settings.
928   $settings['user']['uid'] = \Drupal::currentUser();
929
930   // Manipulate settings.
931   if (isset($settings['dialog'])) {
932     $settings['dialog']['autoResize'] = FALSE;
933   }
934 }
935
936 /**
937  * Alter libraries provided by an extension.
938  *
939  * Allows modules and themes to change libraries' definitions; mostly used to
940  * update a library to a newer version, while ensuring backward compatibility.
941  * In general, such manipulations should only be done to extend the library's
942  * functionality in a backward-compatible way, to avoid breaking other modules
943  * and themes that may be using the library.
944  *
945  * @param array $libraries
946  *   An associative array of libraries registered by $extension. Keyed by
947  *   internal library name and passed by reference.
948  * @param string $extension
949  *   Can either be 'core' or the machine name of the extension that registered
950  *   the libraries.
951  *
952  * @see \Drupal\Core\Asset\LibraryDiscoveryParser::parseLibraryInfo()
953  */
954 function hook_library_info_alter(&$libraries, $extension) {
955   // Update Farbtastic to version 2.0.
956   if ($extension == 'core' && isset($libraries['jquery.farbtastic'])) {
957     // Verify existing version is older than the one we are updating to.
958     if (version_compare($libraries['jquery.farbtastic']['version'], '2.0', '<')) {
959       // Update the existing Farbtastic to version 2.0.
960       $libraries['jquery.farbtastic']['version'] = '2.0';
961       // To accurately replace library files, the order of files and the options
962       // of each file have to be retained; e.g., like this:
963       $old_path = 'assets/vendor/farbtastic';
964       // Since the replaced library files are no longer located in a directory
965       // relative to the original extension, specify an absolute path (relative
966       // to DRUPAL_ROOT / base_path()) to the new location.
967       $new_path = '/' . drupal_get_path('module', 'farbtastic_update') . '/js';
968       $new_js = [];
969       $replacements = [
970         $old_path . '/farbtastic.js' => $new_path . '/farbtastic-2.0.js',
971       ];
972       foreach ($libraries['jquery.farbtastic']['js'] as $source => $options) {
973         if (isset($replacements[$source])) {
974           $new_js[$replacements[$source]] = $options;
975         }
976         else {
977           $new_js[$source] = $options;
978         }
979       }
980       $libraries['jquery.farbtastic']['js'] = $new_js;
981     }
982   }
983 }
984
985 /**
986  * Alter CSS files before they are output on the page.
987  *
988  * @param $css
989  *   An array of all CSS items (files and inline CSS) being requested on the page.
990  * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
991  *   The assets attached to the current response.
992  *
993  * @see Drupal\Core\Asset\LibraryResolverInterface::getCssAssets()
994  */
995 function hook_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {
996   // Remove defaults.css file.
997   unset($css[drupal_get_path('module', 'system') . '/defaults.css']);
998 }
999
1000 /**
1001  * Add attachments (typically assets) to a page before it is rendered.
1002  *
1003  * Use this hook when you want to conditionally add attachments to a page.
1004  *
1005  * If you want to alter the attachments added by other modules or if your module
1006  * depends on the elements of other modules, use hook_page_attachments_alter()
1007  * instead, which runs after this hook.
1008  *
1009  * If you try to add anything but #attached and #cache to the array, an
1010  * exception is thrown.
1011  *
1012  * @param array &$attachments
1013  *   An array that you can add attachments to.
1014  *
1015  * @see hook_page_attachments_alter()
1016  */
1017 function hook_page_attachments(array &$attachments) {
1018   // Unconditionally attach an asset to the page.
1019   $attachments['#attached']['library'][] = 'core/domready';
1020
1021   // Conditionally attach an asset to the page.
1022   if (!\Drupal::currentUser()->hasPermission('may pet kittens')) {
1023     $attachments['#attached']['library'][] = 'core/jquery';
1024   }
1025 }
1026
1027 /**
1028  * Alter attachments (typically assets) to a page before it is rendered.
1029  *
1030  * Use this hook when you want to remove or alter attachments on the page, or
1031  * add attachments to the page that depend on another module's attachments (this
1032  * hook runs after hook_page_attachments().
1033  *
1034  * If you try to add anything but #attached and #cache to the array, an
1035  * exception is thrown.
1036  *
1037  * @param array &$attachments
1038  *   Array of all attachments provided by hook_page_attachments() implementations.
1039  *
1040  * @see hook_page_attachments()
1041  */
1042 function hook_page_attachments_alter(array &$attachments) {
1043   // Conditionally remove an asset.
1044   if (in_array('core/jquery', $attachments['#attached']['library'])) {
1045     $index = array_search('core/jquery', $attachments['#attached']['library']);
1046     unset($attachments['#attached']['library'][$index]);
1047   }
1048 }
1049
1050 /**
1051  * Add a renderable array to the top of the page.
1052  *
1053  * @param array $page_top
1054  *   A renderable array representing the top of the page.
1055  */
1056 function hook_page_top(array &$page_top) {
1057   $page_top['mymodule'] = ['#markup' => 'This is the top.'];
1058 }
1059
1060 /**
1061  * Add a renderable array to the bottom of the page.
1062  *
1063  * @param array $page_bottom
1064  *   A renderable array representing the bottom of the page.
1065  */
1066 function hook_page_bottom(array &$page_bottom) {
1067   $page_bottom['mymodule'] = ['#markup' => 'This is the bottom.'];
1068 }
1069
1070 /**
1071  * Register a module or theme's theme implementations.
1072  *
1073  * The implementations declared by this hook specify how a particular render
1074  * array is to be rendered as HTML.
1075  *
1076  * @param array $existing
1077  *   An array of existing implementations that may be used for override
1078  *   purposes. This is primarily useful for themes that may wish to examine
1079  *   existing implementations to extract data (such as arguments) so that
1080  *   it may properly register its own, higher priority implementations.
1081  * @param $type
1082  *   Whether a theme, module, etc. is being processed. This is primarily useful
1083  *   so that themes tell if they are the actual theme being called or a parent
1084  *   theme. May be one of:
1085  *   - 'module': A module is being checked for theme implementations.
1086  *   - 'base_theme_engine': A theme engine is being checked for a theme that is
1087  *     a parent of the actual theme being used.
1088  *   - 'theme_engine': A theme engine is being checked for the actual theme
1089  *     being used.
1090  *   - 'base_theme': A base theme is being checked for theme implementations.
1091  *   - 'theme': The actual theme in use is being checked.
1092  * @param $theme
1093  *   The actual name of theme, module, etc. that is being being processed.
1094  * @param $path
1095  *   The directory path of the theme or module, so that it doesn't need to be
1096  *   looked up.
1097  *
1098  * @return array
1099  *   An associative array of information about theme implementations. The keys
1100  *   on the outer array are known as "theme hooks". For theme suggestions,
1101  *   instead of the array key being the base theme hook, the key is a theme
1102  *   suggestion name with the format 'base_hook_name__sub_hook_name'.
1103  *   For render elements, the key is the machine name of the render element.
1104  *   The array values are themselves arrays containing information about the
1105  *   theme hook and its implementation. Each information array must contain
1106  *   either a 'variables' element (for using a #theme element) or a
1107  *   'render element' element (for render elements), but not both.
1108  *   The following elements may be part of each information array:
1109  *   - variables: Only used for #theme in render array: an array of variables,
1110  *     where the array keys are the names of the variables, and the array
1111  *     values are the default values if they are not given in the render array.
1112  *     Template implementations receive each array key as a variable in the
1113  *     template file (so they must be legal PHP/Twig variable names). Function
1114  *     implementations are passed the variables in a single $variables function
1115  *     argument. If you are using these variables in a render array, prefix the
1116  *     variable names defined here with a #.
1117  *   - render element: Used for render element items only: the name of the
1118  *     renderable element or element tree to pass to the theme function. This
1119  *     name is used as the name of the variable that holds the renderable
1120  *     element or tree in preprocess and process functions.
1121  *   - file: The file the implementation resides in. This file will be included
1122  *     prior to the theme being rendered, to make sure that the function or
1123  *     preprocess function (as needed) is actually loaded.
1124  *   - path: Override the path of the file to be used. Ordinarily the module or
1125  *     theme path will be used, but if the file will not be in the default
1126  *     path, include it here. This path should be relative to the Drupal root
1127  *     directory.
1128  *   - template: If specified, the theme implementation is a template file, and
1129  *     this is the template name. Do not add 'html.twig' on the end of the
1130  *     template name. The extension will be added automatically by the default
1131  *     rendering engine (which is Twig.) If 'path' is specified, 'template'
1132  *     should also be specified. If neither 'template' nor 'function' are
1133  *     specified, a default template name will be assumed. For example, if a
1134  *     module registers the 'search_result' theme hook, 'search-result' will be
1135  *     assigned as its template name.
1136  *   - function: (deprecated in Drupal 8.0.x, will be removed in Drupal 9.0.x)
1137  *     If specified, this will be the function name to invoke for this
1138  *     implementation. If neither 'template' nor 'function' are specified, a
1139  *     default template name will be assumed. See above for more details.
1140  *   - base hook: Used for theme suggestions only: the base theme hook name.
1141  *     Instead of this suggestion's implementation being used directly, the base
1142  *     hook will be invoked with this implementation as its first suggestion.
1143  *     The base hook's files will be included and the base hook's preprocess
1144  *     functions will be called in addition to any suggestion's preprocess
1145  *     functions. If an implementation of hook_theme_suggestions_HOOK() (where
1146  *     HOOK is the base hook) changes the suggestion order, a different
1147  *     suggestion may be used in place of this suggestion. If after
1148  *     hook_theme_suggestions_HOOK() this suggestion remains the first
1149  *     suggestion, then this suggestion's function or template will be used to
1150  *     generate the rendered output.
1151  *   - pattern: A regular expression pattern to be used to allow this theme
1152  *     implementation to have a dynamic name. The convention is to use __ to
1153  *     differentiate the dynamic portion of the theme. For example, to allow
1154  *     forums to be themed individually, the pattern might be: 'forum__'. Then,
1155  *     when the forum is rendered, following render array can be used:
1156  *     @code
1157  *     $render_array = array(
1158  *       '#theme' => array('forum__' . $tid, 'forum'),
1159  *       '#forum' => $forum,
1160  *     );
1161  *     @endcode
1162  *   - preprocess functions: A list of functions used to preprocess this data.
1163  *     Ordinarily this won't be used; it's automatically filled in. By default,
1164  *     for a module this will be filled in as template_preprocess_HOOK. For
1165  *     a theme this will be filled in as twig_preprocess and
1166  *     twig_preprocess_HOOK as well as themename_preprocess and
1167  *     themename_preprocess_HOOK.
1168  *   - override preprocess functions: Set to TRUE when a theme does NOT want
1169  *     the standard preprocess functions to run. This can be used to give a
1170  *     theme FULL control over how variables are set. For example, if a theme
1171  *     wants total control over how certain variables in the page.html.twig are
1172  *     set, this can be set to true. Please keep in mind that when this is used
1173  *     by a theme, that theme becomes responsible for making sure necessary
1174  *     variables are set.
1175  *   - type: (automatically derived) Where the theme hook is defined:
1176  *     'module', 'theme_engine', or 'theme'.
1177  *   - theme path: (automatically derived) The directory path of the theme or
1178  *     module, so that it doesn't need to be looked up.
1179  *
1180  * @see themeable
1181  * @see hook_theme_registry_alter()
1182  */
1183 function hook_theme($existing, $type, $theme, $path) {
1184   return [
1185     'forum_display' => [
1186       'variables' => ['forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL],
1187     ],
1188     'forum_list' => [
1189       'variables' => ['forums' => NULL, 'parents' => NULL, 'tid' => NULL],
1190     ],
1191     'forum_icon' => [
1192       'variables' => ['new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0],
1193     ],
1194     'status_report' => [
1195       'render element' => 'requirements',
1196       'file' => 'system.admin.inc',
1197     ],
1198   ];
1199 }
1200
1201 /**
1202  * Alter the theme registry information returned from hook_theme().
1203  *
1204  * The theme registry stores information about all available theme hooks,
1205  * including which callback functions those hooks will call when triggered,
1206  * what template files are exposed by these hooks, and so on.
1207  *
1208  * Note that this hook is only executed as the theme cache is re-built.
1209  * Changes here will not be visible until the next cache clear.
1210  *
1211  * The $theme_registry array is keyed by theme hook name, and contains the
1212  * information returned from hook_theme(), as well as additional properties
1213  * added by \Drupal\Core\Theme\Registry::processExtension().
1214  *
1215  * For example:
1216  * @code
1217  * $theme_registry['block_content_add_list'] = array (
1218  *   'template' => 'block-content-add-list',
1219  *   'path' => 'core/themes/seven/templates',
1220  *   'type' => 'theme_engine',
1221  *   'theme path' => 'core/themes/seven',
1222  *   'includes' => array (
1223  *     0 => 'core/modules/block_content/block_content.pages.inc',
1224  *   ),
1225  *   'variables' => array (
1226  *     'content' => NULL,
1227  *   ),
1228  *   'preprocess functions' => array (
1229  *     0 => 'template_preprocess',
1230  *     1 => 'template_preprocess_block_content_add_list',
1231  *     2 => 'contextual_preprocess',
1232  *     3 => 'seven_preprocess_block_content_add_list',
1233  *   ),
1234  * );
1235  * @endcode
1236  *
1237  * @param $theme_registry
1238  *   The entire cache of theme registry information, post-processing.
1239  *
1240  * @see hook_theme()
1241  * @see \Drupal\Core\Theme\Registry::processExtension()
1242  */
1243 function hook_theme_registry_alter(&$theme_registry) {
1244   // Kill the next/previous forum topic navigation links.
1245   foreach ($theme_registry['forum_topic_navigation']['preprocess functions'] as $key => $value) {
1246     if ($value == 'template_preprocess_forum_topic_navigation') {
1247       unset($theme_registry['forum_topic_navigation']['preprocess functions'][$key]);
1248     }
1249   }
1250 }
1251
1252 /**
1253  * Alter the default, hook-independent variables for all templates.
1254  *
1255  * Allows modules to provide additional default template variables or manipulate
1256  * existing. This hook is invoked from template_preprocess() after basic default
1257  * template variables have been set up and before the next template preprocess
1258  * function is invoked.
1259  *
1260  * Note that the default template variables are statically cached within a
1261  * request. When adding a template variable that depends on other context, it is
1262  * your responsibility to appropriately reset the static cache in
1263  * template_preprocess() when needed:
1264  * @code
1265  * drupal_static_reset('template_preprocess');
1266  * @endcode
1267  *
1268  * See user_template_preprocess_default_variables_alter() for an example.
1269  *
1270  * @param array $variables
1271  *   An associative array of default template variables, as set up by
1272  *   _template_preprocess_default_variables(). Passed by reference.
1273  *
1274  * @see template_preprocess()
1275  * @see _template_preprocess_default_variables()
1276  */
1277 function hook_template_preprocess_default_variables_alter(&$variables) {
1278   $variables['is_admin'] = \Drupal::currentUser()->hasPermission('access administration pages');
1279 }
1280
1281 /**
1282  * @} End of "addtogroup hooks".
1283  */