+ All Categories
Home > Internet > Drupal 8 theming deep dive

Drupal 8 theming deep dive

Date post: 11-Feb-2017
Category:
Upload: romain-jarraud
View: 95 times
Download: 0 times
Share this document with a friend
60
Drupal 8 Theming Deep Dive Romain JARRAUD @romainjarraud
Transcript
Page 1: Drupal 8 theming deep dive

Drupal 8 Theming Deep Dive

Romain JARRAUD @romainjarraud

Page 2: Drupal 8 theming deep dive

Romain JARRAUD Trainer/consultant

Trained People romainj on d.o

Page 3: Drupal 8 theming deep dive

What is theming?

Page 4: Drupal 8 theming deep dive

Theming?

Functionnal Display

Drupal and modules Theme

Page 5: Drupal 8 theming deep dive

Theming?

• THEMING = HTML output

• Other outputs could be RSS feed, JSON…

• HEADfull Drupal 8 here!

Page 6: Drupal 8 theming deep dive

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.

Page 7: Drupal 8 theming deep dive

Theming?

Theme overrides defaults:

• html code for every element on the page.

• styles: font size, colors, images...

• effects using javascript.

Page 8: Drupal 8 theming deep dive

Theme

Browser CSS

TemplatesDrupal & modules Content

HTML

Web Page

Request

Page 9: Drupal 8 theming deep dive

The menu• Theme creation.

• Template.

• Asset management.

• Breakpoints.

• Configuration form.

• External configuration.

• Declaration of our own template file.

Page 10: Drupal 8 theming deep dive

Create a theme

Page 11: Drupal 8 theming deep dive

Create a theme/themes/ive/ive.info.yml

Page 12: Drupal 8 theming deep dive

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.

Page 13: Drupal 8 theming deep dive

Classy vs. Stable/core/themes/stable/templates/block/

block.html.twig/core/themes/classy/templates/block/

block.html.twig

Page 14: Drupal 8 theming deep dive

Theme files

Page 15: Drupal 8 theming deep dive

Templates

Page 16: Drupal 8 theming deep dive

TWIG

• Drupal 8 uses TWIG.

• TWIG created by Fabien Potencier.

• Templating system to generate HTML.

Page 17: Drupal 8 theming deep dive

• Template name .html.twig: example of page.html.twig for the page template.

• They hold the HTML tags along with TWIG code.

TWIG

Page 18: Drupal 8 theming deep dive

TWIG (quickly)• Display the content of var: {{ var }}.

• Display any kind of variable (string, array, object): {{ node.title }}.

• Function: {% if var %} {% endif %}.

• Comments: {# comment #}

• Translation:

• {% trans %} translatable {{ string }} {% endtrans %}.

• Ready to be translated through Drupal UI!

Page 19: Drupal 8 theming deep dive

TWIG (quickly)•Filters:

• {{ date|format_date(‘medium’) }}

•string:

•Escaping: {{ text }} (default).

•No escaping: {{ text|passthrough }} (be carreful!).

•Placeholder : {{ text|placeholder }}.

• {{ content|without(‘links’) }}.

• {{ string|lower }} (upper as well).

• {{ class_name|clean_class }}.

• {{ id_name|clean_id }}.

Page 20: Drupal 8 theming deep dive

TWIG Debug

Activate Twig Debug in local environment by editing the file /sites/default/services.yml.

Page 21: Drupal 8 theming deep dive

• 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.

Page 22: Drupal 8 theming deep dive

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.

Page 23: Drupal 8 theming deep dive

• 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

Page 24: Drupal 8 theming deep dive

Template suggestions

(Those comments are shown

thanks to TWIG Debug mode)

Page 25: Drupal 8 theming deep dive

Template override

• Copy original file into theme template folder.

• Rename it if necessary.

• Empty theme registry.

• Do what you want!

Page 26: Drupal 8 theming deep dive

Preprocess functions

Page 27: Drupal 8 theming deep dive

Preprocess functions

TemplateDefault

preprocess

$variables

Theme preprocess

$variables

Modules preprocess

$variables

Page 28: Drupal 8 theming deep dive

Called functions orderTheme suggestions

• MODULE_theme_suggestions_HOOK(array $variables)

• OTHERMODULE_theme_suggestions_alter(array &$suggestions, array $variables, $hook)

• OTHERMODULE_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)

