SlideShare a Scribd company logo
CREATING A LEAN, MEAN
DRUPAL 8 THEME
evolvingweb.ca	
@evolvingweb
Drupal Consulting, Development, and Training
evolvingweb.ca
Suzanne Dergacheva, Co-founder
@suzanne_kennedy
Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme
• Introduction to Drupal 8 (1 day)
• Drupal 8 Site Building (2 days)
• Drupal 8 Module Development (2 days)
• Drupal 8 Theming (2 days)
• Zero to Hero Drupal 8 Training (5 days)
training@evolvingweb.ca
evolvingweb.ca/training
Drupal 8 Training
Drupal Themes Can
be Complicated
CSS
Templates
mytheme.theme
or template.php
Regions
Configuration for Display and Layout
• Why are Drupal themes complex?

• Looking at our Drupal 8 theming toolset

• Managing and limiting the complexity

Creating a Lean, Mean
Drupal 8 Theme
Where Does the
Complexity Come
From?
Design Flexible Theme System
Powerful Configuration
Log in
name
password
Login
Log in
name
password
Login
Joe joined...
Lisa joined...
Peter joined...
Mary joined...
Who's new?
Ever wondered?
Great news
Fantastic offer for you
CSS
Functions
ViewsPanels
Display Suite
Templates
Layout
Typography
Regions
Branding
The Drupal Theme
System is Flexible
Need Lots of Different Layouts?
We Can Add Templates!
Need to Add Display Logic
We Can Add Preprocess
Functions!
Specific Design Specs?
Comic Book Tourist Brochure Corporate
We Can Write Tons of CSS
and JavaScript
segment of _blocks.scss
Drupal Configuration
is Powerful
Add a Byline to the Page Title
Create a View
Need to Group Fields?
Use Field Collections!
Varying Content Displays
Featured Menu
Listing GridFeatured Grid
Use View Modes
Layout Variations
Panels or Display Suite
Designers…
Designers Can Create Lots of
Variations
The Designer Can Create Lots
of Variations
Look at all the Fancy Layouts!
Everything is Unique!
FAQ About Us
Board of
Directors
Landing Pages
Design Flexible Theme System
Powerful Configuration
Log in
name
password
Login
Log in
name
password
Login
Joe joined...
Lisa joined...
Peter joined...
Mary joined...
Who's new?
Ever wondered?
Great news
Fantastic offer for you
CSS
Functions
ViewsPanels
Display Suite
Templates
Layout
Typography
Regions
Branding
Enter Drupal 8
• Libraries for loading CSS/JS

• Twig Templating

• More Templates!

• Core Base Themes: Classy and Stable

• Breakpoints
Drupal 8 Theming
• No Javascript by default

• SMACSS-style categorization of CSS

• HTML5 Markup

• Stable theme

• Better accessibility

• De-coupled Drupal is possible
D8 Theme Improvements
Does that mean that Drupal 8
solves all our theming
problems?
Using Your Front-end
Toolset
Stable
Classy
Stable
html.html.twig
Classy
html.html.twig
Libraries
mytheme.libraries.yml
global-styling:
css:
theme:
css/styles.css: {}
search-styling:
css:
theme:
css/search.css: {}
Attaching Libraries
libraries:
- ‘mytheme/global-styling’
Globally in mytheme.info.yml
Attaching Libraries
{{ attach_library(‘mytheme/search-styling’) }}
Contextually in page--search.html.twig
Attaching Libraries
Contextually in mytheme.theme
Contextual CSS
• Use libraries when there is a significant
body specific CSS + JS
Search CSS + JS
Global CSS
Store CSS + JS
Too Many Libraries?
Only create separate libraries for sections of
the site that are separate + specific
Search Store
Front Page User ProfilesLanding Pages
CSS in Libraries
Don’t make your selectors too specific
Search Store
CSS + SASS
Don’t nest your selectors 

when you don’t have to

Writing Good SASS
Don’t use IDs if you can

avoid it

Writing Good SASS
Name and use your 

variables consistently

Writing Good SASS
Writing Good SASS
• @extend should always appear before
properties

• @include should appear second

• Review the CSS that gets generated
https://github.com/anthonyshort/idiomatic-sass
Templates
Core Templates
More Core Templates
Don’t Create Templates
Use Twig Blocks
{% extends 'node.html.twig' %}
{% block nodeheader %}
<p>This is an article node.</p>
{% endblock %}
node--article.html.twig
{% block nodeheader %}
<p>This is a generic node.</p>
{% endblock %}
node.html.twig
No Field Left Behind
{{ content|without('links') }}
{{ content.links }}
{{ content.links }}
Preprocess
Functions
Customize the Display Logic
Preprocess Functions:
The Good!
• Transform content before it gets rendered in
templates

• Link one field to another

• Change the format of the submitted text

• Add new variables to the render array
Preprocess Functions:
The Ugly!
• Formatting field types

• Overrides that depend on a node ID

• Database calls to style past events and ‘on
tour’ events differently

• Commerce line item calculations for
currencies

• Hiding results of a view
Preprocess vs. Template
• Preprocess
• Adding logic

• Adding new
variables

• When the output
depends on the
context

!
!
• Templates
• Adding classes

• Wrapping fields
in different
markup

• Bundling fields
together
View Modes
Varying Content Displays
Featured Menu
Listing GridFeatured Grid
It’s easier than
ever to create view
modes
Adding View Modes in D8
Don’t Use View Modes as a
Switch
Theme Settings
Theme Variations
Theme Settings
Alternative to creating multiple themes
Main theme
Sub-theme!
different branding
Main theme mytheme.settings.yml
+
Regions
Only the Regions You Need
Header
Hero
Navigation
Content
Content Bottom
Footer
Your Mission: Limit
the Complexity of the
Theme
Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme
1. Be consistent
• Make choices and stick with them

• If you’re using view modes, define and use
them consistently

• If you’re using the Bootstrap theme, apply
the Bootstrap classes for all layouts
2. Recognize Complexity
• Adding template suggestions to create more
templates

• Adding a lot of classes and IDs

• Adding view modes for each content type

• Adding a lot of theme settings

• Using Node IDs in your preprocess functions

• You have a library for every page

• You have a node template for each content
type
3. Ask: How hard will this be
to maintain?
• How much work will it be to apply this to
a new content type?

• What will happen if the client wants to
add a field?

• What if the branding changes?

• Will your colleague, client, or future self
understand this?
4. Documentation
• What to document:

• Regions that don’t get displayed
everywhere

• Preprocess functions that are complex

• Templates - to indicate what you
changed

• Comment your libraries to say why you
added them
5. Refactor
• If you come across a theme that’s very
complex, refactor it before it’s too late!

• Tools to use:

• Site diff: github.com/evolvingweb/
sitediff

• Visual diff tool like backtrac.io
• Introduction to Drupal 8 (1 day)
• Drupal 8 Site Building (2 days)
• Drupal 8 Module Development (2 days)
• Drupal 8 Theming (2 days)
• Zero to Hero Drupal 8 Training (5 days)
training@evolvingweb.ca
evolvingweb.ca/training
Drupal 8 Training
DrupalNorth June 16-19 drupalnorth.org

More Related Content

Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme