introduction to drupal (7) theming

44
Theming in Drupal An introduction to theming with a little Drupal 7 twist Drupal Camp Edinburgh May 2011 Rob Carr @robertgcarr

Upload: robert-carr

Post on 27-Jan-2015

127 views

Category:

Technology


1 download

DESCRIPTION

Introduction to Drupal Theming. Given at DrupalCamp Edinburgh (UK) 21 May 2011

TRANSCRIPT

Page 1: Introduction to Drupal (7) Theming

Theming in Drupal

An introduction to theming !with a little Drupal 7 twist!

!Drupal Camp Edinburgh May 2011!

Rob Carr @robertgcarr!

Page 2: Introduction to Drupal (7) Theming

What’s covered…

Prereqs.!Theming concepts.!Theme components.!Hardcore.!Tools.!

Page 3: Introduction to Drupal (7) Theming

Pre-requisites for Theming Adventures

HTML.!CSS.!Basics of Drupal.!Some idea of PHP.!

Page 4: Introduction to Drupal (7) Theming

‘Some idea of PHP:’

•  Print variables!•  Logic (if… then)!•  Call functions!

For the braver: Arrays and Objects knowledge useful

Page 5: Introduction to Drupal (7) Theming

   

Theming Options

Core theme!Off the Shelf:!•  Contributed (d.o)!•  Purchase!Scratch (hardcore!)!Sub-theme!!

Garland!!

Bartik!!

Seven!!

Stark!Never hack

a theme

Page 6: Introduction to Drupal (7) Theming

Sub Theme?

Select [base] theme.!Override elements you don’t like.!Add custom PHP snippets, HTML, CSS or JS.!

   

Base theme!

Sub theme!(your custom code)"

Never hack a theme

Page 7: Introduction to Drupal (7) Theming

What is a Theme?

A folder full of code:!•  Manifest (.info)"

•  Templates (.tpl.php)"

•  Styles (.css)"

•  Logic (template.php)"

•  JavaScript (.js)""

Custom themes live in: /sites/all/themes/

themename

Page 8: Introduction to Drupal (7) Theming

Manifest: yourtheme.info

Theme description.!Requirements.!Stylesheets.!Regions.!Scripts.!(Features).!(Settings).!

;  DESCRIPTION  name  =  yourtheme  descrip=on  =  A  flexible,  simple  custom  theme  based  on  Bar=k.    ;  REQUIREMENTS  core  =  7.x  base  theme  =  bar=k    ;  STYLESHEETS  stylesheets[all][]  =  layout.css  stylesheets[all][]  =  style.css  stylesheets[print][]  =  print.css    ;  REGIONS  regions[header]  =  Header  regions[help]  =  Help  regions[page_top]  =  Page  top  regions[page_boSom]  =  Page  boSom  regions[highlighted]  =  Highlighted  regions[naviga=on]  =  Naviga=on  regions[content]  =  Content  regions[sidebar_first]  =  Sidebar  first  regions[sidebar_second]  =  Sidebar  second  regions[footer]  =  Footer    ;  SCRIPTS  scripts[]  =  scripts.js    

Page 9: Introduction to Drupal (7) Theming

CSS

Cascading Style Sheets – inherit or override!

Page 10: Introduction to Drupal (7) Theming

Regions

;  REGIONS  regions[header]  =  Header  regions[help]  =  Help  regions[page_top]  =  Page  top  regions[page_boSom]  =  Page  boSom  regions[highlighted]  =  Highlighted  regions[naviga=on]  =  Naviga=on  regions[content]  =  Content  regions[sidebar_first]  =  Sidebar  first  regions[sidebar_second]  =  Sidebar  second  regions[footer]  =  Footer  

Page 11: Introduction to Drupal (7) Theming

Templating

Page 12: Introduction to Drupal (7) Theming

Templates html.tpl.php – master template; includes <HEAD> content.!!page.tpl.php – layout of all <BODY> content.!!node.tpl.php – individual nodes (pieces of content).!

html.tpl.php"

node.tpl.php"

field.tpl.php"

page.tpl.php"

block.tpl.php"

block.tpl.php"region.tpl.php"

region.tpl.php"

region.tpl.php"

Page 13: Introduction to Drupal (7) Theming

html.tpl.php

Theme:  Sky  (HTML5  theme)  

Page 14: Introduction to Drupal (7) Theming

html.tpl.php – Generated

Page 15: Introduction to Drupal (7) Theming

Template variables

$page – rendered page content – page.tpl.php

Page 16: Introduction to Drupal (7) Theming

page.tpl.php

Page 17: Introduction to Drupal (7) Theming

page.tpl.php variables

http://api.drupal.org/api/drupal/modules--system--page.tpl.php/7"

<?php  if  ($site_slogan):  ?>                      <h2  class="site-­‐slogan"><?php  print  $site_slogan;  ?></h2>  <?php  endif;  ?>  

Page 18: Introduction to Drupal (7) Theming

page.tpl.php regions

<?php  print  render($page[region_name]);  ?>  

The array $page contains all region content for that particular page"

Page 19: Introduction to Drupal (7) Theming

