SlideShare a Scribd company logo
8 Things to Know About
Theming in Drupal 8
Or, “Lessons I Learned when Building a D8 Subtheme”
Logan Farr || LoganFarr.com || @logan_farr || @loganfarr
Site building
Object oriented programming
Module building
Hooks and API functions not
normally in theming.
JavaScript frameworks
Things we will not
be covering.
Constructing a Drupal 8 (sub)theme
How a Drupal 8 theme works
Templating and Twig*
Template preprocessing and theme functions
What the (theme) differences are between D7 and D8
Tools to debug a theme
Things we will be covering
1.File structures have changed
D8 Base Path
D8 Theme
2. File types have changed
THEME_NAME.info -> THEME_NAME.info.yml
template.php -> THEME_NAME.theme
hook_template.tpl.php -> hook_template.html.twig
dcu.info
name = DCU
description = An example theme for Drupal Camp Utah
base theme = Omega
screenshot = screenshot.png
engine = phptemplate
core = 7.x
stylesheets[all][] = css/dcu.styles.css
scripts[] = js/dcu.behaviors.js
regions[content] = Content
...
dcu.info.yml
name: DCU
type: theme
description: An example theme for Drupal Camp Utah
base theme: Omega
engine = phptemplate
screenshot: screenshot.png
core: 8.x
libraries:
- dcu/global-styling
#Regions
regions:
navigation: Navigation
header: Header
content: Content
machine_readable: Human Readable
stylesheets-remove:
- core/assets/vendor/normalize-css/normalize.css
3. stylesheets-remove
You can prevent stylesheets from rendering on your page!
In THEME_NAME.info.yml:
stylesheets-remove:
- (path to stylesheet here)
- core/assets/.../normalize.css
Note: You need the FULL path of the CSS stylesheet (from the root directory).
Especially useful if:
● Trying to remove default module
styling
● Trying to remove unnecessary
system stylesheets
● Trying to remove a base theme
stylesheet
4. Libraries
Probably the most notable change in Drupal 8.
Define library:
A collection of CSS stylesheets and JS scripts
Can be used globally, or just as needed.
Library Naming (NOT file naming):
THEME_NAME/LIB_NAME
dcu/global-styling
dcu.libraries.yml - Overview
global-styling: #LIBRARY_NAME
version: 1.x
css:
theme:
styles/css/style.css {}
styles/css/print.css { media: print }
js:
js/script.js {}
header:
js/header.js {}
dependencies:
core/jquery
sessions: #LIBRARY_NAME
...
(Yes, jQuery is now included in Core!)
dcu.libraries.yml - CSS
D8 dcu.info.yml:
global-styling:
version: 1.x
css:
theme:
styles/css/style.css {}
styles/css/print.css { media: print }
D7 dcu.info:
stylesheets[all][] = css/global.styles.css
stylesheets[all][] = css/responsive.styles.css
stylesheets[all][] = css/print.css
dcu.libraries.yml - JS
D8 dcu.info.yml:
global-styling:
...
# By default, all JS is loaded in the footer
js:
js/script.js: {}
# But it can be overridden!
js-header:
header: true
js:
js/header.js: {}
js-footer:
js:
js/footer.js: {}
D7 dcu.info:
scripts[] = js/script.js
Attaching a Library to a Page
Options for loading a library:
● Every page
● A subset of pages
● Individual templates
(Remember, THEME_NAME/LIBRARY_NAME is how you reference a library)
To load a library on every page, place in your THEME_NAME.info.yml file.
To load a library on a subset of pages:
<?php
function dcu_preprocess_maintenance_page(&$variables) {
$variables['#attached']['library'][] = dcu/sessions';
}
?>
To load on a Twig template:
{{ attach_library('dcu/sessions') }}
You can override a library and remove a library with the commands
library-override
library-remove
Another note about libraries...
5. Don’t mess up your YML.
If you are developing a theme and you get a WSOD or a 500 error, it’s because you have messed up
YML code.
Use spaces, not tabs
Don’t forget your colons (also forget your semi-colons)
Use colons not equal signs
Follow indentation
6. The Themer’s Best Friend
Twig debugging!
To turn on, edit settings.php and services.yml
sites/default/settings.php
$settings['twig_debug'] = TRUE;
$settings['container_yamls'][] = __DIR__ . '/services.yml';
sites/default/services.yml
parameters:
twig.config:
debug: true
Thanks to Derek Walker (@derekawalker) for helping me out with this.
Also Devel - although a note: the
dpm() function is now kint()
Twig Debug, Turned OFF
Twig Debug, Turned ON
<!-- THEME DEBUG -->
<!-- THEME HOOK: ‘page’ →
<!-- FILE NAME SUGGESTIONS:
x page--page.html.twig
* page--front.html.twig
* page.html.twig
-->
<!-- BEGIN OUTPUT FROM
‘themes/dcu/templates/page--page.html.twig’ -->
(HTML HERE)
Functions now done in THEME_NAME.theme!
Some functions are different though.
For example, to add template suggestions to a page, use
hook_theme_suggestions_HOOK_alter().
Preprocess functions, like in D7, are used to pass variables to the page (Twig) templates.
7. Preprocess Functions are still a thing
Example Functions in dcu.theme
/**
* Implements hook_preprocess_HOOK().
*/
function dcu_preprocess_page(&$variables) {
$variables[‘title’] = ‘Come to the dark side.’;
$variables[‘page_class’] = ‘page-dark-side’;
kint();
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function dcu_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = Drupal::routeMatch()->getParameter('node')) {
$suggestions[] = 'page__' . $node->getType();
}
return $suggestions;
}
8. Templates are now done in Twig (not PHP)
<section id="content-outer-wrapper" class="outer-wrapper clearfix">
<div id="main-layout" class="{{ region_classes.main }}
inner-wrapper clearfix">
{% if page.content %}
{{ page.content }}
{% endif %}
{% if page.sidebar_first %}
{{ page.sidebar_first }}
{% endif %}
{% if page.sidebar_second %}
{{ page.sidebar_second }}
{% endif %}
</div><!-- /#main -->
</section><!-- /#main-outer-wrapper -->
<div class="l-main-wrapper">
<div class="l-main">
<div class="l-content" role="main">
<a id="main-content"></a>
<?php print render($tabs); ?>
<?php print render($page['help']); ?>
<?php if ($action_links): ?>
<ul class="action-links">
<?php print render($action_links); ?>
</ul>
<?php endif; ?>
<?php print render($page['content']); ?>
</div>
<?php print render($page['sidebar_first']); ?>
<?php print render($page['sidebar_second']); ?>
</div>
</div>
Twig Reference & PHP Equivalent
{{ twig_variable }}
{% if twig_variable %}
{% elseif twig_variable %}
{% else %}
{% endif %}
{% for i in 0..10 %}
{{ i }}
{% endfor %}
echo $php_variable;
if($boolean)
else if($boolean)
else
for($i = 0; $i < 10; $i++) {
echo $i;
}
Twig reference to come soon…
Twig Variables & Preprocess Functions
<body class=”{{ page_class }} main”>
<h1>{{ title }} It’s awesome.</h1>
...
</body>
<body class=”page_dark_side main”>
<h1>Come to the dark side. It’s
awesome.</h1>
...
</body>
Twig Loops and PHP Arrays
<?php
function dcu_preprocess_page(&$variables) {
$variables[‘sessions’] = array(
‘auditorium’ => array(
‘title’ => ‘Drupal 8 Theming’,
‘time => ‘11:15 AM’,
),
‘room_b’ => array(
‘title’ => ‘Drulenium’,
‘time’ => ‘11:15 AM’,
),
);
}
?>
{% for session in sessions %}
{{ session.title }} at {{ session.time }}
{% endfor %}
Helpful note: {{ variable_name.key }} for array
elements
Drupal 8 Theming Fundamentals, Part 1
https://www.lullabot.com/articles/drupal-8-theming-fundamentals-part-1
Drupal 8 Assets Theming Guide
https://www.drupal.org/theme-guide/8/assets
Classy Theming by Morten DK
https://drupalwatchdog.com/volume-5/issue-1/classy-theming
My Drupal 8 Omega Subtheme “Resistor” (as reference material)
https://github.com/loganfarr/Resistor
Barcelona DrupalCon - Drupal 8 Theming
https://events.drupal.org/barcelona2015/sessions/drupal-8-theming-0
MortenDK’s Los Angeles DrupalCon Session - Drupal 8 Theming with <3
https://events.drupal.org/losangeles2015/sessions/drupal-8-theming
Twig Reference (Coming Soon)
https://loganfarr.com/twig
This presentation + extended examples (Also coming soon)
https://loganfarr.com/d8-theming
Additional Reading Material
Now go build a
Drupal 8 theme.

