Skip to content

Instantly share code, notes, and snippets.

@gregmercer
Created August 10, 2012 20:03
Show Gist options
  • Save gregmercer/3317401 to your computer and use it in GitHub Desktop.
Save gregmercer/3317401 to your computer and use it in GitHub Desktop.
Drupal: creating a custom block
/**
* Implements hook_theme().
*
* Define custom theme hooks
*/
function <replace with module name>_theme($existing, $type , $theme, $path) {
return array(
'<replace with module name>_<block name>' => array(
'template' => 'templates/<replace with module name>-<block name>',
'file' => '<block name>.theme.inc',
),
);
}
/**
* Implementation of hook_block_info()
*
* Define custom blocks
*/
function <replace with module name>_block_info() {
$blocks['<machine name for block>'] = array(
'info' => t('<description of block>'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implementation of hook_block_view
*
* Define callbacks for block display
*/
function <replace with module name>_block_view($delta = '') {
$block = array();
switch ($delta) {
case '<machine name for block>':
$block['content'] = theme('<replace with module name>_<block name>');
break;
}
return $block;
}
...
in <block name>.theme.inc
function <replace with module name>_preprocess_<replace with module name>_<block name>(&$variables) {
$variables['date'] = date('Y');
}
...
in templates/<replace with module name>-<block name>.tpl.php
<p>Date is:<?php print $date; ?></p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment