61
© Ibuildings 2014/2015 - All rights reserved #DrupalDaysEU DRUPAL THEMING - a practical approach by Chris Jansen

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

Embed Size (px)

Citation preview

Page 1: Drupal theming - a practical approach (European Drupal Days 2015)

Subtitel

1© Ibuildings 2014/2015 - All rights reserved

#DrupalDaysEU

DRUPAL THEMING - a practical approach

by Chris Jansen

Page 2: Drupal theming - a practical approach (European Drupal Days 2015)

#DrupalDaysEU

© Ibuildings 2014/2015 - All rights reserved

Gold Sponsors

Page 3: Drupal theming - a practical approach (European Drupal Days 2015)

#DrupalDaysEU

© Ibuildings 2014/2015 - All rights reserved

Media Sponsors

Silver Sponsors

Page 4: Drupal theming - a practical approach (European Drupal Days 2015)

A practical approach to the basics

Chris Jansen20 march 2015, Milan

Drupal theming

Page 5: Drupal theming - a practical approach (European Drupal Days 2015)

About me

5

Page 6: Drupal theming - a practical approach (European Drupal Days 2015)

About me

6

Page 7: Drupal theming - a practical approach (European Drupal Days 2015)

Topics

Tools and materials Introducing basic concepts Design breakdown Creating the theme Common mistakes

7

Page 8: Drupal theming - a practical approach (European Drupal Days 2015)

Tools and materials

8

Page 9: Drupal theming - a practical approach (European Drupal Days 2015)

Tools

Virtualbox Vagrant

9

Materials

Git repository Slides on slideshare

Page 10: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 11: Drupal theming - a practical approach (European Drupal Days 2015)

Access drupal site

192.168.33.10 user: admin pass: admin

11

Access server

vagrant ssh

Docroot on server

/vagrant/public/ == /var/www/public

Page 12: Drupal theming - a practical approach (European Drupal Days 2015)

Introducing basic concepts

12

Page 13: Drupal theming - a practical approach (European Drupal Days 2015)

Introducing basic concepts

• Directory layout • Basic theme elements • Entities & view modes • Render arrays • Hooks • Render process • Overriding theme functions • Overriding templates

13

Page 14: Drupal theming - a practical approach (European Drupal Days 2015)

Directory layout

14