More Related Content

What's hot

One-hour Drupal 8 Theming
One-hour Drupal 8 ThemingOne-hour Drupal 8 Theming
One-hour Drupal 8 Theming
Mediacurrent
 
Drupal 8 - Corso frontend development
Drupal 8 - Corso frontend developmentDrupal 8 - Corso frontend development
Drupal 8 - Corso frontend development
sparkfabrik
 
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Laura Scott
 
Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)
Eugenio Minardi
 
Getting started with drupal 8 code
Getting started with drupal 8 codeGetting started with drupal 8 code
Getting started with drupal 8 code
Forum One
 
Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) Theming
Robert Carr
 
Grok Drupal (7) Theming
Grok Drupal (7) ThemingGrok Drupal (7) Theming
Grok Drupal (7) Theming
PINGV
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme System
Peter Arato
 
Intro to Theming Drupal, FOSSLC Summer Camp 2010
Intro to Theming Drupal, FOSSLC Summer Camp 2010Intro to Theming Drupal, FOSSLC Summer Camp 2010
Intro to Theming Drupal, FOSSLC Summer Camp 2010Emma Jane Hogbin Westby
 
A look at Drupal 7 Theming
A look at Drupal 7 ThemingA look at Drupal 7 Theming
A look at Drupal 7 Theming
Aimee Maree Forsstrom
 
