5. Drupal 6 way
● Use theme functions
● Implement hook_theme
● Move large markup to templates
1 $items = array('item-1', 'item-2', 'item-3', 'item-4', 'item-5');
2 $output = theme('item_list', $items);
6. Drupal 6 way - advantages
● Common way to render elements
● Reusable functions
● Predictable markup
● Possibility to change generation
● Output altering
7. Drupal 6 way - disadvantages
● Slower performance
● Caching difficulties
● Different parameters
8. Drupal 7 way
● Use renderable arrays
● Alter content you need
1 $items = array('item-1', 'item-2', 'item-3', 'item-4', 'item-5');
2 $output = array(
3 '#items' => $items,
4 '#theme' => 'item_list'
5 );
9. Drupal 7 way advantages
● Content alterable in a common way
● All the renderable elements have preprocess
and process functions
● Transparent caching
● Resources could be attached to elements
14. #type
● Loads defaults from hook_element_info()
1 // If the default values for this element have not been loaded yet, populate
2 // them.
3 if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
4 $elements += element_info($elements['#type']);
5}
1 function module_template_element_info() {
2 return array(
3 'file_template' => array(
4 '#name' => 'misc',
5 '#fileinfo' => array(
6 'filename' => '[module_name].[name].[extension]',
7 'path' => 'includes',
8 'extension' => 'inc'
9)
10 )
11 );
11 }
15. #theme
● Invokes theme() function
1 // Get the children of the element, sorted by weight.
2 $children = element_children($elements, TRUE);
3
4 // Initialize this element's #children, unless a #pre_render callback already
5 // preset #children.
6 if (!isset($elements['#children'])) {
7 $elements['#children'] = '';
8}
9 // Call the element's #theme function if it is set. Then any children of the
10 // element have to be rendered there.
11 if (isset($elements['#theme'])) {
12 $elements['#children'] = theme($elements['#theme'], $elements);
13 }
14 // If #theme was not set and the element has children, render them now.
15 // This is the same process as drupal_render_children() but is inlined
16 // for speed.
17 if ($elements['#children'] == '') {
18 foreach ($children as $key) {
19 $elements['#children'] .= drupal_render($elements[$key]);
20 }
21 }
16. #theme_wrappers
● Wrap #children element with code
1 // Let the theme functions in #theme_wrappers add markup around the rendered
2 // children.
3 if (isset($elements['#theme_wrappers'])) {
4 foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
5 $elements['#children'] = theme($theme_wrapper, $elements);
6}
7}
17. #states
● Adds JavaScript to change the state of an
element based on another element
1 $form['toggle_me'] = array(
2 '#type' => 'checkbox',
3 '#title' => t('Tick this box to type'),
4 );
5 $form['settings'] = array(
6 '#type' => 'textfield',
7 '#states' => array(
8 // Only show this field when the 'toggle_me' checkbox is enabled.
9 'visible' => array(
10 ':input[name="toggle_me"]' => array('checked' => TRUE),
11 ),
12 ),
13 );
20. #cache
● 'keys' => an array of keys which will be
concatenated to form the cache key.
● 'bin' => the name of the cache bin to be used
(as in 'cache' or 'cache_page', etc.
● 'expire' => a Unix timestamp indicating the
expiration time of the cache.
● 'granularity' => a bitmask indicating the cache
type. This should be
DRUPAL_CACHE_PER_PAGE,
DRUPAL_CACHE_PER_ROLE, or
DRUPAL_CACHE_PER_USER
21. Some cache usage tips
● Don’t cache small items
● Items from #attached not stored with rendered
Elements 5 100 500
items
With #cache
Without #cache
3211
747
3276
4257
3235
18336
● Use cache targeting
● Cache items will not be expired until cron runs,
regardless of the expiration time used
22. Summary
● Don’t use direct call of theme() function
● Generate HTML as later as possible