Drupal Front End PHP

68
 Drupal Front End Tips and Tricks www.hicktech.com www.designtotheme.com @emmajanedotnet

Transcript of Drupal Front End PHP

Page 1: Drupal Front End PHP

   

Drupal Front EndTips and Tricks

www.hicktech.comwww.designtotheme.com@emmajanedotnet

Page 2: Drupal Front End PHP

   

PHP Survival TechniquesUsing Square Dancing

as an Analogy

Page 3: Drupal Front End PHP

   

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 crow­bar­worthy 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 (co­authored by Emma Jane and Konstantin Kaefer).

Page 4: Drupal Front End PHP

   Stick around, I've got copies to give away.

Page 5: Drupal Front End PHP

   

Drupal Theme Guidehttp://drupal.org/theme­guide

Page 6: Drupal Front End PHP

   

Theme snippetshttp://drupal.org/node/45471

Page 7: Drupal Front End PHP

   

The Zen Themehttp://drupal.org/project/zen

Page 8: Drupal Front End PHP

   

Learning through analogies

Page 9: Drupal Front End PHP

   www.travelinghoedowners.com

Page 10: Drupal Front End PHP

   bootstrappingbootstrapping

Page 11: Drupal Front End PHP

   http://www.jontwest.com/Pro­Bono.php

VariablesVariables

Page 12: Drupal Front End PHP

   

Variables

Page 13: Drupal Front End PHP

   

Contents of variablesexist inside their containers

Page 14: Drupal Front End PHP

   

Contents of variablesexist inside their containers

http://www.laboutiquedupetitprince.com/en/figures­56/pixi­81/pixi­figure­the­little­prince­sheep­box­518.html

Page 15: Drupal Front End PHP

   http://opswingers.free.fr/cestquoi.htm

RegionsRegions

Page 16: Drupal Front End PHP

   

Regions

Page 17: Drupal Front End PHP

   http://www.dehnbase.org/sd/tutorial/counter­rotate.php?p=4

FunctionsFunctions

Page 18: Drupal Front End PHP

   

Functions

user_is_logged_in ()

Page 19: Drupal Front End PHP

   