Grok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb updateGrok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb update
Laura Scott
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
drubb
 
Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Emma Jane Hogbin Westby
 
Drupal 8 Theme System: The Backend of Frontend
Drupal 8 Theme System: The Backend of FrontendDrupal 8 Theme System: The Backend of Frontend
Drupal 8 Theme System: The Backend of Frontend
Acquia
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
Amanda Giles
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
drubb
 
Performance on a budget (European Drupal Days 2015)
Performance on a budget (European Drupal Days 2015)  Performance on a budget (European Drupal Days 2015)
Performance on a budget (European Drupal Days 2015)
Eugenio Minardi
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
drubb
 
D7 theming what's new - London
D7 theming what's new - LondonD7 theming what's new - London
D7 theming what's new - London
Marek Sotak
 

What's hot (20)

One-hour Drupal 8 Theming
One-hour Drupal 8 ThemingOne-hour Drupal 8 Theming
One-hour Drupal 8 Theming
 
Drupal 8 - Corso frontend development
Drupal 8 - Corso frontend developmentDrupal 8 - Corso frontend development
Drupal 8 - Corso frontend development
 
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
 
Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)
 
Design to Theme @ CMSExpo
Design to Theme @ CMSExpoDesign to Theme @ CMSExpo
Design to Theme @ CMSExpo
 
Getting started with drupal 8 code
Getting started with drupal 8 codeGetting started with drupal 8 code
Getting started with drupal 8 code
 
Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) Theming
 
Grok Drupal (7) Theming
Grok Drupal (7) ThemingGrok Drupal (7) Theming
Grok Drupal (7) Theming
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme System
 
Intro to Theming Drupal, FOSSLC Summer Camp 2010
Intro to Theming Drupal, FOSSLC Summer Camp 2010Intro to Theming Drupal, FOSSLC Summer Camp 2010
Intro to Theming Drupal, FOSSLC Summer Camp 2010
 
A look at Drupal 7 Theming
A look at Drupal 7 ThemingA look at Drupal 7 Theming
A look at Drupal 7 Theming
 
Grok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb updateGrok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb update
 
Drupal 8: Entities
Drupal 8: EntitiesDrupal 8: Entities
Drupal 8: Entities
 
Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009
 
Drupal 8 Theme System: The Backend of Frontend
Drupal 8 Theme System: The Backend of FrontendDrupal 8 Theme System: The Backend of Frontend
Drupal 8 Theme System: The Backend of Frontend
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Performance on a budget (European Drupal Days 2015)
Performance on a budget (European Drupal Days 2015)  Performance on a budget (European Drupal Days 2015)
Performance on a budget (European Drupal Days 2015)
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
 
