Render API in Drupal7


Shmaleniuk Nikolay
n.shmaleniuk@gmail.com
Gold Sponsor of
DrupalCamp Kyiv 2011
Silver Sponsors of
DrupalCamp Kyiv 2011
Render API introduction

Render API similar to the work of the Form API.
1. The system collects an array which contains all the
   necessary data
– Array data converted to html and displayed
Main hooks

• hook_page_build
 add items to the page
• hook_page_alter
 override the output of all page elements before output
dsm(hook_page_alter_data)
Render API theming

• #theme - theme_function to be called
• #arg_1, #arg_2 - arguments with prefix "#", necessary for the
  theme function
The advantages of using

• One general system of generating output data
• All html on the page, can be easily overridden by using a one
  hook
• Reusability code. Reusability the menu callbacks or blocks
  for the their tasks (eg AJAX replies)
• Caching
3 types of element

1. Standerd element, key #type (hook_element_info())
2. Text data, key #markup
3. Theme element, key #theme
Example, node.tpl.php
render(), show(), hide()

  <?php
   hide($content['comments']);
   hide($content['links']);
   print render($content);
  ?>

  <?php if (!empty($content['links'])): ?>
   <div class="links"><?php print render($content['links']); ?>

   </div>
  <?php endif; ?>

  <?php print render($content['comments']); ?>
Example
// Drupal 6.
function my_module_show_same_items() {
  $items = array('item 1', 'item 2', 'item 3');
  $out = theme('item_list', $items);
  return $out;
}

// Drupal 7.
function my_module_show_some_items() {
  $items = array('item 1', 'item 2', 'item 3');
  $build = array(
    'items' => array('#theme' => 'item_list', '#items' => $items),
  );
  return $build;
}
Example
function theme_pager_link($variables) {
  $var_text = $variables['var'];
  $var_page_new = $variables['var_2'];
... }

function my_module_show_some_text() {
  $item = array(
    'items' => array(
       '#theme' => 'pager_link',
       '#var' => 'some text',
       '#var_2' => '...',
    ),
  );
  return $item;
}
The array keys Render API
•   #access - TRUE or FALSE
•   #type - str
•   #printed - TRUE or FALSE
•   #cache - array()
•   #theme - str
•   #theme_wrappers - array()
•   #pre_render - array()
•   #post_render - array()
•   #attached - array()
•   #prefix, #suffix - str
Sequence of actions drupal_render()

