Blog (view all)
10 Drupal Theme Functions
Themers don't need to know PHP, but a little knowledge can go a long way!
In the introduction to Drupal Theming handbook, you will find this nifty little statement:
A basic knowledge of PHP is needed for some tasks, but it is possible to avoid it entirely, especially if you do not have a need to override functions.
In 2008 at Drupalcon Boston, most talks that I had about Drupal theming stated that the themer needs to learn PHP. The very next year in DC - as the Drupal Designers were rising in force - more emphasis went into being creative rather than learning code. And although some may argue that these people were 'designers' and not 'themers', most considered themselves themers despite their potential coding shortcomings. When I was starting, I personally got into using Drupal because I felt that it did the development for me.
There is, however, an understanding that is required. This is an understanding of the .info file, an understanding of the template files (and no - this doesn't necessarily mean PHP), and an understanding of the DIV structure.
For a smooth work-flow, it is more imortant to draw the lines between the development and the theming for the project than it is to argue over how much PHP the themer should know. So, as Themers AND Developers, make sure you discuss the limitations of your work before starting a project.
But to make life a little easier for early themers, how about a few tips? Hopefully these tips will help you control more of your template files without having to learn an entire language.
For printing the name of an author, I almost always think it is $author - but that is just for comments. This function will print the NODE author.
<?php print $name; ?>
Great fo "if" statements - if node type is equal to this, print that.
<?php if($node->type == 'blog'): ?> HTML <?php endif; ?>
Flags true if user is logged in
<?php if($logged_in): ?> HTML <?php endif; ?>
Flags true if user is an admin - useful for hiding elements from other users without removing them completely.
<?php if($is_admin):?> HTML <?php endif; ?>
I use this instead of $base_path because this one won't default to just a "/"
<?php print base_path() ; ?>
Follow the above to direct the link to your theme folder.
<?php print path_to_theme(); ?>
This is actually a function you will have to create in the template file. Go to your edit menu page to find your menu name. The term "my-menu" was found at "admin/build/menu-customize/my-menu"
$menu = menu_navigation_links("my-menu");
print theme('links', $menu);
Display a region that is assigned in the .info file (replace myregion with your own)
.info - regions[myregion] = my region
.tpl.php - <?php print $myregion; ?>
display the search form
<?php print $search_box; ?>
good for "if" teaser statements.
<?php if($teaser): ?> HTML <?php endif;?>
Some of these are easier to find than others, but these code snippets were very helpful to me when I was getting started.
If you have useful code snippets for getting started, post them below!