SlideShare a Scribd company logo
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
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
dsm(hook_page_alter_data)
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
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

More Related Content

KEY
第49回Php勉強会@関東 Datasource
KEY
Introducing CakeEntity
KEY
Introducing CakeEntity
KEY
Zf Zend Db by aida
ZIP
What's new in the Drupal 7 API?
KEY
テストデータどうしてますか?
KEY
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
PDF
Advanced Querying with CakePHP 3
第49回Php勉強会@関東 Datasource
Introducing CakeEntity
Introducing CakeEntity
Zf Zend Db by aida
What's new in the Drupal 7 API?
テストデータどうしてますか?
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Advanced Querying with CakePHP 3

What's hot (20)

PDF
The History of PHPersistence
PDF
Database Design Patterns
PDF
Silex meets SOAP & REST
KEY
Lithium Best
PDF
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
PDF
Drupal Field API. Practical usage
PDF
Design Patterns avec PHP 5.3, Symfony et Pimple
PDF
New in cakephp3
PDF
CakeFest 2013 keynote
PDF
Doctrine fixtures
PDF
Jqeury ajax plugins
PDF
Pagination in PHP
KEY
MTDDC 2010.2.5 Tokyo - Brand new API
PDF
Drupal Render API
PDF
Add edit delete in Codeigniter in PHP
PPT
Database api
PDF
Country State City Dropdown in PHP
PPTX
Drupal 8 migrate!
PDF
Be RESTful (Symfony Camp 2008)
PPTX
Wp query
The History of PHPersistence
Database Design Patterns
Silex meets SOAP & REST
Lithium Best
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Drupal Field API. Practical usage
Design Patterns avec PHP 5.3, Symfony et Pimple
New in cakephp3
CakeFest 2013 keynote
Doctrine fixtures
Jqeury ajax plugins
Pagination in PHP
MTDDC 2010.2.5 Tokyo - Brand new API
Drupal Render API
Add edit delete in Codeigniter in PHP
Database api
Country State City Dropdown in PHP
Drupal 8 migrate!
Be RESTful (Symfony Camp 2008)
Wp query
Ad

Viewers also liked (7)

PDF
Pavel Makhrinsky.Field API.DrupalCamp Kyiv 2011
PDF
Automated UI testing.Selenium.DrupalCamp Kyiv 2011
PDF
Oleg Natalushko. Drupal server anatomy. DrupalCamp Kyiv 2011
PDF
Vlad Savitsky.Modules parade.DrupalCamp Kyiv 2011
PDF
Grigory Naumovets.Multilingual sites.DrupalCamp Kyiv 2011
PDF
Eugene Poltorakov.HTML 5 and drupal.DrupalCamp Kiev 2011
PDF
Vadim Mirgorod.OpenAcaDept.DrupalCamp Kyiv 2011
Pavel Makhrinsky.Field API.DrupalCamp Kyiv 2011
Automated UI testing.Selenium.DrupalCamp Kyiv 2011
Oleg Natalushko. Drupal server anatomy. DrupalCamp Kyiv 2011
Vlad Savitsky.Modules parade.DrupalCamp Kyiv 2011
Grigory Naumovets.Multilingual sites.DrupalCamp Kyiv 2011
Eugene Poltorakov.HTML 5 and drupal.DrupalCamp Kiev 2011
Vadim Mirgorod.OpenAcaDept.DrupalCamp Kyiv 2011
Ad

Similar to Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011 (20)

PPTX
DrupalCamp Foz - Novas APIs Drupal 7
PDF
Separation of concerns - DPC12
PPT
Propel sfugmd
PPTX
Let's write secure Drupal code!
PDF
50 Laravel Tricks in 50 Minutes
PDF
laravel tricks in 50minutes
PPTX
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PDF
Drupal 7 Theming - Behind the scenes
KEY
Backbone.js Simple Tutorial
PPTX
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
PDF
DRUPAL 8 STORAGES OVERVIEW
PDF
Lazy evaluation drupal camp moscow 2014
PDF
Metaprogramovanie #1
PDF
Drupal & javascript
PPTX
Анатолий Поляков - Drupal.ajax framework from a to z
PDF
Php tips-and-tricks4128
PDF
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
PDF
Desarrollo de módulos en Drupal e integración con dispositivos móviles
PDF
The Zen of Lithium
DrupalCamp Foz - Novas APIs Drupal 7
Separation of concerns - DPC12
Propel sfugmd
Let's write secure Drupal code!
50 Laravel Tricks in 50 Minutes
laravel tricks in 50minutes
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Keeping it Small: Getting to know the Slim Micro Framework
Drupal 7 Theming - Behind the scenes
Backbone.js Simple Tutorial
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
DRUPAL 8 STORAGES OVERVIEW
Lazy evaluation drupal camp moscow 2014
Metaprogramovanie #1
Drupal & javascript
Анатолий Поляков - Drupal.ajax framework from a to z
Php tips-and-tricks4128
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Desarrollo de módulos en Drupal e integración con dispositivos móviles
The Zen of Lithium

