Community Documentation

Advanced themeing: system_user

Last updated February 19, 2008. Created by webchick on November 3, 2005.
Edited by ax. Log in to edit this page.

This example demonstrates how to build and implement a custom theming function that themes only a portion of the form array. This can be used when:

  • A section of the form needs to be themed identically multiple times, in which case the function can be written once and reused.
  • A form array needs to be returned without the use of drupal_get_form (as in the case of a core hook), but a portion of the array needs to be custom themed.

Before

<?php

function system_user($type, $edit, &$user, $category = NULL) {
  if (
$type == 'form' && $category == 'account') {
   
$allthemes = list_themes();

   
// list only active themes
   
foreach ($allthemes as $key => $theme) {
      if (
$theme->status) {
       
$themes[$key] = $theme;
      }
    }

    if (
count($themes) > 1) {
     
$rows = array();
      foreach (
$themes as $key => $value) {
       
$row = array();

       
// Screenshot column.
       
$screenshot = dirname($value->filename) .'/screenshot.png';
       
$row[] = file_exists($screenshot) ? theme('image', $screenshot, t('Screenshot for %theme theme', array('%theme' => $value->name)), '', 'class="screenshot"', false) : t('no screenshot');

       
// Information field.
       
$field = '<strong>'. $value->name .'</strong>';
       
$row[] = $field;

       
// Reset to follow site default theme if user selects the site default
       
if ($key == variable_get('theme_default', 'bluemarine')) {
         
$key = '';
          if (
$edit['theme'] == variable_get('theme_default', 'bluemarine')) {
           
$edit['theme'] = '';
          }
        }

       
// Selected column.
       
$row[] = array('data' => form_radio('', 'theme', $key, ($edit['theme'] == $key) ? 1 : 0), 'align' => 'center');

       
$rows[] = $row;
      }
     
$header = array(t('Screenshot'), t('Name'), t('Selected'));
     
$data[] = array('title' => t('Theme settings'), 'data' => form_item('', theme('table', $header, $rows), t('Selecting a different theme will change the look and feel of the site.')), 'weight' => 2);
    }

    if (
variable_get('configurable_timezones', 1)) {
     
$zones = _system_zonelist();
     
$data[] = array('title' => t('Locale settings'), 'data' => form_select(t('Time zone'), 'timezone', strlen($edit['timezone']) ? $edit['timezone'] : variable_get('date_default_timezone', 0), $zones, t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.')), 'weight' => 2);
    }
    return
$data;
  }
}
?>

After

<?php
function system_user($type, $edit, &$user, $category = NULL) {
  if (
$type == 'form' && $category == 'account') {
   
$themes = list_themes();
   
ksort($themes);

   
// Reset to follow site default theme if user selects the site default
   
if ($key == variable_get('theme_default', 'bluemarine')) {
     
$key = '';
      if (
$edit['theme'] == variable_get('theme_default', 'bluemarine')) {
       
$edit['theme'] = '';
      }
    }

   
### Notice the use of the '#weight' attribute--individual elements can now be weighted
    ### to reorder the form.  Also note the '#theme' attribute--this is a reference to the
    ### custom theming function for this section of the form.  The function is named by
    ### prepending theme_ to the value of the '#theme' attribute.
   
$form['themes'] = array(
     
'#type' => 'fieldset', '#title' => t('Theme configuration'), '#description' => t('Selecting a different theme will change the look and feel of the site.'), '#weight' => 2, '#collapsible' => TRUE, '#collapsed' => FALSE, '#theme' => 'system_user');

    foreach (
$themes as $info) {
     
$info->screenshot = dirname($info->filename) . '/screenshot.png';
     
$screenshot = file_exists($info->screenshot) ? theme('image', $info->screenshot, t('Screenshot for %theme theme', array('%theme' => $info->name)), '', array('class' => 'screenshot'), false) : t('no screenshot');

     
$form['themes'][$info->name]['screenshot'] = array('#type' => 'markup', '#value' => $screenshot);
     
$form['themes'][$info->name]['description'] = array('#type' => 'item', '#title' => $info->name'#value' => dirname($info->filename));
     
$options[$info->name] = '';
    }

   
$form['themes']['theme'] = array('#type' => 'radios', '#options' => $options, '#default_value' => $edit['theme'] ? $edit['theme'] : variable_get('theme_default', 'bluemarine'));

    if (
variable_get('configurable_timezones', 1)) {
     
$zones = _system_zonelist();
     
$form['locale'] = array('#type'=>'item', '#title' => t('Locale settings'), '#weight' => 6);
     
$form['locale']['timezone'] = array(
       
'#type' => 'select', '#title' => t('Time zone'), '#default_value' => strlen($edit['timezone']) ? $edit['timezone'] : variable_get('date_default_timezone', 0),
       
'#options' => $zones, '#description' => t('Select your current local time. Dates and times throughout this site will be displayed using this time zone.')
      );
    }

   
### This is an implementation of a core hook, hook_user--so the constructed form array is
    ### returned without calling drupal_get_form.  This makes the use of the individual theming
    ### function necessary, otherwise no theming information could be passed back to the hook
   
return $form;
  }
}
?>

This is the individual theming function whose callback was specified above. Notice that it follows the same basic approach as the custom theming function in the previous example, including returning $output. The only difference is that this function only themes the part of the form where it's callback was declared.

<?php
function theme_system_user($form) {
  foreach (
element_children($form) as $key) {
   
$row = array();
    if (
is_array($form[$key]['description'])) {
     
$row[] = form_render($form[$key]['screenshot']);
     
$row[] = form_render($form[$key]['description']);
     
$row[] = form_render($form['theme'][$key]);
    }
   
$rows[] = $row;
  }

 
$header = array(t('Screenshot'), t('Name'), t('Selected'));
 
$output = theme('table', $header, $rows);
  return
$output;
}
?>

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here