Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

PHP for Themers

Video loading...

  • 0:00
    Advanced Theming in Drupal 7
  • 0:02
    PHP for Themers
  • 0:05
    with James Walker
  • 0:08
    Before we get too far into our advanced theming,
  • 0:10
    I want to take a moment to review some PHP
  • 0:12
    topics that will aid us in doing the work that we've got ahead of us.
  • 0:16
    For those of you who've worked in PHP a lot
  • 0:18
    or feel comfortable with the language, most of this
  • 0:20
    should be review, but we're going to point out
  • 0:22
    some key things that you'll run into frequently
  • 0:24
    when doing advanced theme work.
  • 0:26
    First, we want to start with PHP data structures.
  • 0:29
    Most simply, we have variables.
  • 0:32
    Variables in PHP are prefixed with a dollar sign
  • 0:35
    and all variable names can only have alphanumeric characters--
  • 0:38
    that is alphabetical letters and numerals-- and underscores.
  • 0:43
    There will be two kinds of main data structures
  • 0:45
    that we'll be using in theming.
  • 0:47
    The first is arrays.
  • 0:49
    Arrays are generally lists that map values to keys.
  • 0:53
    They come in two variations.
  • 0:55
    The first is a simple vector array, which is just a list of values.
  • 1:00
    We've got an example here where we've got a simple array with two
  • 1:03
    elements, stingray and telescope.
  • 1:06
    In a simple vector array, the items are indexed
  • 1:08
    by integers beginning with zero.
  • 1:11
    So in this array, we have indexes of 0 and 1.
  • 1:16
    The other common format of arrays in PHP
  • 1:19
    are hash tables or associative arrays.
  • 1:22
    These arrays are indexed by strings or other values
  • 1:26
    not necessarily 0, 1, et cetera.
  • 1:29
    So in our example here, we have an array of cherries and apple
  • 1:33
    with the indexes of red and green.
  • 1:36
    When working with arrays, we'll frequently
  • 1:37
    want to reference certain items that are part of the array.
  • 1:42
    So in our stingray and telescope array example,
  • 1:45
    if we wanted to print out the first item
  • 1:48
    we could reference that by using $stuff and then square brackets
  • 1:53
    around the index, such as 0.
  • 1:55
    Printing that value would give us the string stingray.
  • 1:59
    In a hash table example, we use the same method,
  • 2:02
    but using the key string rather than the integer index.
  • 2:07
    So here for our fruit array, if we print
  • 2:10
    fruit and then green in square brackets,
  • 2:13
    we will get the value apple.
  • 2:16
    The other data structure that we'll see frequently in Drupal theming
  • 2:19
    are objects.
  • 2:21
    Objects have properties that are similar to elements of an array.
  • 2:26
    The syntax is slightly different, though.
  • 2:28
    To reference an object property we use
  • 2:31
    the arrow notation, which is created by a dash and an angle bracket.
  • 2:36
    So as you can see here, we have a robot
  • 2:38
    object that has three properties-- color, material, and finish.
  • 2:44
    Objects in PHP are similar to arrays, the main difference
  • 2:47
    being that they can have methods or functions associated with them.
  • 2:51
    Primarily in theming we'll be just concerned with object properties.
  • 2:56
    In a more complex example, something that we may actually
  • 2:59
    run into in Drupal theming, we'll see
  • 3:02
    that we can chain these things together to get
  • 3:04
    quite complicated data structures.
  • 3:06
    So in our first example, we're printing the nodes links item.
  • 3:12
    So we're starting with we have a node object
  • 3:15
    in which we're referencing a certain property known as links.
  • 3:18
    Links is actually a two-dimensional array where we have
  • 3:23
    first a key of blog_usernames_blog.
  • 3:28
    And then within that, we have an href key.
  • 3:31
    We can see that value prints out blog slash one, which is the URL
  • 3:36
    for an individual user's blog.
  • 3:39
    The next topic we want to cover are conditionals.
  • 3:43
    We'll frequently see these to apply certain display
  • 3:46
    logic, only printing things when certain conditions apply.
  • 3:51
    The most common conditional structure that we'll see
  • 3:53
    is the if else statements.
  • 3:56
    So our example here reads that if a is equal to b,
  • 4:00
    it'll print "it's equal."
  • 4:02
    If the values of a and b aren't equal,
  • 4:05
    then it'll check the next conditional, which
  • 4:08
    is the else if a is less than b.
  • 4:11
    So if the value of a is in fact less than b,
  • 4:13
    then we will print "it's smaller."
  • 4:16
    Finally, if none of the previous conditions are true,
  • 4:18
    we'll fall back to the else statement, which will
  • 4:21
    just print the text "who knows."
  • 4:24
    The if else statements in PHP have a couple of syntax variations
  • 4:28
    that you'll frequently run into in Drupal themes.
  • 4:31
    The first is a colon notation, where again, the logic is the same.
  • 4:36
    We're saying if a is equal to b, then print "it's equal,"
  • 4:40
    otherwise end the statement.
  • 4:42
    The nice thing about this syntax that we'll see frequently in themes
  • 4:45
    is that we can break this syntax up and include
  • 4:48
    some markup in the middle.
  • 4:50
    We'll see examples of that in most template files.
  • 4:54
    The other variation that we'll see is a nice shorthand,
  • 4:57
    if you just have a small conditional that you want to only take up
  • 4:59
    one line.
  • 5:01
    In this syntax, we have our action first.
  • 5:05
    So print is what we want to ultimately do with the value.
  • 5:09
    Then we have our conditional.
  • 5:11
    So if this evaluates to true, then we want to print
  • 5:16
    the part after the question mark.
  • 5:19
    If the conditional is not true, then our else statement
  • 5:23
    is signified by the colon.
  • 5:26
    So what this conditional is doing is if a is equal to b, then
  • 5:30
    it will print "it's equal."
  • 5:31
    Otherwise, it will print nothing.
  • 5:35
    A slightly more complicated version of this conditional
  • 5:37
    is the switch statement.
  • 5:39
    This is something we'll also see in template.php files from time
  • 5:42
    to time.
  • 5:43
    Essentially, switch is a shorthand for very long if and else if
  • 5:47
  • 5:49
    In this example here, we're going to switch our cases based on the value
  • 5:54
    of the time of day variable.
  • 5:57
    And what the switch statement is going
  • 5:58
    to do is based on the value of that variable,
  • 6:02
    it's going to look for matching cases.
  • 6:05
    So in the first block we have the case morning.
  • 6:08
    So if time of day is equal to morning,
  • 6:10
    then it'll print "eat breakfast."
  • 6:13
    The second case is of afternoon.
  • 6:16
    So if time of day is not equal to morning,
  • 6:18
    then it'll move on and check to see if time of day
  • 6:20
    is equal to afternoon.
  • 6:22
    If that's true, then it'll print "lunch time."
  • 6:25
    Finally, much like an else clause, if none of the previous conditions
  • 6:29
    evaluate, than the default syntax can be used, in which case
  • 6:32
    it'll just print Zs.
  • 6:35
    Important to note in switch statements
  • 6:37
    is that you can have multiple cases that may match,
  • 6:40
    which is why we make use of the break statement.
  • 6:43
    So if we want only morning to be applied,
  • 6:46
    we will break after the print "east breakfast."
  • 6:49
    Now let's talk about loops.
  • 6:50
    We use loops in PHP when we want to execute a certain segment of code
  • 6:54
    multiple times in a row.
  • 6:57
    Just like conditional statements, loops
  • 6:59
    have a couple different syntax variations in PHP.
  • 7:02
    The first of these is known as a while loop.
  • 7:05
    In a while loop we're going to have a single condition that will be
  • 7:08
    checked on each iteration of the loop,
  • 7:10
    whether or not the condition still evaluates to true.
  • 7:14
    So in our example here, we have a simple counter variable called i,
  • 7:18
    and as we go through the loop each iteration
  • 7:21
    through the loop the condition's going to be checked
  • 7:23
    to see if I is still less than 10.
  • 7:26
    If that's true, then it will execute the code that will print
  • 7:30
    "Lullabot loves you" followed by a shorthand
  • 7:33
    for incrementing the value of i.
  • 7:36
    So on the first run i will be equal to zero,
  • 7:39
    which is, in fact, less than 10.
  • 7:41
    So "Lullabot loves you" will print, and then
  • 7:43
    i's value will be updated to 1.
  • 7:47
    PHP will then return to the top, check to see that i is
  • 7:50
    still less than 10, and continue.
  • 7:53
    The result will be that "Lullabot loves you"
  • 7:56
    will be printed 10 times in a row.
  • 7:59
    The same structure has an alternate syntax known as a for loop.
  • 8:02
    In for loops, we get a special sort of condition that has three
  • 8:06
    parts separated by semicolons.
  • 8:09
    The first is that initial condition, which is i equals 0,
  • 8:13
    followed by the conditional or the test
  • 8:16
    to check to see if it's still true, followed
  • 8:19
    by a statement to increment the counter, generally.
  • 8:24
    So this for loop functions in the exact same way
  • 8:26
    as the previous while loop.
  • 8:28
    Our i counter begins at 0 and each iteration through the loop
  • 8:32
    is updated by one until it is equal to 10.
  • 8:36
    So we get the exact same result.
  • 8:38
    Lullabot loves you is printed 10 times.
  • 8:41
    Final variation on looping syntax is known as foreach.
  • 8:45
    Foreach is handy if we have an array full of items
  • 8:47
    that we wanted execute some code on each element in the array.
  • 8:52
    So in this example we're going to loop through our stuff array
  • 8:56
    and print each item in that array.
  • 8:58
    So if we recall our original stuff array,
  • 9:01
    we'll get stingray printed by telescope.
  • 9:05
    We can clean this up slightly and add in a br tag, for example,
  • 9:11
    and that'll change our output to be stingray and telescope.
  • 9:15
    Finally we'd like to talk about functions.
  • 9:17
    As we get into more advanced theming topics,
  • 9:19
    we're going to be writing a lot of functions
  • 9:21
    to implement certain hooks inside of Drupal's theme system.
  • 9:25
    Let's have a look at how they're constructed in PHP.
  • 9:29
    Functions in PHP are used to encapsulate
  • 9:31
    frequently repeated tasks.
  • 9:33
    We'll be writing lots of functions as we
  • 9:35
    start to override Drupal theme functions
  • 9:37
    and implement preprocess hooks.
  • 9:40
    The structure of a function begins with the word function,
  • 9:43
    so that we know we're beginning a new function declaration.
  • 9:46
    Then we give a name to our function.
  • 9:48
    In this case, my_function.
  • 9:50
    And then we pass in a list of parameters, which will be
  • 9:53
    the input values to our function.
  • 9:56
    In the case in this example, we have the variable a and b.
  • 10:01
    Finally, a function can optionally have a return
  • 10:03
    value that it provides as output.
  • 10:06
    In this case, we'll be outputting the value of the variable c.
  • 10:10
    There's a couple interesting things to note
  • 10:12
    when working with functions.
  • 10:13
    First off, we can pass any values into the a and b parameters.
  • 10:18
    They can be variables that aren't named a or b, or static values.
  • 10:24
    The other thing to note is that we can have variables that
  • 10:27
    exist only inside the scope of our function, such as the variable c,
  • 10:31
  • 10:32
    The variable c only exists inside the execution of my_function.
  • 10:38
    Let's have a look at how our example function will behave when used.
  • 10:41
    The bottom here, we have a variable x that we're setting equal to 4
  • 10:46
    and then calling print my_function with the variable x passed
  • 10:50
    as the first parameter and the number
  • 10:52
    6 passed as the second parameter.
  • 10:56
    Inside of my function's call now the variable a
  • 10:59
    will be equal to the variable x, which
  • 11:02
    is going to be the value of 4.
  • 11:05
    The parameter b will be equal to the number 6.
  • 11:09
    So we'll see that c is going to get set to 4 plus 6, which is 10,
  • 11:14
    and then that value will be returned.
  • 11:16
    So the result of print my_function will be the number 10.
  • 11:20
    Also inside of our function we have the value
  • 11:23
    of a being set to a plus 1.
  • 11:26
    So inside of the function, the value of a will be five.
  • 11:31
    However, that a variable only exists inside the scope of our function.
  • 11:36
    Thus when we print x after our function call,
  • 11:39
    it's still equal to the number 4.
  • 11:41
    In PHP we can also pass parameters by a reference.
  • 11:45
    We can see this in the syntax by the addition of the ampersand
  • 11:49
    before the parameter a.
  • 11:53
    This will change the behavior of our function slightly when called.
  • 11:56
    If we look at our previous example again, where we have the variable x
  • 12:00
    equal to 4, the first print statement
  • 12:03
    that's printing the result of my function
  • 12:06
    will still be 4 plus 6, which is the value
  • 12:09
    of c, so it'll still print 10.
  • 12:12
    However, because our x variable has now been passed by a reference,
  • 12:17
    any changes that happen inside the body of our function
  • 12:21
    will actually persist after the call has been made.
  • 12:24
    So when we take x and refer to it as a inside of the function,
  • 12:30
    and we do a equals a plus one, that will
  • 12:33
    actually change the value of the x variable from the calling scope.
  • 12:38
    So when we print the x variable after the call to my_function,
  • 12:42
    that variable will now print out the value 5.
  • 12:46
    While we don't have time in this video to cover everything
  • 12:49
    that there is to know about PHP, we hope a few of these simple pointers
  • 12:52
    will help make some of the code that we'll be looking
  • 12:54
    at later make a little bit more sense to those of you
  • 12:57
    who are newer to PHP.
  • 12:59
    If you're looking for more information
  • 13:01
    or want to learn more about PHP,
  • 13:04
    is a great resource that fully documents the entire language.

PHP for Themers


Before we dive into advanced theming we are going to review the basic PHP you will need to use. This is a short review of PHP, including topics like data structures (arrays and objects), conditionals and operators, loops, and functions.

Log in or sign up to download companion files.
Additional resources: