SlideShare a Scribd company logo
Subtitel
1
© Ibuildings 2014/2015 - All rights reserved
#DrupalDaysEU
DRUPAL THEMING -
a practical approach
by Chris Jansen
#DrupalDaysEU
© Ibuildings 2014/2015 - All rights reserved
Gold Sponsors
#DrupalDaysEU
© Ibuildings 2014/2015 - All rights reserved
Media Sponsors
Silver Sponsors
A practical approach to the basics
Chris Jansen
20 march 2015, Milan
Drupal theming
About me
5
About me
6
Topics
Tools and materials
Introducing basic concepts
Design breakdown
Creating the theme
Common mistakes
7
Tools and materials
8
Tools
Virtualbox
Vagrant
9
Materials
Git repository
Slides on slideshare
Install virtualbox and vagrant
http://www.vagrantup.com/downloads
https://www.virtualbox.org/wiki/Downloads
10
Copy image
Supplied on several USB sticks
Boot VM
vagrant up
Access drupal site
192.168.33.10
user: admin
pass: admin
11
Access server
vagrant ssh
Docroot on server
/vagrant/public/ == /var/www/public
Introducing basic
concepts
12
Introducing basic concepts
• Directory layout
• Basic theme elements
• Entities & view modes
• Render arrays
• Hooks
• Render process
• Overriding theme functions
• Overriding templates
13
Directory layout
14
drupal.org/project/*
Custom and/or
sandbox modules.
Modules created by
the features module.
Custom and/or
contributed themes.
Best practice
Basic theme elements
• Regions
• Blocks
• Logo
• Site name & slogan
• Main/Secondary menu
15
Entities & view modes
Entities are stored information (usually
content)
• Nodes
• Taxonomy terms
• Users
• etc.
16
Entities & view modes
View modes are different ways to display
entities
• Node
• Full (Default view mode)
• Teaser (configurable by default)
• Rss
• Taxonomy
• Taxonomy term page (Default view mode)
• User
• User account (Default view mode)
• etc.
17
Uitwerken
Adding your own view modes
Create a custom module
• Implement hook_entity_info_alter()
Use contrib module:
• Entity View Mode
• https://www.drupal.org/project/entity_view_mode
• Display suite
• https://www.drupal.org/project/ds
18
Keyed array of data
• #Propery or #value
• Child
Render arrays - WHAT?
19
render()
theme_container()
drupal_attributes()
render()
Render arrays - HOW?
Properties
• What to render
• How to render it
Children
• What to include within
20
Properties
• What to render
• How to render it
render()
theme_item_list()
drupal_attributes()
drupal_process_attached()
Render arrays - HOW?
21
Render arrays - HOW?
Properties
• What to render
• How to render it
22
render()
Render arrays - WHY?
Data can be changed, markup can not.
• Change positioning
• Add/Change/Remove values
23
Render arrays - WHY?
Data can be changed, markup can not.
• Change positioning
• Add/Change/Remove values
24
Hooks
hook_*
• Modules only
hook_*_alter
• Modules
• Themes
template_*
• Modules
• Themes
theme_*
• Themes
25
Module
Base theme
Subtheme
Current theme
Theme function
• Fast
• Limited (pre)processing
• theme_*()
Template
• Flexible
• *.tpl.php
Render process
theme()
26
render aray
theme function template
HTML
process

template_process_*
preprocess

template_preprocess_*
Overriding theme functions
Overrides live in template.php
Copy & rename original theme function
• theme_item_list -> themename_item_list
Alter the function to suit your needs.
Clear cache
27
Overriding templates
Copy template file to your theme folder
• modules/block/block.tpl.php -> sites/all/
themes/yourtheme/templates/block.tpl.php
Alter the template
Clear cache
28
Best practice: Place your templates in a template folder
e.g.: ../yourtheme/templates/block.tpl.php
Design breakdown
29
Design breakdown
Identify regions
Determine additional requirements
30
Identify regions
Top bar
Main menu
Left sidebar
Main content
Footer
31
Identify regions
Top bar
Main menu
Sidebar Left & Right
Main content
Footer
32
Identify regions
Top bar
Main menu
Sidebar Left & Right
Main content
Split footer Left & Right
Footer
33
Identify regions
Top bar
Main menu
Sidebar Left & Right
Main content
Below content Left & Right
Footer
34
Determine additional requirements
Browser support
Slideshows
Fancy menu’s
etc.
35
Creating the theme
36
Where to start
Buy a theme
Start from scratch
Subtheme
• Start from scratch
• Starterkit
37
Creating the theme
Creating the .info file
• Theme from scratch
• Subtheme from scratch
• Define regions
• Define CSS/JS
‣ Override/disable CSS/JS
Other ways to add CSS/JS
Alter markup using templates
38
Theme from scratch
Required keys
• name
• core
Recommended keys
• description
Commonly used optional keys
• base theme
• stylesheets
• scripts
39
Subtheme from scratch
Required keys
• name
• core
Recommended keys
• description
Commonly used optional keys
• base theme
• stylesheets
• scripts
40
Exercises
41
1. Create a new theme
2. Change your theme to be a Zen subtheme
3. Compare your subtheme to Zen, what do you
notice?
Subtheme compared to Zen
Different regions
• Unless defined otherwise Drupal assumes
default regions: Header, Highlighted, Help,
Content, Left sidebar, Right sidebar, Footer
No stylesheets
• All stylesheets defined in basetheme.info are
inherited, but only if at least one stylesheet has
been defined in mytheme.info
• Zen conditionally defines stylesheets in
hook_preprocess_html
No Logo
42
Add region to .info file
Clear caches
Print the region in page.tpl.php
Define regions
43
Best practice: Place your templates in a template folder
e.g.: ../yourtheme/templates/block.tpl.php
Exercises
44
1. Add zen’s regions to your info file
2. Add a custom region to your info file
3. Render the region in page.tpl.php
Hints:
• Copy zen’s page.tpl.php
• git checkout ex-1 for previous
exercise results
Define CSS/JS
Add CSS/JS to info file
Clear Caches
45
Other ways to add CSS/JS
Functions to use
• using drupal_add_css()
• using drupal_add_js()
• using [‘#attached’] in a render array()
Where to use them
• hook_preprocess_HOOK()
• hook_process_HOOK()
• hook_TYPE_alter()
46
#attached example
Drupal_add_* example
Other ways to add CSS/JS
47
Other ways to add CSS/JS
Adding options to your JS
48
Other ways to add CSS/JS
Add/Override/Remove directly
• hook_js_alter();
• hook_css_alter();
49
Exercise
50
Experiment
1. Define CSS in info file
2. Remove/override CSS in info file
3. Add css using drupal_add_css()
4. Add a “hello world” script using
drupal_add_js()
5. Add css using [‘#attached’]
6. Add a “hello world” script using [‘#attached’]
7. Place a script in the footer.
Hints:
• git checkout ex-2 for previous
exercise results
Alter markup using templates
Lets create a two-column article.
Copy template file to your theme folder
sites/all/themes/zen/templates/node.tpl.php ->
sites/all/themes/yourtheme/templates/node.tpl.php
Alter the template
Clear cache
51
Alter markup using templates
52
Alter markup using templates
Problem!
• Template is used for all content types
Solution: suggestions
• node.tpl.php
• node--type.tpl.php
• node--type--view_mode.tpl.php (EVM)
• node--nodeid.tpl.php
• node--nodeid--view_mode.tpl.php (EVM)
53
Display the author on an article page.
• Render user with view mode teaser
• Use preprocessing
• Use a template
Exercise
54
Hints:
• user_load()
• user_view()
• $variables[‘theme_hook_suggestions’]
• git checkout ex-3 for previous exercise
results
Author name
Article title
Article text Article text
Article text Article text
Article text Article text
Article text Article text Article text
Article text Article text Article text
Article text Article text Article text
Common mistakes
55
Hacking core, modules or themes
Why not?
• Breaks upgrades
• Maintainability
• theme_*
Alternatives
• template_(pre)process_*
• hook_*_alter hooks
• theme_*
56
Advanced logic in templates
Why not?
• Templates are responsible for display only
• Difficult to get to variables
• Hard to maintain
Alternatives
• (pre)process data before it reaches the template
Only PHP you should use
• if(content_present)
• foreach($content_list as $content)
• print ($content);
57
Changing field ordering in templates
Why not?
• Hard to maintain
Alternatives
• Use view modes
58
Using global $user
Why not?
• Security!
Alternatives
• Use user_uid_optional_load()
59
Not using check_plain()/check_markup()
Why is this a problem?
• Security!
When to use them?
• Check_plain()
• Check_markup()
60
Happy theming
Thank you!
61

More Related Content

PPTX
A look at Drupal 7 Theming
PDF
Introduction to Drupal (7) Theming
PDF
Drupal 7 Theme System
PDF
Performance on a budget (European Drupal Days 2015)
KEY
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
PDF
Grok Drupal (7) Theming - 2011 Feb update
PPT
PSD to a Drupal Theme (using a base theme)
PDF
Grok Drupal (7) Theming
A look at Drupal 7 Theming
Introduction to Drupal (7) Theming
Drupal 7 Theme System
Performance on a budget (European Drupal Days 2015)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming - 2011 Feb update
PSD to a Drupal Theme (using a base theme)
Grok Drupal (7) Theming

What's hot (19)

PPTX
8 things to know about theming in drupal 8
PPTX
Converting (X)HTML/CSS template to Drupal 7 Theme
PDF
Drupal 8 theming deep dive
PDF
Building a Custom Theme in Drupal 8
PDF
Design to Theme @ CMSExpo
PDF
Absolute Beginners Guide to Drupal
PDF
Intro to Theming Drupal, FOSSLC Summer Camp 2010
PDF
Learning PHP for Drupal Theming, DC Chicago 2009
ODP
Drupal Theme Development - DrupalCon Chicago 2011
PDF
Drupal 8 - Corso frontend development
PDF
Building Drupal 6 Theme
PDF
Drupal 8: Theming
PDF
Ts drupal6 module development v0.2
PPTX
Getting started with drupal 8 code
ZIP
Theme Kickstart
PPTX
WordPress Theme Development: Part 2
PDF
Drupal 8 templating with twig
PPTX
WordPress Themes 101 - PSUWeb13 Workshop
PDF
Plone 5 theming
8 things to know about theming in drupal 8
Converting (X)HTML/CSS template to Drupal 7 Theme
Drupal 8 theming deep dive
Building a Custom Theme in Drupal 8
Design to Theme @ CMSExpo
Absolute Beginners Guide to Drupal
Intro to Theming Drupal, FOSSLC Summer Camp 2010
Learning PHP for Drupal Theming, DC Chicago 2009
Drupal Theme Development - DrupalCon Chicago 2011
Drupal 8 - Corso frontend development
Building Drupal 6 Theme
Drupal 8: Theming
Ts drupal6 module development v0.2
Getting started with drupal 8 code
Theme Kickstart
WordPress Theme Development: Part 2
Drupal 8 templating with twig
WordPress Themes 101 - PSUWeb13 Workshop
Plone 5 theming
Ad

Viewers also liked (15)

PDF
Deploying an Open Source DAM in SAAS Mode (European Drupal Days 2015)
PDF
Drupal Security: How to survive Drupalgeddon and prepare for future (European...
PDF
Optimizing MariaDB for Web Applications (European Drupal Days 2015)
PDF
The multilingual Drupal 8 experience (European Drupal Days 2015)
PDF
PhpStorm for Drupal Development (European Drupal Days 2015)
PDF
Sponsorship Opportunities European Drupal Days & Dutch PHP Conference 2015
PDF
The benefits of an elastic infrastructure on a Drupal e-commerce (European Dr...
PDF
Secure Drupal, from start to finish (European Drupal Days 2015)
PDF
Bridging the gap between business and technology - Behaviour Driven Developme...
PDF
Verifying Drupal modules with OWASP ASVS 2014 (European Drupal Days 2015)
PDF
Drupal for Big Data - is it ready? (European Drupal Days 2015)
PDF
Web automation with #d8rules (European Drupal Days 2015)
PDF
Drupal Continuous Integration (European Drupal Days 2015)
PDF
UN World Food Programme Standards & Best Practises (European Drupal Days 2015)
PDF
A Practical Introduction to Symfony (European Drupal Days 2015)
Deploying an Open Source DAM in SAAS Mode (European Drupal Days 2015)
Drupal Security: How to survive Drupalgeddon and prepare for future (European...
Optimizing MariaDB for Web Applications (European Drupal Days 2015)
The multilingual Drupal 8 experience (European Drupal Days 2015)
PhpStorm for Drupal Development (European Drupal Days 2015)
Sponsorship Opportunities European Drupal Days & Dutch PHP Conference 2015
The benefits of an elastic infrastructure on a Drupal e-commerce (European Dr...
Secure Drupal, from start to finish (European Drupal Days 2015)
Bridging the gap between business and technology - Behaviour Driven Developme...
Verifying Drupal modules with OWASP ASVS 2014 (European Drupal Days 2015)
Drupal for Big Data - is it ready? (European Drupal Days 2015)
Web automation with #d8rules (European Drupal Days 2015)
Drupal Continuous Integration (European Drupal Days 2015)
UN World Food Programme Standards & Best Practises (European Drupal Days 2015)
A Practical Introduction to Symfony (European Drupal Days 2015)
Ad

Similar to Drupal theming - a practical approach (European Drupal Days 2015) (20)

PPTX
Theming Drupal: Beyond the Look and Feel
ODP
DrupalEasy: Intro to Theme Development
PDF
Drupal theming
PDF
New Adventures in Drupal Theming
PDF
Nanocon Taiwan
PDF
Introduction into Drupal site building
PPTX
Drupalcamp Atlanta 2010 Design-to-Theme
KEY
PSD to Drupal - Introductory Drupal Theming
PDF
Drupal Front End PHP
PPTX
Drupal Theming for Developers
PPTX
Theming Drupal 6 - An Introduction to the Basics
PPT
Drupal - Introduction to Drupal Creating Modules
PDF
Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme
PPT
SynapseIndia drupal presentation on drupal best practices
PDF
Drupal Theming An Introduction
PDF
Demystifying drupal 7 theming
PPTX
Drupal Theme Development
PDF
(some) Drupal Theming by Ryan Price
ODP
Drupal
PPTX
Drupal theme development
Theming Drupal: Beyond the Look and Feel
DrupalEasy: Intro to Theme Development
Drupal theming
New Adventures in Drupal Theming
Nanocon Taiwan
Introduction into Drupal site building
Drupalcamp Atlanta 2010 Design-to-Theme
PSD to Drupal - Introductory Drupal Theming
Drupal Front End PHP
Drupal Theming for Developers
Theming Drupal 6 - An Introduction to the Basics
Drupal - Introduction to Drupal Creating Modules
Minimalist Theming: How to Build a Lean, Mean Drupal 8 Theme
SynapseIndia drupal presentation on drupal best practices
Drupal Theming An Introduction
Demystifying drupal 7 theming
Drupal Theme Development
(some) Drupal Theming by Ryan Price
Drupal
Drupal theme development

More from Eugenio Minardi (14)

PDF
Delphi and ExtJS (26 ottobre 2017)
PDF
ExtJS: La piattaforma vincente (tools)
PDF
ExtJS: La piattaforma vincente (multiple screens)
PDF
ExtJS: La piattaforma vincente (rich UI)
PDF
ExtJS: La piattaforma vincente (class system)
PDF
ExtJS: La piattaforma vincente
PDF
Distributed Team Management: 
Pitfall, Challenges and Advantages
PDF
Another Copernican Revolution: maintenance first, projects second (European D...
PDF
MongoDB: What, why, when
PDF
Il Web orientato al futuro: Express, Angular e nodeJS
PDF
MEAN: il nuovo stack di sviluppo per il futuro del web
PDF
Gestione della configurazione in Drupal 8
PDF
Labortatorio di Information Design e UX con Drupal
PDF
Drupal dashboard for dummies with d3
Delphi and ExtJS (26 ottobre 2017)
ExtJS: La piattaforma vincente (tools)
ExtJS: La piattaforma vincente (multiple screens)
ExtJS: La piattaforma vincente (rich UI)
ExtJS: La piattaforma vincente (class system)
ExtJS: La piattaforma vincente
Distributed Team Management: 
Pitfall, Challenges and Advantages
Another Copernican Revolution: maintenance first, projects second (European D...
MongoDB: What, why, when
Il Web orientato al futuro: Express, Angular e nodeJS
MEAN: il nuovo stack di sviluppo per il futuro del web
Gestione della configurazione in Drupal 8
Labortatorio di Information Design e UX con Drupal
Drupal dashboard for dummies with d3

Recently uploaded (20)

PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPTX
Digital Literacy And Online Safety on internet
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Introduction to Information and Communication Technology
PDF
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
DOCX
Unit-3 cyber security network security of internet system
PDF
Testing WebRTC applications at scale.pdf
PDF
Paper PDF World Game (s) Great Redesign.pdf
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPT
tcp ip networks nd ip layering assotred slides
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PPTX
artificial intelligence overview of it and more
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
SASE Traffic Flow - ZTNA Connector-1.pdf
Digital Literacy And Online Safety on internet
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Introduction to Information and Communication Technology
💰 𝐔𝐊𝐓𝐈 𝐊𝐄𝐌𝐄𝐍𝐀𝐍𝐆𝐀𝐍 𝐊𝐈𝐏𝐄𝐑𝟒𝐃 𝐇𝐀𝐑𝐈 𝐈𝐍𝐈 𝟐𝟎𝟐𝟓 💰
Introuction about ICD -10 and ICD-11 PPT.pptx
Tenda Login Guide: Access Your Router in 5 Easy Steps
Unit-3 cyber security network security of internet system
Testing WebRTC applications at scale.pdf
Paper PDF World Game (s) Great Redesign.pdf
Module 1 - Cyber Law and Ethics 101.pptx
Introuction about WHO-FIC in ICD-10.pptx
tcp ip networks nd ip layering assotred slides
Cloud-Scale Log Monitoring _ Datadog.pdf
Triggering QUIC, presented by Geoff Huston at IETF 123
artificial intelligence overview of it and more
Slides PDF The World Game (s) Eco Economic Epochs.pdf
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf

Drupal theming - a practical approach (European Drupal Days 2015)

  • 1. Subtitel 1 © Ibuildings 2014/2015 - All rights reserved #DrupalDaysEU DRUPAL THEMING - a practical approach by Chris Jansen
  • 2. #DrupalDaysEU © Ibuildings 2014/2015 - All rights reserved Gold Sponsors
  • 3. #DrupalDaysEU © Ibuildings 2014/2015 - All rights reserved Media Sponsors Silver Sponsors
  • 4. A practical approach to the basics Chris Jansen 20 march 2015, Milan Drupal theming
  • 7. Topics Tools and materials Introducing basic concepts Design breakdown Creating the theme Common mistakes 7
  • 10. Install virtualbox and vagrant http://www.vagrantup.com/downloads https://www.virtualbox.org/wiki/Downloads 10 Copy image Supplied on several USB sticks Boot VM vagrant up
  • 11. Access drupal site 192.168.33.10 user: admin pass: admin 11 Access server vagrant ssh Docroot on server /vagrant/public/ == /var/www/public
  • 13. Introducing basic concepts • Directory layout • Basic theme elements • Entities & view modes • Render arrays • Hooks • Render process • Overriding theme functions • Overriding templates 13
  • 14. Directory layout 14 drupal.org/project/* Custom and/or sandbox modules. Modules created by the features module. Custom and/or contributed themes. Best practice
  • 15. Basic theme elements • Regions • Blocks • Logo • Site name & slogan • Main/Secondary menu 15
  • 16. Entities & view modes Entities are stored information (usually content) • Nodes • Taxonomy terms • Users • etc. 16
  • 17. Entities & view modes View modes are different ways to display entities • Node • Full (Default view mode) • Teaser (configurable by default) • Rss • Taxonomy • Taxonomy term page (Default view mode) • User • User account (Default view mode) • etc. 17 Uitwerken
  • 18. Adding your own view modes Create a custom module • Implement hook_entity_info_alter() Use contrib module: • Entity View Mode • https://www.drupal.org/project/entity_view_mode • Display suite • https://www.drupal.org/project/ds 18
  • 19. Keyed array of data • #Propery or #value • Child Render arrays - WHAT? 19
  • 20. render() theme_container() drupal_attributes() render() Render arrays - HOW? Properties • What to render • How to render it Children • What to include within 20
  • 21. Properties • What to render • How to render it render() theme_item_list() drupal_attributes() drupal_process_attached() Render arrays - HOW? 21
  • 22. Render arrays - HOW? Properties • What to render • How to render it 22 render()
  • 23. Render arrays - WHY? Data can be changed, markup can not. • Change positioning • Add/Change/Remove values 23
  • 24. Render arrays - WHY? Data can be changed, markup can not. • Change positioning • Add/Change/Remove values 24
  • 25. Hooks hook_* • Modules only hook_*_alter • Modules • Themes template_* • Modules • Themes theme_* • Themes 25 Module Base theme Subtheme Current theme
  • 26. Theme function • Fast • Limited (pre)processing • theme_*() Template • Flexible • *.tpl.php Render process theme() 26 render aray theme function template HTML process
 template_process_* preprocess
 template_preprocess_*
  • 27. Overriding theme functions Overrides live in template.php Copy & rename original theme function • theme_item_list -> themename_item_list Alter the function to suit your needs. Clear cache 27
  • 28. Overriding templates Copy template file to your theme folder • modules/block/block.tpl.php -> sites/all/ themes/yourtheme/templates/block.tpl.php Alter the template Clear cache 28 Best practice: Place your templates in a template folder e.g.: ../yourtheme/templates/block.tpl.php
  • 30. Design breakdown Identify regions Determine additional requirements 30
  • 31. Identify regions Top bar Main menu Left sidebar Main content Footer 31
  • 32. Identify regions Top bar Main menu Sidebar Left & Right Main content Footer 32
  • 33. Identify regions Top bar Main menu Sidebar Left & Right Main content Split footer Left & Right Footer 33
  • 34. Identify regions Top bar Main menu Sidebar Left & Right Main content Below content Left & Right Footer 34
  • 35. Determine additional requirements Browser support Slideshows Fancy menu’s etc. 35
  • 37. Where to start Buy a theme Start from scratch Subtheme • Start from scratch • Starterkit 37
  • 38. Creating the theme Creating the .info file • Theme from scratch • Subtheme from scratch • Define regions • Define CSS/JS ‣ Override/disable CSS/JS Other ways to add CSS/JS Alter markup using templates 38
  • 39. Theme from scratch Required keys • name • core Recommended keys • description Commonly used optional keys • base theme • stylesheets • scripts 39
  • 40. Subtheme from scratch Required keys • name • core Recommended keys • description Commonly used optional keys • base theme • stylesheets • scripts 40
  • 41. Exercises 41 1. Create a new theme 2. Change your theme to be a Zen subtheme 3. Compare your subtheme to Zen, what do you notice?
  • 42. Subtheme compared to Zen Different regions • Unless defined otherwise Drupal assumes default regions: Header, Highlighted, Help, Content, Left sidebar, Right sidebar, Footer No stylesheets • All stylesheets defined in basetheme.info are inherited, but only if at least one stylesheet has been defined in mytheme.info • Zen conditionally defines stylesheets in hook_preprocess_html No Logo 42
  • 43. Add region to .info file Clear caches Print the region in page.tpl.php Define regions 43 Best practice: Place your templates in a template folder e.g.: ../yourtheme/templates/block.tpl.php
  • 44. Exercises 44 1. Add zen’s regions to your info file 2. Add a custom region to your info file 3. Render the region in page.tpl.php Hints: • Copy zen’s page.tpl.php • git checkout ex-1 for previous exercise results
  • 45. Define CSS/JS Add CSS/JS to info file Clear Caches 45
  • 46. Other ways to add CSS/JS Functions to use • using drupal_add_css() • using drupal_add_js() • using [‘#attached’] in a render array() Where to use them • hook_preprocess_HOOK() • hook_process_HOOK() • hook_TYPE_alter() 46
  • 48. Other ways to add CSS/JS Adding options to your JS 48
  • 49. Other ways to add CSS/JS Add/Override/Remove directly • hook_js_alter(); • hook_css_alter(); 49
  • 50. Exercise 50 Experiment 1. Define CSS in info file 2. Remove/override CSS in info file 3. Add css using drupal_add_css() 4. Add a “hello world” script using drupal_add_js() 5. Add css using [‘#attached’] 6. Add a “hello world” script using [‘#attached’] 7. Place a script in the footer. Hints: • git checkout ex-2 for previous exercise results
  • 51. Alter markup using templates Lets create a two-column article. Copy template file to your theme folder sites/all/themes/zen/templates/node.tpl.php -> sites/all/themes/yourtheme/templates/node.tpl.php Alter the template Clear cache 51
  • 52. Alter markup using templates 52
  • 53. Alter markup using templates Problem! • Template is used for all content types Solution: suggestions • node.tpl.php • node--type.tpl.php • node--type--view_mode.tpl.php (EVM) • node--nodeid.tpl.php • node--nodeid--view_mode.tpl.php (EVM) 53
  • 54. Display the author on an article page. • Render user with view mode teaser • Use preprocessing • Use a template Exercise 54 Hints: • user_load() • user_view() • $variables[‘theme_hook_suggestions’] • git checkout ex-3 for previous exercise results Author name Article title Article text Article text Article text Article text Article text Article text Article text Article text Article text Article text Article text Article text Article text Article text Article text
  • 56. Hacking core, modules or themes Why not? • Breaks upgrades • Maintainability • theme_* Alternatives • template_(pre)process_* • hook_*_alter hooks • theme_* 56
  • 57. Advanced logic in templates Why not? • Templates are responsible for display only • Difficult to get to variables • Hard to maintain Alternatives • (pre)process data before it reaches the template Only PHP you should use • if(content_present) • foreach($content_list as $content) • print ($content); 57
  • 58. Changing field ordering in templates Why not? • Hard to maintain Alternatives • Use view modes 58
  • 59. Using global $user Why not? • Security! Alternatives • Use user_uid_optional_load() 59
  • 60. Not using check_plain()/check_markup() Why is this a problem? • Security! When to use them? • Check_plain() • Check_markup() 60