Skip to content

Instantly share code, notes, and snippets.

@jonhattan
Last active December 11, 2015 05:58
Show Gist options
  • Save jonhattan/4555622 to your computer and use it in GitHub Desktop.
Save jonhattan/4555622 to your computer and use it in GitHub Desktop.
Definition and usage of a theme hook. Code provide two alternative options: using a template file or a theme function (commented out). It also implements a block to ilustrate the example.
<?php
/**
* Contents for file MODULE.module
*/
/**
* Implements hook_theme().
*
* Define here a theme hook to be implemented within MODULE.theme.inc as function theme_THEMEHOOK($vars) {}
*/
function MODULE_theme() {
$hooks = array();
$hooks['THEMEHOOK'] = array(
'variables' => array('param1' => NULL, 'param2' => NULL, 'items' => array()),
// You can implement the theme function in a separate file.
//'file' => 'MODULE.theme.inc',
// Instead of a theme function you can use a template file.
'template' => 'THEMEHOOK',
);
return $hooks;
}
/**
* Implements hook_block_info().
*/
function MODULE_block_info() {
$blocks = array();
$blocks['foo'] = array(
'info' => 'Lorem ipsum',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function MODULE_block_view($delta) {
$block = array();
switch ($delta) {
case 'foo':
$block['subject'] = 'Dolor sit amet';
$block['content'] = MODULE_block_content_foo();
break;
}
return $block;
}
/**
* Compute contents for block 'foo'.
*/
function MODULE_block_content_foo() {
$items = array();
// TODO: Obtain content for $items
// Return a build array to be rendered by our theme function.
$build = array(
'#theme' => 'THEMEHOOK',
'#param1' => 'bar',
'#param2' => 'baz',
'#items' => $items,
);
return $build;
}
<?php
/**
* This file is needed in case you are defining a theme function instead of a template file.
*/
/**
* Inmplements THEMEHOOK theme hook.
*/
function theme_THEMEHOOK($vars) {
$output = '';
// Do whatever you want with the data to render.
$vars['param1'];
$vars['param2'];
$vars['items'];
return $output;
}
<?php
/**
* Available variables:
*
* $param1
* $param2
* $items
*/
?>
<h3><?php print $param1;?></h3>
etc etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment