learning php for drupal theming, dc chicago 2009
TRANSCRIPT
![Page 1: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/1.jpg)
Drupal Front EndTips and Tricks
www.hicktech.comwww.designtotheme.com@emmajanedotnet
![Page 2: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/2.jpg)
PHP Survival TechniquesUsing Square Dancing
as an Analogy
![Page 3: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/3.jpg)
About this talk● There are a lot of theme snippets available in the Theme Guide. There
is not, however, a lot of information about PHP which is the language that makes up these snippets. If you're tired of copying, pasting and praying and are ready to understand some of the magic behind those snippets, this session is for you!
● In this session you will learn how to manipulate and master:
● The very, very basics of PHP and the popular theming engine PHPtemplate
● Variables and tpl.php template files
● Arrays and objects and other crowbarworthy data containers.
● The really scary looking stuff that's in the mysterious file template.php
● Examples will be pulled from the Drupal.org Theme Guide as well as the wildly successful book on theming, Front End Drupal (coauthored by Emma Jane and Konstantin Kaefer).
![Page 4: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/4.jpg)
Stick around, I've got copies to give away.
![Page 5: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/5.jpg)
Drupal Theme Guidehttp://drupal.org/themeguide
![Page 6: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/6.jpg)
Theme snippetshttp://drupal.org/node/45471
![Page 7: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/7.jpg)
The Zen Themehttp://drupal.org/project/zen
![Page 8: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/8.jpg)
Learning through analogies
![Page 9: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/9.jpg)
www.travelinghoedowners.com
![Page 10: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/10.jpg)
bootstrappingbootstrapping
![Page 11: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/11.jpg)
http://www.jontwest.com/ProBono.php
VariablesVariables
![Page 12: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/12.jpg)
Available Page Variables
![Page 13: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/13.jpg)
http://opswingers.free.fr/cestquoi.htm
RegionsRegions
![Page 14: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/14.jpg)
Regions
![Page 15: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/15.jpg)
http://www.dehnbase.org/sd/tutorial/counterrotate.php?p=4
FunctionsFunctions
![Page 16: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/16.jpg)
Functions
user_is_logged_in ()
![Page 17: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/17.jpg)
Homework (f'reals)
● It's time to make your firstever function!● www.designtotheme.com
![Page 18: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/18.jpg)
Functions with Parametersuser_access ('access administration pages')
in_array ('admin', array_values ($user>roles))
theme('links', $primary_links, array('class' => 'links primarylinks'))
![Page 19: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/19.jpg)
Theme Functionshttp://api.drupal.org/api/group/themeable/6
theme('links',
$primary_links,
array('class' => 'links primarylinks')
)
See also: theme_links
![Page 20: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/20.jpg)
www.squaredanceomaha.org/dress
ThemingTheming
![Page 21: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/21.jpg)
Decide on the danceChoose your clothes Dance the dance
http://www.kodakgallery.com/Slideshow.jsp?mode=fromshare&Uc=6m9np57.9mj7q0yf&Uy=ripni&Ux=0
PHPtemplatePHPtemplate
![Page 22: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/22.jpg)
PHPtemplate
Collect the content from Drupal using modules Run through the
Drupal theme functions & your
custom theme layer
Print the variables in your template
files
http://www.kodakgallery.com/Slideshow.jsp?mode=fromshare&Uc=6m9np57.9mj7q0yf&Uy=ripni&Ux=0
![Page 23: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/23.jpg)
How to create themes
1.Download an existing theme.
2.Look for variables and functions.
3.Alter the placement of the “printed” things.
4.Save and upload the theme files.
5.Clear the theme registry (Drupal admin).
6.Enjoy your new theme.
![Page 24: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/24.jpg)
tpl.php files are like fashion accessories
![Page 25: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/25.jpg)
page.tpl.php template file<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language>language ?>" xml:lang="<?php print $language>language ?>"> <head> <title><?php print $head_title ?></title> <?php print $head ?> <?php print $styles ?> </head> <body> <div id="container"> <div id="header"> <div id="logoWrapper"> <?php if ($logo) { ?> <div id="logo"> <a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><img src="<?php print $logo ?>" alt="<?php print t('Home') ?>" /></a> </div><?php } ?>
![Page 26: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/26.jpg)
page.tpl.php template file<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language>language ?>" xml:lang="<?php print $language>language ?>"> <head> <title><?php print $head_title ?></title> <?php print $head ?> <?php print $styles ?> </head> <body> <div id="container"> <div id="header"> <div id="logoWrapper"> <?php if ($logo) { ?> <div id="logo"> <a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><img src="<?php print $logo ?>" alt="<?php print t('Home') ?>" /></a> </div><?php } ?>
![Page 27: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/27.jpg)
Variables must be printed
<?php print ?>
![Page 28: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/28.jpg)
Variables must be printed
<title>
<?php print $head_title ?></title>
![Page 29: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/29.jpg)
Zomgwhere'd you find those variables?● Look at /modules/system/page.tpl.php
OR● http://api.drupal.org/api/drupal/modules
systempage.tpl.php/6
![Page 30: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/30.jpg)
api.drupal.org is your friend.go there often.
![Page 31: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/31.jpg)
The modules folder is also your friend.
![Page 32: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/32.jpg)
Look with your eyes,not your editor.
![Page 33: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/33.jpg)
tpl.php files
● Look for basic files:● /modules● Download Zen.● Download Root Candy.
● Copy tpl.php files into your theme's folder.● Manipulate them.
![Page 34: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/34.jpg)
http://www.dehnbase.org/sd/tutorial/counterrotate.php?p=4
ConditionalsConditionalsif (you're the inside couple) {
go clockwise} else {
go counter clockwise.}
![Page 35: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/35.jpg)
What's an “if”?
if ($logo) {
<?php print ?>
}
![Page 36: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/36.jpg)
page.tpl.php template file<!DOCTYPE html PUBLIC "//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language>language ?>" xml:lang="<?php print $language>language ?>"> <head> <title><?php print $head_title ?></title> <?php print $head ?> <?php print $styles ?> </head> <body> <div id="container"> <div id="header"> <div id="logoWrapper"> <?php if ($logo) { ?> <div id="logo"> <a href="<?php print $base_path ?>" title="<?php print t('Home') ?>"><img src="<?php print $logo ?>" alt="<?php print t('Home') ?>" /></a> </div><?php } ?>
![Page 37: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/37.jpg)
Fancy data structures: arrays + objectsGrouping and sorting your data
![Page 38: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/38.jpg)
Fancy data structures: arrays + objectsGrouping and sorting your data
![Page 39: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/39.jpg)
Fancy data structures: arrays
Arrays have sheep.
Objects have “drawers” of sorted content
![Page 40: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/40.jpg)
Fancy data structures: arrays
Arrays have sheep.
Objects have “drawers” of sorted content
![Page 41: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/41.jpg)
Analogies wanted.
![Page 42: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/42.jpg)
Devel Module: Themer Info
![Page 43: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/43.jpg)
$node object
$node>nid$node>body$node>content['body'][#value]
![Page 44: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/44.jpg)
“Advanced” PHP
● Never be afraid to try something.● Always back up your files first.● Use version control.● Take a LOT of notes.● Be bold! And be brave!
![Page 45: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/45.jpg)
Lessons from Drawing Class
1. Imagine what you want.2. Make a gesture drawing.
3. Fill out the details.
![Page 46: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/46.jpg)
Applied to PHP
1. Imagine what you want.
2. Find the right place for it.
3. Write the comments In PHP for what you're about to do.
4. Fill in the code for the comments.
![Page 47: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/47.jpg)
My first Perl scripts had comments explaining “foreach” loops.There is no shame in this level of commenting because I say so.
![Page 48: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/48.jpg)
A snippet for node.tpl.phphttp://drupal.org/node/120855
<?php if ($submitted) { ?>
<span class="submitted">
<?php if ($node>type == 'book') {
if ($node>parent != 0) {
print format_date($node>created, 'custom', "F jS, Y") ;}
} ?>
</span>
<?php } ?>
![Page 49: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/49.jpg)
PHP Snippetfrom: http://drupal.org/node/21401
<?php if ($submitted) { ?><span class="submitted"><?php if ($node>type == 'blog') { print 'Posted ' . format_date($node>created, 'custom', "F jS, Y") . ' by ' . theme('username', $node); } else { print 'By ' . theme('username', $node) . ' <br /> ' . format_date($node>created, 'custom', "F jS, Y") ; } ?></span><?php } ?>
![Page 50: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/50.jpg)
More Homework
● Find a snippet in the theming guide at: http://drupal.org/node/45471
● Figure out what it does.● Test it out in your theme.● Fix the documentation if it's wrong.
![Page 51: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/51.jpg)
template.php: sup with that?
● Preparing variables that weren't assembled by Drupal and its modules.
● Altering the contents of variables that were prepared by Drupal and its modules.
● Special theming functions to do fun things like 'edit this block' links and random images.
● Read the Zen base theme documentation and template.php file.
![Page 52: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/52.jpg)
http://www.flickr.com/photos/98274023@N00/3335326425/
![Page 53: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/53.jpg)
TomatoesPeanut butter and
Mayonnaiseon brown bread.
Wrapped in saran.
![Page 54: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/54.jpg)
http://www.flickr.com/photos/chegs/190039408/
![Page 55: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/55.jpg)
Preprocess functions:making your own (*^#Q$% lunch.
![Page 56: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/56.jpg)
In the file template.phpfunction bolg_preprocess_page (&$variables) {
// Add a "go home" button to page.tpl.php
if ($variables['logged_in'] == TRUE && $variables['is_front'] == FALSE) {
$image_path = $variables['directory'] . "/images/go_home.jpg";
$image_text = t("Go home!");
$image = theme('image', $image_path, $image_text, $image_text);
$variables['go_home'] = l($image, "<front>", array('html'=> TRUE));
}
} // End of the preprocess_page function
http://www.informit.com/articles/article.aspx?p=1336146&seqNum=2
![Page 57: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/57.jpg)
Even More Homework
● Download and dissect the Zen Theme● http://drupal.org/project/zen
● Read Chapter 4 of Front End Drupal● http://www.informit.com/articles/article.aspx?p=1336146
● Imagine why you'd want a new template variable.
● Create a preprocess function.
![Page 58: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/58.jpg)
Enough? Aight. Let's wrap it up...
![Page 59: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/59.jpg)
In short.....PHP theming essentials:
● PHP is a linear “programming” language, just like a dance.
● PHP stores information in variables and actions in functions.
● Sometimes variables hold lots of information in special variables called “arrays” and objects.
● PHP and Drupal both have functions.● Drupal has lots of magic variables that are loaded
with content. Check out: http://drupal.org/themeguide
![Page 60: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/60.jpg)
http://www.jontwest.com/ProBono.php
VariablesVariables
![Page 61: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/61.jpg)
http://opswingers.free.fr/cestquoi.htm
RegionsRegions
![Page 62: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/62.jpg)
http://www.dehnbase.org/sd/tutorial/counterrotate.php?p=4
FunctionsFunctions
![Page 63: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/63.jpg)
http://www.dehnbase.org/sd/tutorial/counterrotate.php?p=4
ConditionalsConditionalsif (you're the inside couple) {
go clockwise
} else {go counter clockwise.
}
![Page 64: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/64.jpg)
Snippets &Snippets &VariablesVariables
● Create a library.● Use a base theme.● Beg, borrow, steal snippets. GPL 'em and
give 'em back to the community.
![Page 65: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/65.jpg)
http://www.flickr.com/photos/98274023@N00/3335326425/
PreprocessPreprocessfunctionsfunctions
![Page 66: Learning PHP for Drupal Theming, DC Chicago 2009](https://reader033.vdocuments.us/reader033/viewer/2022060107/554c4bc8b4c905282a8b4586/html5/thumbnails/66.jpg)
Friends don't let friends eat peanut butter and mayo sandwiches.
[email protected] < sign up for goodiesFront End Drupal < buy the theming book