• THEME_theme_suggestions_alter(array &$suggestions, array $variables, $hook)

• THEME_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)

Preprocess

• template_preprocess_HOOK(array &$variables)

• OTHERMODULE_preprocess(array &$variables, $hook)

• OTHERMODULE_preprocess_HOOK(array &$variables)

• THEME_preprocess(array &$variables, $hook)

• THEME_preprocess_HOOK(array &$variables)

Page 29: Drupal 8 theming deep dive

Librairies

Page 30: Drupal 8 theming deep dive

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.

Page 31: Drupal 8 theming deep dive

Librairies

Declaration Loading

Page 32: Drupal 8 theming deep dive

Libraries declaration/themes/ive/ive.libraries.yml

Page 33: Drupal 8 theming deep dive

Loading from /themes/ive/ive.info.yml

Libraries loading

Page 34: Drupal 8 theming deep dive

Libraries loading

Loading from a template file

/themes/ive/templates/node.html.twig

Page 35: Drupal 8 theming deep dive

Loading from /themes/ive/ive.theme using THEME_page_attachments_alter()

Libraries loading

Page 36: Drupal 8 theming deep dive

Loading from /themes/ive/ive.theme using a preprocess fonction

Libraries loading

Page 37: Drupal 8 theming deep dive

Library dependency• No javascript loaded for anonymous users.

• Dependencies should be explicit.

/themes/ive/ive.libraries.yml

Page 38: Drupal 8 theming deep dive

Libraries override/themes/ive/ive.info.yml

Page 39: Drupal 8 theming deep dive

Libraries extension/themes/ive/ive.info.yml

Page 40: Drupal 8 theming deep dive

From PHP to JS/themes/ive/ive.theme

/themes/ive/js/ive.js

Page 41: Drupal 8 theming deep dive

Breakpoints

Page 42: Drupal 8 theming deep dive

Breakpoints

Breakpoints are exposed on the server side.

For example, images can be loaded depending on those breakpoints.

Page 43: Drupal 8 theming deep dive

/themes/ive/ive.breakpoints.yml

Breakpoints

Page 44: Drupal 8 theming deep dive

Breakpoints

Page 45: Drupal 8 theming deep dive

Configuration system

Page 46: Drupal 8 theming deep dive

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).

Page 47: Drupal 8 theming deep dive

/themes/ive/config/install/ive.settings.yml

Defines:

• ive.settings configuration.

• « ive » with its default value « one ».

This configuration is loaded during installation.

Configuration system

Page 48: Drupal 8 theming deep dive

/themes/ive/ive.theme

Configuration system

Page 49: Drupal 8 theming deep dive

Configuration system

Page 50: Drupal 8 theming deep dive

• 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

Page 51: Drupal 8 theming deep dive

Adding your own template

Page 52: Drupal 8 theming deep dive

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()).

Page 53: Drupal 8 theming deep dive

/modules/simple/simple.module

Theme hook declaration

Page 54: Drupal 8 theming deep dive

Template file

/modules/simple/templates/simple.html.twig

Page 55: Drupal 8 theming deep dive

/modules/simple/src/Controller/Simple.php

Theme hook use

Page 56: Drupal 8 theming deep dive

A few more things…

Page 57: Drupal 8 theming deep dive

• 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.

Page 58: Drupal 8 theming deep dive

Conclusion

Not so many things

to learn compared to Drupal 7.

Better organization for assets management.

Easier with Drupal 8!

Page 59: Drupal 8 theming deep dive

Thanks!

Page 60: Drupal 8 theming deep dive

Recommended