This document discusses theming in Drupal 8. It covers:
- What theming is and how themes override default module outputs
- Creating a theme and selecting a base theme like classy or stable
- Using Twig templates to define HTML and preprocess functions
- Declaring libraries for asset loading and managing dependencies
- Defining breakpoints and configuration options for the admin interface
- Registering new theme hooks and template files
5. Theming?
• THEMING = HTML output
• Other outputs could be RSS feed, JSON…
• HEADfull Drupal 8 here!
6. Theming?
• Modules produce datas.
• Modules have default renderings.
• For example, Block module define
block.twig.html template to render each block.
• The theme OVERRIDES the default renderings.
12. Base theme
• Theme inherits from its parents.
• Drupal 8 provides 2 base themes:
• classy - for those who loves having
lots of html code.
• stable - for the others.
20. TWIG Debug
Activate Twig Debug in local environment by
editing the file /sites/default/services.yml.
21. • Not enough because Drupal caches entity rendering.
• Uncomment the loading of a local.settings.php file
in /sites/default/settings.php.
TWIG Debug
That file adds a cache backend render service which avoid
Drupal to cache.
22. Template suggestions
• Depending on context you can ask
Drupal to load a specific template.
• For example to display a node Drupal
uses node.html.twig. But it can use
node--article.html.twig (if it exists!)
for any node of type article.
23. • A module defines suggestions with
hook_theme_suggestions_HOOK().
• Other modules or themes can add
suggestions with
hook_theme_suggestions_alter() or
hook_theme_suggestions_HOOK_alter().
Template suggestions
30. Librairies
• No more manually asset loading.
• Bye bye drupal_add_css(),
drupal_add_js() and
drupal_add_library()!
• Must add any asset through a library.
• Drupal takes care of libraries loading
and dependencies.
46. Configuration system
• How to add settings in the backoffice.
• Each theme gets its own default form.
• Alter the form with
THEME_form_system_theme_settings_alter()
(using the Form API).
50. • External configuration files (e.g. image style)
can be integrated into a theme:
• Export files via Drupal UI.
• Clean UUID.
• Place files into /themes/ive/config/optional.
• Those files are only loaded during installation.
Configuration system
52. Theme hook declaration
• Any theme and module can declare its own
formatting.
• Each theme hook is associated with a
template file.
• Each theme hook got its own preprocess
function (template_preprocess_HOOK()) and
theme suggestions function
(MODULE_theme_suggestions_HOOK()).
57. • Everything is a block: breadcrumb, messages…
• Logo format is SVG.
• Utilities modules for themers:
• Libraries UI
• Breakpoints UI
• No more theme functions but templates
instead.
• No more theme() function but Render Arrays.
• No more process functions.
58. Conclusion
Not so many things
to learn compared to Drupal 7.
Better organization for assets
management.
Easier with Drupal 8!