More from camp_drupal_ua (20)

ODP
Peter Lozovitsky.Money under your feet or why you have not already training i...
PDF
Alexandr Shvets.Trends in drupal.DrupalCamp Kyiv 2011
PDF
Dmitry Kostyuk.Cloud hosting for drupal.DrupalCamp Kyiv 2011
PDF
Vadim Mirgorod.Best practices for cross browser compatibility of drupal websi...
PDF
Sergey Korzh.CCK 3 presentation.DrupalCamp Kyiv 2011
PDF
Dennis Povshedny.Mediamosa intro.DrupalCamp Kyiv 2011
PDF
Artem Pankov.Eclipse.DrupalCamp Kyiv 2011
PDF
Vasily Yaremchuk.Single page website.DrupalCamp Kiev 2011
PDF
Anton Paschenko.Geolocation with Apach Solr using search API.DrupalCamp Kyiv ...
PDF
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
PDF
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
PDF
Pavel Prischepa.node load without-restrictions.DrupalCamp Kiev 2011
PDF
Anna Fedoruk.Theworkflow.DrupalCamp Kyiv 2011
PDF
Viktor Levandovsky.Why drupal.DrupalCamp Kyiv 2011
PDF
Petrov Olexandr.Varnish and drupal.DrupalCamp Kyiv 2011
PDF
Pankov Artem.Improving drupal performance www.hr portal.ru.DrupalCamp Kyiv 2011
PDF
Grigory naumovets.multilingual sites.drupal camp kyiv 2011
PDF
Ramil Kinzyabulatov.Organization online store.DrupalCamp Kyiv 2011
PDF
Ilivanov Alexander.How to make best theme.DrupalCamp Kyiv 2011
PDF
Roman Chernov.Panels custom layouts.DrupalCampKyiv 2011
Peter Lozovitsky.Money under your feet or why you have not already training i...
Alexandr Shvets.Trends in drupal.DrupalCamp Kyiv 2011
Dmitry Kostyuk.Cloud hosting for drupal.DrupalCamp Kyiv 2011
Vadim Mirgorod.Best practices for cross browser compatibility of drupal websi...
Sergey Korzh.CCK 3 presentation.DrupalCamp Kyiv 2011
Dennis Povshedny.Mediamosa intro.DrupalCamp Kyiv 2011
Artem Pankov.Eclipse.DrupalCamp Kyiv 2011
Vasily Yaremchuk.Single page website.DrupalCamp Kiev 2011
Anton Paschenko.Geolocation with Apach Solr using search API.DrupalCamp Kyiv ...
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Andriy Podanenko.Drupal database api.DrupalCamp Kyiv 2011
Pavel Prischepa.node load without-restrictions.DrupalCamp Kiev 2011
Anna Fedoruk.Theworkflow.DrupalCamp Kyiv 2011
Viktor Levandovsky.Why drupal.DrupalCamp Kyiv 2011
Petrov Olexandr.Varnish and drupal.DrupalCamp Kyiv 2011
Pankov Artem.Improving drupal performance www.hr portal.ru.DrupalCamp Kyiv 2011
Grigory naumovets.multilingual sites.drupal camp kyiv 2011
Ramil Kinzyabulatov.Organization online store.DrupalCamp Kyiv 2011
Ilivanov Alexander.How to make best theme.DrupalCamp Kyiv 2011
Roman Chernov.Panels custom layouts.DrupalCampKyiv 2011

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced IT Governance
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
Teaching material agriculture food technology
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
KodekX | Application Modernization Development
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced IT Governance
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Teaching material agriculture food technology
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
Advanced Soft Computing BINUS July 2025.pdf
NewMind AI Weekly Chronicles - August'25 Week I
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
KodekX | Application Modernization Development
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Review of recent advances in non-invasive hemoglobin estimation
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced methodologies resolving dimensionality complications for autism neur...

Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011

  • 1. Render API in Drupal7 Shmaleniuk Nikolay n.shmaleniuk@gmail.com
  • 4. 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
  • 5. Main hooks • hook_page_build add items to the page • hook_page_alter override the output of all page elements before output
  • 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 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
  • 12. 3 types of element 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. 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; }
  • 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 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
  • 17. 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;
  • 18. 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', );
  • 19. Attached recurses, JS settings $settings = array('id' => 'mymodule-element-1'); $form['#attached']['js'][] = array( 'data' => array('mymodule' => $settings), 'type' => 'setting', );
  • 20. 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');
  • 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 Arrays in Drupal http://drupal.org/node/930760 • Examples http://drupal.org/project/examples Shmaleniuk Nikolay n.shmaleniuk@gmail.com