drupal.org/project/*

Custom and/or sandbox modules.

Modules created by the features module.

Custom and/or contributed themes.

Best practice

Page 15: Drupal theming - a practical approach (European Drupal Days 2015)

Basic theme elements

• Regions • Blocks • Logo • Site name & slogan • Main/Secondary menu

15

Page 16: Drupal theming - a practical approach (European Drupal Days 2015)

Entities & view modes

Entities are stored information (usually content)

• Nodes • Taxonomy terms • Users • etc.

16

Page 17: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 18: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 19: Drupal theming - a practical approach (European Drupal Days 2015)

Keyed array of data • #Propery or #value • Child

Render arrays - WHAT?

19

Page 20: Drupal theming - a practical approach (European Drupal Days 2015)

render()theme_container()

drupal_attributes()

render()

Render arrays - HOW?

Properties • What to render • How to render it

Children • What to include within

20

Page 21: Drupal theming - a practical approach (European Drupal Days 2015)

Properties • What to render • How to render it

render()theme_item_list()

drupal_attributes()

drupal_process_attached()

Render arrays - HOW?

21

Page 22: Drupal theming - a practical approach (European Drupal Days 2015)

Render arrays - HOW?

Properties • What to render • How to render it

22

render()

Page 23: Drupal theming - a practical approach (European Drupal Days 2015)

Render arrays - WHY?

Data can be changed, markup can not. • Change positioning • Add/Change/Remove values

23

Page 24: Drupal theming - a practical approach (European Drupal Days 2015)

Render arrays - WHY?

Data can be changed, markup can not. • Change positioning • Add/Change/Remove values

24

Page 25: Drupal theming - a practical approach (European Drupal Days 2015)

Hooks

hook_* • Modules only

hook_*_alter • Modules • Themes

template_* • Modules • Themes

theme_* • Themes

25

Module

Base theme

Subtheme

Current theme

Page 26: Drupal theming - a practical approach (European Drupal Days 2015)

Theme function • Fast • Limited (pre)processing • theme_*()

Template • Flexible • *.tpl.php

Render process

theme()

26

render aray

theme function template

HTML

processtemplate_process_*

preprocesstemplate_preprocess_*

Page 27: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 28: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 29: Drupal theming - a practical approach (European Drupal Days 2015)

Design breakdown

29

Page 30: Drupal theming - a practical approach (European Drupal Days 2015)

Design breakdown

Identify regions Determine additional requirements

30

Page 31: Drupal theming - a practical approach (European Drupal Days 2015)

Identify regions

Top bar Main menu Left sidebar Main content Footer

31

Page 32: Drupal theming - a practical approach (European Drupal Days 2015)

Identify regions

Top bar Main menu Sidebar Left & Right Main content Footer

32

Page 33: Drupal theming - a practical approach (European Drupal Days 2015)

Identify regions

Top bar Main menu Sidebar Left & Right Main content Split footer Left & Right Footer

33

Page 34: Drupal theming - a practical approach (European Drupal Days 2015)

Identify regions

Top bar Main menu Sidebar Left & Right Main content Below content Left & Right Footer

34

Page 35: Drupal theming - a practical approach (European Drupal Days 2015)

Determine additional requirements

Browser support Slideshows Fancy menu’s etc.

35

Page 36: Drupal theming - a practical approach (European Drupal Days 2015)

Creating the theme

36

Page 37: Drupal theming - a practical approach (European Drupal Days 2015)

Where to start

Buy a theme Start from scratch Subtheme

• Start from scratch • Starterkit

37

Page 38: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 39: Drupal theming - a practical approach (European Drupal Days 2015)

Theme from scratch

Required keys • name • core

Recommended keys • description

Commonly used optional keys • base theme • stylesheets • scripts

39

Page 40: Drupal theming - a practical approach (European Drupal Days 2015)

Subtheme from scratch

Required keys • name • core

Recommended keys • description

Commonly used optional keys • base theme • stylesheets • scripts

40

Page 41: Drupal theming - a practical approach (European Drupal Days 2015)

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?

Page 42: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 43: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 44: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 45: Drupal theming - a practical approach (European Drupal Days 2015)

Define CSS/JS

Add CSS/JS to info file Clear Caches

45

Page 46: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 47: Drupal theming - a practical approach (European Drupal Days 2015)

#attached example

Drupal_add_* example

Other ways to add CSS/JS

47

Page 48: Drupal theming - a practical approach (European Drupal Days 2015)

Other ways to add CSS/JS

Adding options to your JS

48

Page 49: Drupal theming - a practical approach (European Drupal Days 2015)

Other ways to add CSS/JS

Add/Override/Remove directly • hook_js_alter(); • hook_css_alter();

49

Page 50: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 51: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 52: Drupal theming - a practical approach (European Drupal Days 2015)

Alter markup using templates

52

Page 53: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 54: Drupal theming - a practical approach (European Drupal Days 2015)

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 titleArticle text Article text Article text Article text Article text Article textArticle text Article text Article text Article text Article text Article text Article text Article text Article text

Page 55: Drupal theming - a practical approach (European Drupal Days 2015)

Common mistakes

55

Page 56: Drupal theming - a practical approach (European Drupal Days 2015)

Hacking core, modules or themes

Why not? • Breaks upgrades • Maintainability • theme_*

Alternatives • template_(pre)process_* • hook_*_alter hooks • theme_*

56

Page 57: Drupal theming - a practical approach (European Drupal Days 2015)

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

Page 58: Drupal theming - a practical approach (European Drupal Days 2015)

Changing field ordering in templates

Why not? • Hard to maintain

Alternatives • Use view modes

58

Page 59: Drupal theming - a practical approach (European Drupal Days 2015)

Using global $user

Why not? • Security!

Alternatives • Use user_uid_optional_load()

59

Page 60: Drupal theming - a practical approach (European Drupal Days 2015)

Not using check_plain()/check_markup()

Why is this a problem? • Security!

When to use them? • Check_plain() • Check_markup()

60

Page 61: Drupal theming - a practical approach (European Drupal Days 2015)

Happy theming

Thank you!

61