1. Checking #access и #printed
2. Checking the cache
3. Loading the default item (if #type)
4. Call #pre_render
5. Call #theme
6. Call #theme_wrappers
7. Call #post_render
8. Load attached recurses #attached (JS, CSS, etc.)
9. If #cache set, the write cache
10.return #prefix . $out . #suffix;
Attached recurses, JS/CSS files

$form['#attached']['css'] = array(
  drupal_get_path('module', 'ajax_example') .
'/ajax_example.css',
);

$form['#attached']['js'] = array(
  drupal_get_path('module', 'ajax_example') . '/ajax_example.js',
);
Attached recurses, JS settings

$settings = array('id' => 'mymodule-element-1');

$form['#attached']['js'][] = array(
  'data' => array('mymodule' => $settings),
  'type' => 'setting',
);
Attached recurses, http header
and library
$form['#attached']['drupal_add_http_header'] = array(
  array('Content-Type', 'application/rss+xml; charset=utf-8'),
);

$form['#attached']['library'][] = array('system', 'drupal.ajax');
Caching

  $build = array(
    'items' => array(
        '#theme' => 'item_list',
        '#items' => $items,
'#cache' => array(
            'keys'        => array('example', 'cache'),
            'bin'         => 'cache',
            'expire'      => time() + $interval,
            'granularity' => DRUPAL_CACHE_PER_PAGE |
                             DRUPAL_CACHE_PER_ROLE,
        ),
     ),
  );
Caching

$items = module_load_item_list();
$build = array(
  'items' => array(
      '#theme' => 'item_list',
      '#items' => $items,
      '#cache' => array(
          'keys'        => array('example', 'cache'),
          'bin'         => 'cache',
          'expire'      => time() + $interval,
          'granularity' => DRUPAL_CACHE_PER_PAGE |
                           DRUPAL_CACHE_PER_ROLE,
      ),
   ),
);
Caching
$build = array(
  'items' => array(
      '#theme' => 'item_list',
      '#pre_render' => array( 'module_load_item_list'),
      '#items' => array(),
      '#cache' => array(
          'keys'        => array('example', 'cache'),
          'granularity' => DRUPAL_CACHE_PER_PAGE,
      ),
   ),
);
function module_load_item_list(&$element) {
  $element['#items'] = module_load_item_list();
}
• Render Arrays in Drupal
  http://drupal.org/node/930760
• Examples
  http://drupal.org/project/examples



Shmaleniuk Nikolay
n.shmaleniuk@gmail.com

Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011

  • 1.
    Render API inDrupal7 Shmaleniuk Nikolay n.shmaleniuk@gmail.com
  • 2.
  • 3.
  • 4.
    Render API introduction RenderAPI similar to the work of the Form API. 1. The system collects an array which contains all the necessary data – Array data converted to html and displayed
  • 5.
    Main hooks • hook_page_build add items to the page • hook_page_alter override the output of all page elements before output
  • 7.
  • 10.
    Render API theming •#theme - theme_function to be called • #arg_1, #arg_2 - arguments with prefix "#", necessary for the theme function
  • 11.
    The advantages ofusing • One general system of generating output data • All html on the page, can be easily overridden by using a one hook • Reusability code. Reusability the menu callbacks or blocks for the their tasks (eg AJAX replies) • Caching
  • 12.
    3 types ofelement 1. Standerd element, key #type (hook_element_info()) 2. Text data, key #markup 3. Theme element, key #theme
  • 13.
    Example, node.tpl.php render(), show(),hide() <?php hide($content['comments']); hide($content['links']); print render($content); ?> <?php if (!empty($content['links'])): ?> <div class="links"><?php print render($content['links']); ?> </div> <?php endif; ?> <?php print render($content['comments']); ?>
  • 14.
    Example // Drupal 6. functionmy_module_show_same_items() { $items = array('item 1', 'item 2', 'item 3'); $out = theme('item_list', $items); return $out; } // Drupal 7. function my_module_show_some_items() { $items = array('item 1', 'item 2', 'item 3'); $build = array( 'items' => array('#theme' => 'item_list', '#items' => $items), ); return $build; }
  • 15.
    Example function theme_pager_link($variables) { $var_text = $variables['var']; $var_page_new = $variables['var_2']; ... } function my_module_show_some_text() { $item = array( 'items' => array( '#theme' => 'pager_link', '#var' => 'some text', '#var_2' => '...', ), ); return $item; }
  • 16.
    The array keysRender API • #access - TRUE or FALSE • #type - str • #printed - TRUE or FALSE • #cache - array() • #theme - str • #theme_wrappers - array() • #pre_render - array() • #post_render - array() • #attached - array() • #prefix, #suffix - str
  • 17.
    Sequence of actionsdrupal_render() 1. Checking #access и #printed 2. Checking the cache 3. Loading the default item (if #type) 4. Call #pre_render 5. Call #theme 6. Call #theme_wrappers 7. Call #post_render 8. Load attached recurses #attached (JS, CSS, etc.) 9. If #cache set, the write cache 10.return #prefix . $out . #suffix;
  • 18.
    Attached recurses, JS/CSSfiles $form['#attached']['css'] = array( drupal_get_path('module', 'ajax_example') . '/ajax_example.css', ); $form['#attached']['js'] = array( drupal_get_path('module', 'ajax_example') . '/ajax_example.js', );
  • 19.
    Attached recurses, JSsettings $settings = array('id' => 'mymodule-element-1'); $form['#attached']['js'][] = array( 'data' => array('mymodule' => $settings), 'type' => 'setting', );
  • 20.
    Attached recurses, httpheader and library $form['#attached']['drupal_add_http_header'] = array( array('Content-Type', 'application/rss+xml; charset=utf-8'), ); $form['#attached']['library'][] = array('system', 'drupal.ajax');
  • 21.
    Caching $build= array( 'items' => array( '#theme' => 'item_list', '#items' => $items, '#cache' => array( 'keys' => array('example', 'cache'), 'bin' => 'cache', 'expire' => time() + $interval, 'granularity' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE, ), ), );
  • 22.
    Caching $items = module_load_item_list(); $build = array( 'items' => array( '#theme' => 'item_list', '#items' => $items, '#cache' => array( 'keys' => array('example', 'cache'), 'bin' => 'cache', 'expire' => time() + $interval, 'granularity' => DRUPAL_CACHE_PER_PAGE | DRUPAL_CACHE_PER_ROLE, ), ), );
  • 23.
    Caching $build = array( 'items' => array( '#theme' => 'item_list', '#pre_render' => array( 'module_load_item_list'), '#items' => array(), '#cache' => array( 'keys' => array('example', 'cache'), 'granularity' => DRUPAL_CACHE_PER_PAGE, ), ), ); function module_load_item_list(&$element) { $element['#items'] = module_load_item_list(); }
  • 24.
    • Render Arraysin Drupal http://drupal.org/node/930760 • Examples http://drupal.org/project/examples Shmaleniuk Nikolay n.shmaleniuk@gmail.com