Top Banner
Drupal 8 Theming Deep Dive
55

Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Jul 20, 2015

Download

Internet

Romain Jarraud
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Drupal 8 Theming Deep Dive

Page 2: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Romain JARRAUD Trainer/consultant

Trained People @romainjarraud (mainly in french)

Page 3: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

What is theming?

Page 4: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theming?

Fonctionnal Display

Drupal and modules Theme

Page 5: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theming?

• THEMING = HTML output

• Other outputs could be RSS feed, JSON…

• HEADfull Drupal8 here!

Page 6: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theming?

• Modules produce datas.

• Modules have default renderings.

• For example, Block module define block.twig.html template which render each block.

• The theme OVERRIDES the default renderings.

Page 7: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theming?

Theme overrides defaults:

• html code for every element on the page.

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

• effets using javascript.

Page 8: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theme

Browser CSS

TemplatesDrupal & modules Content

HTML

Web Page

Request

Page 9: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Create a theme

Page 10: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

Page 11: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

End of the story!

Maybe not…

Page 12: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theme files

Page 13: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Templates

Page 14: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

TWIG

• Drupal 8 uses TWIG.

• TWIG created by Fabien Potencier.

• Templating system to generate HTML.

Page 15: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

TWIG

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

• They hold the HTML tags along with TWIG code.

Page 16: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

TWIG (quickly)

• Display the content of var: {{ var }}.

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

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

• Comments: {# commentaire #}

• Translation:

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

• Ready to be translated through Drupal UI!

Page 17: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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 18: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

/themes/ive/templates/block.html.twig

TWIG

Page 19: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

TWIG Debug

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

Page 20: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

• 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 backend cache render service which avoid Drupal to cache.

Page 21: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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 22: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

• 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 23: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Template suggestions

(Those comments are shown

thanks to TWIG Debug mode)

Page 24: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Override

• Copy original file into theme template folder.

• Rename it if necessary.

• Empty theme registry.

• Do what you want!

Page 25: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Preprocess functions

Page 26: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Preprocess functions

TemplateDefault

preprocess

$variables

Theme preprocess

$variables

Modules preprocess

$variables

Page 27: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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 28: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Librairies

Page 29: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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 30: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Librairies

Declaration Loading

Page 31: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Libraries declaration

/themes/ive/ive.libraries.yml

Page 32: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

Libraries loading

Page 33: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Libraries loading

Loading from a template file

/themes/ive/templates/block.html.twig

Page 34: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

Libraries loading

Page 35: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

Libraries loading

Page 36: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Library dependency• No javascript loaded for anonymous users.

• Dependencies should be explicit.

/themes/ive/ive.libraries.yml

Page 37: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

/themes/ive/js/ive.js

Page 38: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Breakpoints

Page 39: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Breakpoints

Breakpoints are exposed on the server side.

For example, images can be loaded depending on those breakpoints (using Responsive Image

module).

Page 40: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

/themes/ive/ive.breakpoints.yml

Breakpoints

Page 41: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Breakpoints

Page 42: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Configuration

Page 43: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Configuration

• 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 44: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

Configuration

Defines:

• ive.settings configuration.

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

This configuration is loaded during installation.

Page 45: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Configuration/themes/ive/ive.theme

Page 46: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Configuration

Page 47: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theme hook declaration

Page 48: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Theme hook declaration

•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 49: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

/modules/simple/simple.module

Theme hook declaration

Page 50: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

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

Theme hook declaration

Page 51: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

A few more things…

Page 52: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

• Everything is a bloc (nearly): breadcrumb, messages…

• Logo format is SVG.

• Base theme Classy.

• No more theme functions but templates instead.

• No more theme() fonction but Render Arrays.

• No more process functions.

• PLEASE, no more database queries in templates!!!

Page 53: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Conclusion

Not so many things

to learn compared to Drupal 7.

!

Easier with Drupal 8!

Page 54: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Thanks!

Page 55: Drupal8 themingdeepdive drupaldevdays-montpellier17042015

Q&A