7 to 8: transitioning from theme() and theme functions to...

106
7 to 8: Transitioning From theme() and Theme Functions to Render Arrays and Templates

Upload: others

Post on 12-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

7 to 8: Transitioning From theme() and Theme Functions to Render Arrays and Templates

Page 2: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

Gus Childs @guschilds

Page 3: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

chromaticsites.com

Page 4: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

chromaticsites.com

Page 5: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

front-end back-end

Page 6: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

front-end this session back-end

Page 7: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions
Page 8: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<h1> </h1>

Page 9: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

render arrays > theme()

Page 10: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

templates > theme functions

Page 11: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

render arrays > theme() templates > theme functions

Page 12: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

render arrays > theme()

Page 13: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions
Page 14: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$variables['image'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), ));

Page 15: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7. <?php print $image; ?>

Page 16: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<img src="logo.png" alt="My logo!" />

Page 17: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$variables['image'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

Page 18: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7. <?php print $image; ?>

Page 19: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7. <?php print render($image); ?>

Page 20: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$variables['image'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

Page 21: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<img src="logo.png" alt="My logo!" />

Page 22: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 8. {{ image }}

Page 23: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$variables['header'] = array( 'title' => array(...), 'slogan' => array(...), 'logo' => array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), ), );

Page 24: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$variables['header'] = array( 'title' => array(...), 'slogan' => array(...), 'logo' => array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), ), );

Page 25: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// From page.tpl.php: <?php print render($page['sidebar']); ?>

// From node.tpl.php: <?php print render($content); ?>

Page 26: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

render arrays > theme()

Page 27: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

templates > theme functions

Page 28: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Theme functions? // @see drupal.org/node/2575081 theme_image() theme_item_list() theme_link()

