Drupal 7 Dynamic Themeing via theme()

Drupal has a theme() function which lets you output HTML partials (chunks) for a given piece of content. The theme function takes in content as input and uses theme functions and template files to build HTML output.

From a module or custom drupal theme I use the theme() function regularly for projects. For instance I may have a function to generate a table listing of valid instructors that may access a website. This function could look like the following:

function _mymodule_valid_instructors_table() {

  $header = array(
    array('data' => 'Course ID', 'field' => 'course_id'),
    array('data' => 'Employee ID', 'field' => 'instructor_eid'),
  );
  $rows = array();

  // Query database for instructors.
  $q = db_query(" ... ");

  $result = $q->execute();
  $num_instructors = $result->rowCount();
  while ($record = $result->fetchAssoc()) {
    $rows[] = // ...;
  }

  return theme('table',
    array(
      'header' => $header,
      'rows'=>$rows,
      // Optional Caption for the table
      'caption' => "Showing $num_instructors Instructors",
      // Optional to indicate whether the table headers should be sticky
      'sticky' => TRUE,
      // Optional empty text for the table if resultset is empty
      'empty' => 'No valid instructors found.',
    )
  );
}

This function works, and it’s great. But, sometimes we want to customize the output of the default theme output. 1 way to do this is to override the default theme function to include our custom code, css classes and logic. But, our theme() call above will never be aware of this new implementation of the theme function.

Thankfully, theme() supports dynamic theming. Theme() can actually take an array of target “theme functions” as the 1st parameter. Drupal will take this list of functions and on the 1st valid function, use its return value to theme our content.

So for any given project we could write something similar to this (using my last example as a further example):

  return theme(array('myproject_instructors_table', 'table'),
    array(
      'header' => $header,
      'rows'=>$rows,
      // Optional Caption for the table
      'caption' => "Showing $num_instructors Instructors",
      // Optional to indicate whether the table headers should be sticky
      'sticky' => TRUE,
      // Optional empty text for the table if resultset is empty
      'empty' => 'No valid instructors found.',
    )
  );

And our module code will be more flexible by allowing custom implementations of theme functions — ultimately defaulting to the default core implementation of a theme() function if none were found. As mentioned in the linked documents the Views module makes use of this capability.

note: This theme() tip is how to make a project’s module code or theme code more dynamic. Maybe even only a modules piece of code. There are other override methods like template files, and subthemes to override presentation of content as well — outside of your custom PHP code.

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 9 = 13

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>