Implementation of hook_block()
I found this code online and I would like to implement it. However, I have never worked with hook functions.
My question is when I put this code into a brand new php file ex: uc_microcartTest.php . How do I call this new php file and get the results to show like this?
/**
* Implementation of hook_block().
*/
function uc_microcart_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Micro-sized cart block for page header.'),
// This block cannot be cached, because anonymous
// sessions can have differing cart contents.
// To improve this, see drupal.org/project/uc_ajax_cart
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
case 'view':
if ($item_count = uc_cart_get_total_qty()) {
$block = array();
$block['subject'] = '';
$block['content'] = theme('image',
drupal_get_path('module', 'uc_cart') .'/images/cart-full.png');
$block['content'] .= format_plural($item_count,
'My cart: 1 item', 'My cart: @count items');
$block['content'] = l($block['content'], 'cart', array('html' => TRUE));
return $block;
}
break;
}
}
Although it is possible to call the functions from external PHP files but I recommend you follow the "Drupal way":
- Create your own custom module
- Implement hook functions in
<your_module_name>.module
file. - Enable your module
You should also read more about how hooks
work in Drupal here