Page 29: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions
Page 30: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions
Page 31: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions
Page 32: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Uses a render array. $variables['image'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

// Uses the theme() function. $variables['image'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), ));

Page 33: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Uses a render array. $variables['image'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

// Uses the theme() function. $variables['image'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), ));

Page 34: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Theme function. theme_image()

// Template. image.tpl.php

Page 35: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Theme function. theme_image()

// Template. // image.tpl.php

Page 36: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions
Page 37: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_image($variables) { $attributes = $variables['attributes']; $attributes['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

core’s theme.inc

Page 38: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Never do this! $image = theme_image(array( 'path' => 'logo.png', 'alt' => t('My logo!'), ));

Page 39: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function my_theme_image($variables) { $attributes = $variables['attributes']; $attributes['data-src'] = file_create_url($variables['path']); $attributes['class'][] = 'lazyload';

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

my_theme’s template.php

Page 40: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Better, but still :(. $image = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), ));

Page 41: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_image($variables) { $attributes = $variables['attributes']; $attributes['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

core’s theme.inc

Page 42: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_item_list($variables) { $items = $variables['items']; $title = $variables['title']; $type = $variables['type']; $attributes = $variables['attributes'];

// Only output the list container and title, if there are any list items. // Check to see whether the block title exists before adding a header. // Empty headers are not semantic and present accessibility challenges. $output = '<div class="item-list">'; if (isset($title) && $title !== '') { $output .= '<h3>' . $title . '</h3>'; }

if (!empty($items)) { $output .= "<$type" . drupal_attributes($attributes) . '>'; $num_items = count($items); $i = 0; foreach ($items as $item) { $attributes = array(); $children = array(); $data = ''; $i++; if (is_array($item)) { foreach ($item as $key => $value) { if ($key == 'data') { $data = $value; } elseif ($key == 'children') { $children = $value; } else { $attributes[$key] = $value; } } } else { $data = $item; } if (count($children) > 0) { // Render nested list. $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes)); } if ($i == 1) { $attributes['class'][] = 'first'; } if ($i == $num_items) { $attributes['class'][] = 'last'; } $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n"; } $output .= "</$type>"; } $output .= '</div>'; return $output; }

core’s theme.inc

Page 43: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$output = '<div class="item-list">';

Page 44: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

core’s theme.incfunction theme_item_list($variables) { $items = $variables['items']; $title = $variables['title']; $type = $variables['type']; $attributes = $variables['attributes'];

// Only output the list container and title, if there are any list items. // Check to see whether the block title exists before adding a header. // Empty headers are not semantic and present accessibility challenges. $output = '<div class="item-list">'; if (isset($title) && $title !== '') { $output .= '<h3>' . $title . '</h3>'; }

if (!empty($items)) { $output .= "<$type" . drupal_attributes($attributes) . '>'; $num_items = count($items); $i = 0; foreach ($items as $item) { $attributes = array(); $children = array(); $data = ''; $i++; if (is_array($item)) { foreach ($item as $key => $value) { if ($key == 'data') { $data = $value; } elseif ($key == 'children') { $children = $value; } else { $attributes[$key] = $value; } } } else { $data = $item; } if (count($children) > 0) { // Render nested list. $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes)); } if ($i == 1) { $attributes['class'][] = 'first'; } if ($i == $num_items) { $attributes['class'][] = 'last'; } $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n"; } $output .= "</$type>"; } $output .= '</div>'; return $output; }

Page 45: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Theme function. theme_image()

// Template. image.tpl.php

Page 46: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Theme function. // theme_image()

// Template. image.tpl.php

Page 47: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Disclaimer: this is just an example. // Core already does this, but in a different // way, and you won’t need to in order to // render an image.

Page 48: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_module.module/** * Implements hook_theme(). */ function my_module_theme() { return array( 'image' => array( 'template' => 'image', 'variables' => array( 'path' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => NULL, 'title' => NULL, 'attributes_array' => array(), 'attributes' => NULL, ), ), ); }

Page 49: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_module.module/** * Implements hook_theme(). */ function my_module_theme() { return array( 'image' => array( 'template' => 'image', 'variables' => array( 'path' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => NULL, 'title' => NULL, 'attributes_array' => array(), 'attributes' => NULL, ), ), ); }

Page 50: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_image($variables) { $attributes = $variables['attributes']; $attributes['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

core’s theme.inc

Page 51: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_image($variables) { $attributes = $variables['attributes']; $attributes['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

core’s theme.inc

Page 52: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7's theoretical image.tpl.php. <img<?php print $attributes; ?> />

// Drupal 8's theoretical image.html.twig. <img{{ attributes }} />

Page 53: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_image($variables) { $attributes = $variables['attributes']; $attributes['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

core’s theme.inc

Page 54: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for image. */ function my_module_preprocess_image(&$variables) { $variables['attributes_array']['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { $variables['attributes_array'][$key] = $variables[$key]; } } }

my_module.module

Page 55: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_module.module

/** * Implements hook_process_HOOK() for image. */ function my_module_process_image(&$variables) { $variables['attributes'] = drupal_attributes($variables['attributes_array']); }

Page 56: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_module.module/** * Implements hook_preprocess_HOOK() for image. */ function my_module_preprocess_image(&$variables) { $variables['attributes_array']['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { $variables['attributes_array'][$key] = $variables[$key]; } } }

/** * Implements hook_process_HOOK() for image. */ function my_module_process_image(&$variables) { $variables['attributes'] = drupal_attributes($variables['attributes_array']); }

Page 57: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function my_theme_image($variables) { $attributes = $variables['attributes']; $attributes['data-src'] = file_create_url($variables['path']); $attributes['class'][] = 'lazyload';

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

my_theme’s template.php

Page 58: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for image. */ function my_theme_preprocess_image(&$variables) { $variables['attributes_array']['class'][] = 'lazyload'; $variables['attributes_array']['data-src'] = $variables['attributes_array']['src']; unset($variables['attributes_array']['src']); }

my_theme’s template.php

Page 59: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for image. */ function my_module_preprocess_image(&$variables) { $variables['attributes_array']['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { $variables['attributes_array'][$key] = $variables[$key]; } } }

/** * Implements hook_preprocess_HOOK() for image. */ function my_theme_preprocess_image(&$variables) { $variables['attributes_array']['class'][] = 'lazyload'; $variables['attributes_array']['data-src'] = $variables['attributes_array']['src']; unset($variables['attributes_array']['src']); }

/** * Implements hook_process_HOOK() for image. */ function my_module_process_image(&$variables) { $variables['attributes'] = drupal_attributes($variables['attributes_array']); }

Page 60: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for image. */ function my_module_preprocess_image(&$variables) { $variables['attributes_array']['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { $variables['attributes_array'][$key] = $variables[$key]; } } }

/** * Implements hook_preprocess_HOOK() for image. */ function my_theme_preprocess_image(&$variables) { $variables['attributes_array']['class'][] = 'lazyload'; $variables['attributes_array']['data-src'] = $variables['attributes_array']['src']; unset($variables['attributes_array']['src']); }

/** * Implements hook_process_HOOK() for image. */ function my_module_process_image(&$variables) { $variables['attributes'] = drupal_attributes($variables['attributes_array']); }

Page 61: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

$variables['image'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

Page 62: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7. <?php print render($image); ?>

// Drupal 8. {{ image }}

Page 63: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Without the lazyload preprocessing. <img src="logo.png" alt="My logo!" />

// With the lazyload preprocessing. <img data-src="logo.png" class="lazyload" alt="My logo!" />

Page 64: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7's theoretical image.tpl.php. <img<?php print $attributes; ?> />

// Drupal 8's theoretical image.html.twig. <img{{ attributes }} />

Page 65: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

core’s theme.incfunction theme_item_list($variables) { $items = $variables['items']; $title = $variables['title']; $type = $variables['type']; $attributes = $variables['attributes'];

// Only output the list container and title, if there are any list items. // Check to see whether the block title exists before adding a header. // Empty headers are not semantic and present accessibility challenges. $output = '<div class="item-list">'; if (isset($title) && $title !== '') { $output .= '<h3>' . $title . '</h3>'; }

if (!empty($items)) { $output .= "<$type" . drupal_attributes($attributes) . '>'; $num_items = count($items); $i = 0; foreach ($items as $item) { $attributes = array(); $children = array(); $data = ''; $i++; if (is_array($item)) { foreach ($item as $key => $value) { if ($key == 'data') { $data = $value; } elseif ($key == 'children') { $children = $value; } else { $attributes[$key] = $value; } } } else { $data = $item; } if (count($children) > 0) { // Render nested list. $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes)); } if ($i == 1) { $attributes['class'][] = 'first'; } if ($i == $num_items) { $attributes['class'][] = 'last'; } $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n"; } $output .= "</$type>"; } $output .= '</div>'; return $output; }

Page 66: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

templates > theme functions

Page 67: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

render arrays > theme()

Page 68: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Example: You’re building a module for a // multi-site installation that renders the // logo in a particular way. This logo will // be printed in each site’s page template // and may be altered by each theme.

Page 69: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); ?> </header>

page.tpl.php

Page 70: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function cool_logo_preprocess_page(&$variables) { $variables['logo'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); }

cool_logo.module

Page 71: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print $logo; ?> </header>

page.tpl.php

Page 72: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

cool_logo.module

/** * Implements hook_preprocess_HOOK() for page. */ function cool_logo_preprocess_page(&$variables) { $variables['logo'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); }

Page 73: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function my_theme_preprocess_page(&$variables) { dpm($variables['logo']); }

my_theme’s template.php

Page 74: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<img src="logo.png" alt="My logo!" />

Page 75: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function my_theme_preprocess_page(&$variables) { $find = 'My logo!'; $replace = t('Woah! Logo!'); $logo = str_replace($find, $replace, $variables['logo']); $variables['logo'] = $logo; }

my_theme’s template.php

Page 76: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

cool_logo.module

/** * Implements hook_preprocess_HOOK() for page. */ function cool_logo_preprocess_page(&$variables) { $variables['logo'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); }

Page 77: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function cool_logo_preprocess_page(&$variables) { $variables['logo'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), ); }

cool_logo.module

Page 78: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_theme’s template.php

/** * Implements hook_preprocess_HOOK() for page. */ function my_theme_preprocess_page(&$variables) { dpm($variables['logo']); }

Page 79: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

Page 80: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function my_theme_preprocess_page(&$variables) { // Change the alt text of the logo. $variables['logo']['#alt'] = t('Woah! Logo!'); }

my_theme’s template.php

Page 81: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print render($logo); ?> </header>

page.tpl.php

Page 82: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print render($logo); ?> </header>

overridden page.tpl.php

Page 83: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print render($logo); ?> </header>

page.tpl.php

Page 84: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1>{{ title }}</h1> {{ logo }} </header>

page.html.twig

Page 85: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

woah. huh?

Page 86: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Uses a render array. $variables['image'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), );

// Uses the theme() function. $variables['image'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), ));

Page 87: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Theme function. theme_image()

// Template. image.tpl.php

Page 88: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_module.module/** * Implements hook_theme(). */ function my_module_theme() { return array( 'image' => array( 'template' => 'image', 'variables' => array( 'path' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => NULL, 'title' => NULL, 'attributes_array' => array(), 'attributes' => NULL, ), ), ); }

Page 89: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function theme_image($variables) { $attributes = $variables['attributes']; $attributes['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

core’s theme.inc

Page 90: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

core’s theme.incfunction theme_item_list($variables) { $items = $variables['items']; $title = $variables['title']; $type = $variables['type']; $attributes = $variables['attributes'];

// Only output the list container and title, if there are any list items. // Check to see whether the block title exists before adding a header. // Empty headers are not semantic and present accessibility challenges. $output = '<div class="item-list">'; if (isset($title) && $title !== '') { $output .= '<h3>' . $title . '</h3>'; }

if (!empty($items)) { $output .= "<$type" . drupal_attributes($attributes) . '>'; $num_items = count($items); $i = 0; foreach ($items as $item) { $attributes = array(); $children = array(); $data = ''; $i++; if (is_array($item)) { foreach ($item as $key => $value) { if ($key == 'data') { $data = $value; } elseif ($key == 'children') { $children = $value; } else { $attributes[$key] = $value; } } } else { $data = $item; } if (count($children) > 0) { // Render nested list. $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes)); } if ($i == 1) { $attributes['class'][] = 'first'; } if ($i == $num_items) { $attributes['class'][] = 'last'; } $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n"; } $output .= "</$type>"; } $output .= '</div>'; return $output; }

Page 91: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

// Drupal 7's theoretical image.tpl.php. <img<?php print $attributes; ?> />

// Drupal 8's theoretical image.html.twig. <img{{ attributes }} />

Page 92: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

my_module.module/** * Implements hook_preprocess_HOOK() for image. */ function my_module_preprocess_image(&$variables) { $variables['attributes_array']['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { $variables['attributes_array'][$key] = $variables[$key]; } } }

/** * Implements hook_process_HOOK() for image. */ function my_module_process_image(&$variables) { $variables['attributes'] = drupal_attributes($variables['attributes_array']); }

Page 93: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

function my_theme_image($variables) { $attributes = $variables['attributes']; $attributes['data-src'] = file_create_url($variables['path']); $attributes['class'][] = 'lazyload';

foreach (array('width', 'height', 'alt', 'title') as $key) {

if (isset($variables[$key])) { $attributes[$key] = $variables[$key]; } }

return '<img' . drupal_attributes($attributes) . ' />'; }

my_theme’s template.php

Page 94: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for image. */ function my_theme_preprocess_image(&$variables) { $variables['attributes_array']['class'][] = 'lazyload'; $variables['attributes_array']['data-src'] = $variables['attributes_array']['src']; unset($variables['attributes_array']['src']); }

my_theme’s template.php

Page 95: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for image. */ function my_module_preprocess_image(&$variables) { $variables['attributes_array']['src'] = file_create_url($variables['path']);

foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { $variables['attributes_array'][$key] = $variables[$key]; } } }

/** * Implements hook_preprocess_HOOK() for image. */ function my_theme_preprocess_image(&$variables) { $variables['attributes_array']['class'][] = 'lazyload'; $variables['attributes_array']['data-src'] = $variables['attributes_array']['src']; unset($variables['attributes_array']['src']); }

/** * Implements hook_process_HOOK() for image. */ function my_module_process_image(&$variables) { $variables['attributes'] = drupal_attributes($variables['attributes_array']); }

Page 96: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); ?> </header>

page.tpl.php

Page 97: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

cool_logo.module

/** * Implements hook_preprocess_HOOK() for page. */ function cool_logo_preprocess_page(&$variables) { $variables['logo'] = theme('image', array( 'path' => 'logo.png', 'alt' => t('My logo!'), )); }

Page 98: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function my_theme_preprocess_page(&$variables) { $find = 'My logo!'; $replace = t('Woah! Logo!'); $logo = str_replace($find, $replace, $variables['logo']); $variables['logo'] = $logo; }

my_theme’s template.php

Page 99: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function cool_logo_preprocess_page(&$variables) { $variables['logo'] = array( '#theme' => 'image', '#path' => 'logo.png', '#alt' => t('My logo!'), ); }

cool_logo.module

Page 100: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

/** * Implements hook_preprocess_HOOK() for page. */ function my_theme_preprocess_page(&$variables) { // Change the alt text of the logo. $variables['logo']['#alt'] = t('Woah! Logo!'); }

my_theme’s template.php

Page 101: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<header> <h1><?php print $title; ?></h1> <?php print render($logo); ?> </header>

page.tpl.php

Page 102: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

<img src="logo.png" alt="My logo!" />

Page 103: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

render arrays > theme() templates > theme functions

Page 104: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

further reading Render Arrays in Drupal 7. drupal.org/node/930760

Twig best practices - preprocess functions, templates, and render arrays.

drupal.org/node/1920746

Page 105: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

thanks!

Page 106: 7 to 8: Transitioning From theme() and Theme Functions to ...blog-media.chromaticsites.com.s3.amazonaws.com/badcamp-2015/… · 7 to 8: Transitioning From theme() and Theme Functions

questions?