D7 theming what's new - London
D7 theming what's new - LondonD7 theming what's new - London
D7 theming what's new - London
 

Similar to 8 things to know about theming in drupal 8

Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes
ramakesavan
 
Drupal theming
Drupal themingDrupal theming
Drupal theming
Philip Norton
 
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011
Ryan Price
 
Drupal 8: What's new? Anton Shubkin
Drupal 8: What's new? Anton ShubkinDrupal 8: What's new? Anton Shubkin
Drupal 8: What's new? Anton ShubkinADCI Solutions
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Debugging in drupal 8
Debugging in drupal 8Debugging in drupal 8
Debugging in drupal 8
Allie Jones
 
Drupal Javascript for developers
Drupal Javascript for developersDrupal Javascript for developers
Drupal Javascript for developers
Dream Production AG
 
Drupal vs WordPress
Drupal vs WordPressDrupal vs WordPress
Drupal vs WordPress
Walter Ebert
 
Improve theming with (Twitter) Bootstrap
Improve theming with  (Twitter) BootstrapImprove theming with  (Twitter) Bootstrap
Improve theming with (Twitter) Bootstrap
Andrey Yurtaev
 
Drupal Development
Drupal DevelopmentDrupal Development
Drupal Development
Jeff Eaton
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalystsvilen.ivanov
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mind
Valentine Matsveiko
 
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
LEDC 2016
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
markparolisi
 
Drupal Themes
Drupal ThemesDrupal Themes
Drupal Themes
akosh
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
Wake Liu
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsDrupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
Luís Carneiro
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
Sérgio Santos
 
Changes to Drupal Themes in version 7 (part 1)
Changes to Drupal Themes in version 7 (part 1)Changes to Drupal Themes in version 7 (part 1)
Changes to Drupal Themes in version 7 (part 1)
Chris Charlton
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
Richard Leland
 

Similar to 8 things to know about theming in drupal 8 (20)

Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes Drupal 7 Theming - Behind the scenes
Drupal 7 Theming - Behind the scenes
 
Drupal theming
Drupal themingDrupal theming
Drupal theming
 
Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011Drupal Theme Development - DrupalCon Chicago 2011
Drupal Theme Development - DrupalCon Chicago 2011
 
Drupal 8: What's new? Anton Shubkin
Drupal 8: What's new? Anton ShubkinDrupal 8: What's new? Anton Shubkin
Drupal 8: What's new? Anton Shubkin
 
Drupal 8 Hooks
Drupal 8 HooksDrupal 8 Hooks
Drupal 8 Hooks
 
Debugging in drupal 8
Debugging in drupal 8Debugging in drupal 8
Debugging in drupal 8
 
Drupal Javascript for developers
Drupal Javascript for developersDrupal Javascript for developers
Drupal Javascript for developers
 
Drupal vs WordPress
Drupal vs WordPressDrupal vs WordPress
Drupal vs WordPress
 
Improve theming with (Twitter) Bootstrap
Improve theming with  (Twitter) BootstrapImprove theming with  (Twitter) Bootstrap
Improve theming with (Twitter) Bootstrap
 
Drupal Development
Drupal DevelopmentDrupal Development
Drupal Development
 
Web applications with Catalyst
Web applications with CatalystWeb applications with Catalyst
Web applications with Catalyst
 
Migrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mindMigrate yourself. code -> module -> mind
Migrate yourself. code -> module -> mind
 
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
Валентин Мацвейко та Владислав Мойсеєнко — D8: Migrate Yourself: code->module...
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
 
Drupal Themes
Drupal ThemesDrupal Themes
Drupal Themes
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
 
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First StepsDrupal Camp Porto - Developing with Drupal: First Steps
Drupal Camp Porto - Developing with Drupal: First Steps
 
Django Vs Rails
Django Vs RailsDjango Vs Rails
Django Vs Rails
 
Changes to Drupal Themes in version 7 (part 1)
Changes to Drupal Themes in version 7 (part 1)Changes to Drupal Themes in version 7 (part 1)
Changes to Drupal Themes in version 7 (part 1)
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 

Recently uploaded

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 

Recently uploaded (20)

Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 