node.tpl.php Renders the content of a node and all its components:!•  Title!•  Main body!•  Author!•  Links (taxonomy etc)!•  Comments!

Page 20: Introduction to Drupal (7) Theming

Different Content (node) types Node type:!•  Article!•  Blog!•  Event!•  Project!

Template:"•  node--article.tpl.php"•  node--blog.tpl.php"•  node--event.tpl.php"•  node--project.tpl.php"

Don’t have to define node.tpl.php to have

nodetype-specific templates It’s a

double --

Page 21: Introduction to Drupal (7) Theming

Sub Theming - Templates

You don’t have to define every single template if it doesn’t need changing.!

Base theme Your theme

html.tpl.php"

page.tpl.php" page.tpl.php"

node.tpl.php" node.tpl.php"

Page 22: Introduction to Drupal (7) Theming

Templates… and finally You can also create tpl.php files for Views. !!Edit View > Advanced > > Information!

Page 23: Introduction to Drupal (7) Theming

Templates… and finally

You can also create tpl.php files for Views too. !!Edit View > Advanced > > Information!

Page 24: Introduction to Drupal (7) Theming

Logic

Q. Where does the HTML contents of the variables come from?!!A. theme()!

We can override this HTML output by altering any

themable function and even add our

own variables"

Page 25: Introduction to Drupal (7) Theming

theme() 1.  Menu system receives

page request.!2.  Node data built and

template applied.!3.  Node HTML

generated.!4.  Preprocessing to

generate HTML for blocks in each region.!

5.  Page template applied.!

Page 26: Introduction to Drupal (7) Theming

Example - Breadcrumb

Page 27: Introduction to Drupal (7) Theming

How Drupal Outputs Breadcrumb Drupal looks for:!1.  yourtheme_breadcrumb().!2.  sites/all/themes/yourtheme/breadcrumb.tpl.php!3.  theme_breadcrumb().!

Generates (example):""Home » contact us"

Page 28: Introduction to Drupal (7) Theming

Overide theme_breadcrumb()

Example:""Home » contact us"

Becomes:""Home → contact us"

Page 29: Introduction to Drupal (7) Theming

A home for our theme function:

Any custom theme functions live in:!!!!And so much more…!

sites/all/themes/yourtheme/template.php

Page 30: Introduction to Drupal (7) Theming

template.php

Contains function overrides and can add/replace template variables for our theme.!!!Examples:!yourtheme_preprocess_page(&$vars)"yourtheme_preprocess_html(&$vars)"

<theme name>_preprocess_<template name>

Page 31: Introduction to Drupal (7) Theming

Add variable example

Insert into template.php:!!!!Add to node.tpl.php:!!!Clear the cache…!

Page 32: Introduction to Drupal (7) Theming

Add variable example

Insert into template.php:!!!!Add to node.tpl.php:!!!Clear the cache…!

Clear caches"

Page 33: Introduction to Drupal (7) Theming

Congrats! You’ve now built a theme!!!But what next: how can I keep tweaking?!

Page 34: Introduction to Drupal (7) Theming

Tools Firebug (Plugin for FF, Firebug-Lite for others)!IE Developer!

Page 35: Introduction to Drupal (7) Theming

Drupal Functions Drupal API: !http://api.drupal.org/!!Contrib Modules API: http://drupalcontrib.org/api/drupal!

!

Page 36: Introduction to Drupal (7) Theming

Template Variables HTML: http://api.drupal.org/api/drupal/modules--system--html.tpl.php/7!!Page: http://api.drupal.org/api/drupal/modules--system--page.tpl.php/7!!Node: http://api.drupal.org/api/drupal/modules--node--node.tpl.php/7!!

Page 37: Introduction to Drupal (7) Theming

The Killer App! Theme Developer Module http://drupal.org/project/devel_themer!!See also Drupal for Firebug (a bit hardcore)!http://drupal.org/project/drupalforfirebug!!

Page 38: Introduction to Drupal (7) Theming

The Killer App! Theme Developer Module http://drupal.org/project/devel_themer!!See also Drupal for Firebug (a bit hardcore)!http://drupal.org/project/drupalforfirebug!!

Page 39: Introduction to Drupal (7) Theming

Top Tip:

Never eat �yellow snow �

Page 40: Introduction to Drupal (7) Theming

Top Tip:

Never eat �yellow snow �

/admin/config/development/performance/

(‘Clear all caches’)

Or

drush cc all

"

Clear the caches!

Page 41: Introduction to Drupal (7) Theming

Drupal 6

Well I mastered theming in Drupal 6, but what changed when Drupal 7 was released?!!Too much to talk about here, but a great list is at:!http://drupal.org/update/themes/6/7!

Page 42: Introduction to Drupal (7) Theming

Packt – Drupal 7 Themes

Only hard copy book available at time of presentation - published June 2011!!

Page 43: Introduction to Drupal (7) Theming

Again!

We covered the basics:!•  Theme components !•  Templates!•  Overriding theme()!•  Tools!•  Reference!

Page 44: Introduction to Drupal (7) Theming

FIN