Homework (f'reals)

● It's time to make your first­ever function!● www.designtotheme.com

Page 20: Drupal Front End PHP

   

Functions with Parametersuser_access ('access administration pages')

in_array ('admin', array_values ($user­>roles))

theme('links', $primary_links, array('class' => 'links primary­links'))

Page 21: Drupal Front End PHP

   

Theme Functionshttp://api.drupal.org/api/group/themeable/6

theme('links',

    $primary_links,

    array('class' => 'links primary­links')

)

See also: theme_links

Page 22: Drupal Front End PHP

   

Homework Part 2 (also f'reals)

● www.designtotheme.com

Page 23: Drupal Front End PHP

   ThemingTheminghttp://usawestwa.com/Outfit/

Page 24: Drupal Front End PHP

   

www.squaredanceomaha.org/dress

ThemingTheming

Page 25: Drupal Front End PHP

   

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 26: Drupal Front End PHP

   

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 27: Drupal Front End PHP

   

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 28: Drupal Front End PHP

   

page.tpl.php template file<!DOCTYPE html PUBLIC "­//W3C//DTD XHTML 1.0 Strict//EN"          "http://www.w3.org/TR/xhtml1/DTD/xhtml1­strict.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 29: Drupal Front End PHP

   

page.tpl.php template file<!DOCTYPE html PUBLIC "­//W3C//DTD XHTML 1.0 Strict//EN"          "http://www.w3.org/TR/xhtml1/DTD/xhtml1­strict.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 30: Drupal Front End PHP

   

Variables must be printed

    <?php print                               ?>

Page 31: Drupal Front End PHP

   

Variables must be printed

<title>

  <?php print $head_title ?></title>

Page 32: Drupal Front End PHP

   

Zomgwhere'd you find those variables?● Look at /modules/system/page.tpl.php

OR● http://api.drupal.org/api/drupal/modules­­

system­­page.tpl.php/6

Page 33: Drupal Front End PHP

   

api.drupal.org is your friend.go there often.

Page 34: Drupal Front End PHP

   

The modules folder is also your friend.

Page 35: Drupal Front End PHP

   

Look with your eyes,not your editor.

Page 36: Drupal Front End PHP

   

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 37: Drupal Front End PHP

   

Even more tpl.php files

● www.example.com/node/5● page­node­5.tpl.php● page­node.tpl.php● page.tpl.php

● http://www.informit.com/articles/article.aspx?p=1336146

Page 38: Drupal Front End PHP

   http://www.dehnbase.org/sd/tutorial/counter­rotate.php?p=4

ConditionalsConditionalsif (you're the inside couple) {

go clockwise}

} else {go counter clockwise.

}

Page 39: Drupal Front End PHP

   

What's an “if”?

if ($logo) {

    <?php print                               ?>

}

Page 40: Drupal Front End PHP

   

page.tpl.php template file<!DOCTYPE html PUBLIC "­//W3C//DTD XHTML 1.0 Strict//EN"          "http://www.w3.org/TR/xhtml1/DTD/xhtml1­strict.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 41: Drupal Front End PHP

   

Fancy data structures: arrays + objectsGrouping and sorting your data

Page 42: Drupal Front End PHP

   

Fancy data structures: arrays

Multiple values stored in one array

Multiple “drawers” of sorted content

Page 43: Drupal Front End PHP

   

Devel Module: Themer Info

Page 44: Drupal Front End PHP

   

$node object

$node­>nid$node­>body$node­>content['body'][#value]

Page 45: Drupal Front End PHP

   

“Advanced” PHP

● Never be afraid to try something.● Always back up your files first.● Take a LOT of notes.● Be bold! And be brave!

Page 46: Drupal Front End PHP

   

Lessons from Drawing Class

1. Imagine what you want.2. Make a gesture drawing.

3. Fill out the details.

Page 47: Drupal Front End PHP

   

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 48: Drupal Front End PHP

   

My first Perl scripts had comments explaining “foreach” loops.There is no shame in this level of commenting because I say so.

Page 49: Drupal Front End PHP

   

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 50: Drupal Front End PHP

   

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 51: Drupal Front End PHP

   

Homework

● Read a snippet and imagine, sketch, visualize what it does.

http://drupal.org/node/45471

Page 52: Drupal Front End PHP

   

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 53: Drupal Front End PHP

   

Using template.php

Collect the content from Drupal using modules and run it through the 

default theme functions provided by Drupal.

Create new Create new information to feed information to feed 

to your themeto your theme

Print the variables in your template 

files

http://www.kodakgallery.com/Slideshow.jsp?mode=fromshare&Uc=6m9np57.9mj7q0yf&Uy=ripni&Ux=0

Page 54: Drupal Front End PHP

   

http://www.flickr.com/photos/98274023@N00/3335326425/

Page 55: Drupal Front End PHP

   

TomatoesPeanut butter and

Mayonnaiseon brown bread.

Wrapped in saran.

Page 56: Drupal Front End PHP

   

http://www.flickr.com/photos/chegs/190039408/

Page 57: Drupal Front End PHP

   

Preprocess functions:making your own (*^#Q$% lunch.

Page 58: Drupal Front End PHP

   

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 59: Drupal Front End PHP

   

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 60: Drupal Front End PHP

   

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/theme­guide

Page 61: Drupal Front End PHP

   ThemingTheminghttp://usawestwa.com/Outfit/

Page 62: Drupal Front End PHP

   http://www.jontwest.com/Pro­Bono.php

VariablesVariables

Page 63: Drupal Front End PHP

   http://opswingers.free.fr/cestquoi.htm

RegionsRegions

Page 64: Drupal Front End PHP

   http://www.dehnbase.org/sd/tutorial/counter­rotate.php?p=4

FunctionsFunctions

Page 65: Drupal Front End PHP

   http://www.dehnbase.org/sd/tutorial/counter­rotate.php?p=4

ConditionalsConditionalsif (you're the inside couple) {

go clockwise}

} else {go counter clockwise.

}

Page 66: Drupal Front End PHP

   

Snippets &Snippets &VariablesVariables

● Create a library.● Use a base theme.● Beg, borrow, steal snippets. GPL 'em and 

give 'em back to the community.

Page 67: Drupal Front End PHP

   

http://www.flickr.com/photos/98274023@N00/3335326425/

PreprocessPreprocessfunctionsfunctions

Page 68: Drupal Front End PHP

   

Friends don't let friends eat peanut butter and mayo sandwiches.

[email protected] <­­­ homeworkFront End Drupal <­­­ theming book