8 things to know about theming in drupal 8

  • 1. 8 Things to Know About Theming in Drupal 8 Or, “Lessons I Learned when Building a D8 Subtheme” Logan Farr || LoganFarr.com || @logan_farr || @loganfarr
  • 2. Site building Object oriented programming Module building Hooks and API functions not normally in theming. JavaScript frameworks Things we will not be covering. Constructing a Drupal 8 (sub)theme How a Drupal 8 theme works Templating and Twig* Template preprocessing and theme functions What the (theme) differences are between D7 and D8 Tools to debug a theme Things we will be covering
  • 3. 1.File structures have changed D8 Base Path D8 Theme
  • 4. 2. File types have changed THEME_NAME.info -> THEME_NAME.info.yml template.php -> THEME_NAME.theme hook_template.tpl.php -> hook_template.html.twig
  • 5. dcu.info name = DCU description = An example theme for Drupal Camp Utah base theme = Omega screenshot = screenshot.png engine = phptemplate core = 7.x stylesheets[all][] = css/dcu.styles.css scripts[] = js/dcu.behaviors.js regions[content] = Content ...
  • 6. dcu.info.yml name: DCU type: theme description: An example theme for Drupal Camp Utah base theme: Omega engine = phptemplate screenshot: screenshot.png core: 8.x libraries: - dcu/global-styling #Regions regions: navigation: Navigation header: Header content: Content machine_readable: Human Readable stylesheets-remove: - core/assets/vendor/normalize-css/normalize.css
  • 7. 3. stylesheets-remove You can prevent stylesheets from rendering on your page! In THEME_NAME.info.yml: stylesheets-remove: - (path to stylesheet here) - core/assets/.../normalize.css Note: You need the FULL path of the CSS stylesheet (from the root directory). Especially useful if: ● Trying to remove default module styling ● Trying to remove unnecessary system stylesheets ● Trying to remove a base theme stylesheet
  • 8. 4. Libraries Probably the most notable change in Drupal 8. Define library: A collection of CSS stylesheets and JS scripts Can be used globally, or just as needed. Library Naming (NOT file naming): THEME_NAME/LIB_NAME dcu/global-styling
  • 9. dcu.libraries.yml - Overview global-styling: #LIBRARY_NAME version: 1.x css: theme: styles/css/style.css {} styles/css/print.css { media: print } js: js/script.js {} header: js/header.js {} dependencies: core/jquery sessions: #LIBRARY_NAME ... (Yes, jQuery is now included in Core!)
  • 10. dcu.libraries.yml - CSS D8 dcu.info.yml: global-styling: version: 1.x css: theme: styles/css/style.css {} styles/css/print.css { media: print } D7 dcu.info: stylesheets[all][] = css/global.styles.css stylesheets[all][] = css/responsive.styles.css stylesheets[all][] = css/print.css
  • 11. dcu.libraries.yml - JS D8 dcu.info.yml: global-styling: ... # By default, all JS is loaded in the footer js: js/script.js: {} # But it can be overridden! js-header: header: true js: js/header.js: {} js-footer: js: js/footer.js: {} D7 dcu.info: scripts[] = js/script.js
  • 12. Attaching a Library to a Page Options for loading a library: ● Every page ● A subset of pages ● Individual templates (Remember, THEME_NAME/LIBRARY_NAME is how you reference a library) To load a library on every page, place in your THEME_NAME.info.yml file. To load a library on a subset of pages: <?php function dcu_preprocess_maintenance_page(&$variables) { $variables['#attached']['library'][] = dcu/sessions'; } ?> To load on a Twig template: {{ attach_library('dcu/sessions') }}
  • 13. You can override a library and remove a library with the commands library-override library-remove Another note about libraries...
  • 14. 5. Don’t mess up your YML. If you are developing a theme and you get a WSOD or a 500 error, it’s because you have messed up YML code. Use spaces, not tabs Don’t forget your colons (also forget your semi-colons) Use colons not equal signs Follow indentation
  • 15. 6. The Themer’s Best Friend Twig debugging! To turn on, edit settings.php and services.yml sites/default/settings.php $settings['twig_debug'] = TRUE; $settings['container_yamls'][] = __DIR__ . '/services.yml'; sites/default/services.yml parameters: twig.config: debug: true Thanks to Derek Walker (@derekawalker) for helping me out with this. Also Devel - although a note: the dpm() function is now kint()
  • 17. Twig Debug, Turned ON <!-- THEME DEBUG --> <!-- THEME HOOK: ‘page’ → <!-- FILE NAME SUGGESTIONS: x page--page.html.twig * page--front.html.twig * page.html.twig --> <!-- BEGIN OUTPUT FROM ‘themes/dcu/templates/page--page.html.twig’ --> (HTML HERE)
  • 18. Functions now done in THEME_NAME.theme! Some functions are different though. For example, to add template suggestions to a page, use hook_theme_suggestions_HOOK_alter(). Preprocess functions, like in D7, are used to pass variables to the page (Twig) templates. 7. Preprocess Functions are still a thing
  • 19. Example Functions in dcu.theme /** * Implements hook_preprocess_HOOK(). */ function dcu_preprocess_page(&$variables) { $variables[‘title’] = ‘Come to the dark side.’; $variables[‘page_class’] = ‘page-dark-side’; kint(); } /** * Implements hook_theme_suggestions_HOOK_alter(). */ function dcu_theme_suggestions_page_alter(array &$suggestions, array $variables) { if ($node = Drupal::routeMatch()->getParameter('node')) { $suggestions[] = 'page__' . $node->getType(); } return $suggestions; }
  • 20. 8. Templates are now done in Twig (not PHP) <section id="content-outer-wrapper" class="outer-wrapper clearfix"> <div id="main-layout" class="{{ region_classes.main }} inner-wrapper clearfix"> {% if page.content %} {{ page.content }} {% endif %} {% if page.sidebar_first %} {{ page.sidebar_first }} {% endif %} {% if page.sidebar_second %} {{ page.sidebar_second }} {% endif %} </div><!-- /#main --> </section><!-- /#main-outer-wrapper --> <div class="l-main-wrapper"> <div class="l-main"> <div class="l-content" role="main"> <a id="main-content"></a> <?php print render($tabs); ?> <?php print render($page['help']); ?> <?php if ($action_links): ?> <ul class="action-links"> <?php print render($action_links); ?> </ul> <?php endif; ?> <?php print render($page['content']); ?> </div> <?php print render($page['sidebar_first']); ?> <?php print render($page['sidebar_second']); ?> </div> </div>
  • 21. Twig Reference & PHP Equivalent {{ twig_variable }} {% if twig_variable %} {% elseif twig_variable %} {% else %} {% endif %} {% for i in 0..10 %} {{ i }} {% endfor %} echo $php_variable; if($boolean) else if($boolean) else for($i = 0; $i < 10; $i++) { echo $i; } Twig reference to come soon…
  • 22. Twig Variables & Preprocess Functions <body class=”{{ page_class }} main”> <h1>{{ title }} It’s awesome.</h1> ... </body> <body class=”page_dark_side main”> <h1>Come to the dark side. It’s awesome.</h1> ... </body>
  • 23. Twig Loops and PHP Arrays <?php function dcu_preprocess_page(&$variables) { $variables[‘sessions’] = array( ‘auditorium’ => array( ‘title’ => ‘Drupal 8 Theming’, ‘time => ‘11:15 AM’, ), ‘room_b’ => array( ‘title’ => ‘Drulenium’, ‘time’ => ‘11:15 AM’, ), ); } ?> {% for session in sessions %} {{ session.title }} at {{ session.time }} {% endfor %} Helpful note: {{ variable_name.key }} for array elements
  • 24. Drupal 8 Theming Fundamentals, Part 1 https://www.lullabot.com/articles/drupal-8-theming-fundamentals-part-1 Drupal 8 Assets Theming Guide https://www.drupal.org/theme-guide/8/assets Classy Theming by Morten DK https://drupalwatchdog.com/volume-5/issue-1/classy-theming My Drupal 8 Omega Subtheme “Resistor” (as reference material) https://github.com/loganfarr/Resistor Barcelona DrupalCon - Drupal 8 Theming https://events.drupal.org/barcelona2015/sessions/drupal-8-theming-0 MortenDK’s Los Angeles DrupalCon Session - Drupal 8 Theming with <3 https://events.drupal.org/losangeles2015/sessions/drupal-8-theming Twig Reference (Coming Soon) https://loganfarr.com/twig This presentation + extended examples (Also coming soon) https://loganfarr.com/d8-theming Additional Reading Material
  • 25. Now go build a Drupal 8 theme.