Introduction-to-Drupal-8-Theme-Development
Introduction-to-Drupal-8-Theme-Development
Theme Development
Introduction to Drupal 8 Theme Development
Jonathan Daggerhart
Technology Coordinator, Educational Partners International
No. 2, Drupal Camp Asheville 2016
Drupal.org: daggerhart
Twitter: @daggerhart
2
Introduction to Drupal 8 Theme Development
What we will cover
trainer
● js/
○ my-js.js
4
Introduction to Drupal 8 Theme Development
Theme info file (trainer.info.yml)
A theme’s info file tells Drupal about the theme and its
configuration. It uses the YAML file format of key-value pairs and
YA lists of KV pairs.
ML
● Required Properties
trainer.info.yml ○ name: 'Trainer'
description: 'Theme example for DCAVl'
version: 8.x-0.1
type: theme
core: 8.x
● Recommended Properties
5
○ base theme: stable
Introduction to Drupal 8 Theme Development
trainer.info.yml continued...
● Regions - containers for blocks (content)
○ regions:
YA navigation: 'Navigation'
ML
header: 'Top Bar'
footer: 'Footer'
trainer.info.yml content: 'Content'
sidebar_first: 'Primary Sidebar'
● trainer/js/my-js.js -
JS (function($){
alert('hello world - jQuery version ' + $.fn.jquery);
my-js.js
})(jQuery);
8
Introduction to Drupal 8 Theme Development
Child Theme
Creating a new theme as a “child theme” means that your theme
inherits the configuration of another theme. This allows a developer
YA to leverage another theme’s infrastructure in hopes of shorter
ML
theme development time and greater theme stability.
copy ○ page.html.twig
pag
e ● themes/trainer/templates
.htm
l.tw
ig
○ page.html.twig
custom theme
template file
11
Introduction to Drupal 8 Theme Development
Overriding templates - Child theme edition
To override a base theme’s template, copy the template from the that
theme’s templates folder to your theme’s templates folder. If the
pag
e.h
tm l.tw file doesn’t exist in base theme, look in the module for it.
ig
base theme template file Copy template from base theme if it is provided--
● core/themes/classy/templates/
copy
○ Page.html.twig
● html.html.twig
● page.html.twig
● region.html.twig
● node.html.twig
● block.html.twig
● field.html.twig
Using what you’ve learned, find these templates, and add them to
your theme. When in doubt, look in the “system” module. 13
Introduction to Drupal 8 Theme Development
Template suggestions
Drupal’s template discovery process is based on an array of
“template suggestions”. This array contains a list of possible
template file names, and can be modified by modules and themes.
[ "page__", "page__front" ]
14
Introduction to Drupal 8 Theme Development
Using a Template Suggestion
Page Template Suggestions on Front page
"page__",
"page__front"
page.html.twig Copy template and rename it to new suggestion, replacing underscores with dashes--
Result:
copy
● themes/trainer/templates
○ page.html.twig
○ page--front.html.twig
page--front.html.twig
Statements {% if page.sidebar_first %}
<aside class="layout-sidebar-first" role="complementary">
{{ page.sidebar_first }}
</aside>
{% endif %}
{% if page.sidebar_second %}
<aside class="layout-sidebar-second" role="complementary">
{{ page.sidebar_second }}
</aside>
{% endif %}
</main> 17
Introduction to Drupal 8 Theme Development
More about Twig
Variable {% set my_string = ‘The quick brown...‘ %}
Assignment {% set my_list = [ ‘three‘, 2, ‘one‘ ] %}
{% set my_object = { ‘name‘: ‘Bobby‘ } %}
18
Introduction to Drupal 8 Theme Development
Twig Resources
Twig
● Documentation - http://twig.sensiolabs.org/documentation
● Template Designers Syntax - http://twig.sensiolabs.org/doc/templates.html
That is the whole purpose of a theme after all; to stylize the output
of a system, as opposed to governing the system with logic.
20
Introduction to Drupal 8 Theme Development
Theme .theme file continued...
Using the trainer.theme file, let’s hook into Drupal and begin making
modifications to the system! We’ll do the following things:
21
Introduction to Drupal 8 Theme Development
hook_template_suggestions_HOOK_alter
22
Introduction to Drupal 8 Theme Development
Using new template suggestion: node--unpublished.html.twig
Copy the basic node template and rename it to new suggestion, replacing
underscores with dashes, and leaving the file extension “.html.twig”--
Result:
node.html.twig ● themes/trainer/templates
○ node.html.twig
copy
○ node--unpublished.html.twig
Now we have a separate node template for unpublished nodes! This isn’t the most
node--unpublished.html.twig
practical example, but you should now have an idea of easy it can be to add a new
template suggestion when you have the right hook:
hook_template_suggestions_HOOK_alter
23
Introduction to Drupal 8 Theme Development
hook_preprocess_HOOK
Alter variables available to the template before they are sent to Twig
Q: What is
this?
A: That’s a
“render array”
24
Introduction to Drupal 8 Theme Development
Render Arrays
"Render Arrays" or "Renderable Arrays" are the foundational
components of a Drupal page. A render array is a hierarchical
associative array containing data to be rendered and properties
describing how the data should be rendered. A render array is often
returned by a function to specify markup to be sent to the web
browser or other services
25
Introduction to Drupal 8 Theme Development
Render Arrays Example: item list
Create an HTML unordered bullet list of strings:
Rendered Output
27
Introduction to Drupal 8 Theme Development
Render Array Resources
● Drupal 8 Render Arrays API - General overview of render arrays
https://www.drupal.org/developing/api/8/render/arrays
● Drupal 7 Form API - Though the API is technically for Drupal 7, these
properties also apply to Drupal 8.
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x
trainer/templates/node_author.html.twig
31
Introduction to Drupal 8 Theme Development
Hook_theme - In Action
Finally, we’ll use hook_preprocess_node to add our new
element to all node pages. In trainer.theme --
32
Introduction to Drupal 8 Theme Development
Hook_theme - Results
trainer.theme node.html.twig
node_author.html.twig
33