|
<?php |
|
|
|
// HOW TO USE: |
|
// Create a MY_MODULE.info file. See https://www.drupal.org/node/542202. |
|
// Create a MY_MODULE.module file. |
|
// Put the codes below in MY_MODULE.module file |
|
|
|
// THE BIG PICTURE: |
|
// For a block to work in Drupal, you have to do a minimum of 3 things: |
|
// 1. Declare the block in hook_block_info(). |
|
// 2. Enable the declared block in admin/structure/block. |
|
// 3. Create a hook_block_view() so that Drupal will know how to display the block |
|
// when Drupal encounters it during a page load. |
|
|
|
/** |
|
* Implements hook_block_info(). |
|
* |
|
* Registers your block to Drupal. |
|
*/ |
|
function MY_MODULE_block_info() { |
|
$blocks = array(); |
|
|
|
// Make sure to enable this block after enabling the module. |
|
$blocks['my_custom_block'] = array( |
|
'info' => t('My block name in admin/structure/block page.'), |
|
); |
|
|
|
return $blocks; |
|
} |
|
|
|
|
|
/** |
|
* Implements hook_block_view(). |
|
* |
|
* Tells Drupal what to do when viewing the block. |
|
*/ |
|
function MY_MODULE_block_view($delta = '') { |
|
$block = array(); |
|
|
|
// $delta contains the IDs of the blocks declared inside |
|
// of this MY_MODULE file. |
|
// If you haven't declared other block here, |
|
// it will always be equal to 'my_custom_block'. |
|
switch ($delta) { |
|
// Check if this is your target block. |
|
case 'my_custom_block': |
|
// Put a title/label if you want. |
|
$block['subject'] = 'My Custom Block Title'; |
|
|
|
// Create a helper function for declring the contents of this block. |
|
$block['content'] = _my_custom_block_content(); |
|
|
|
break; |
|
} |
|
|
|
return $block; |
|
} |
|
|
|
|
|
/** |
|
* Helper function for better code maintainability. |
|
*/ |
|
function _my_custom_block_content() { |
|
// Get the URL to Drupal's default Druplicon image. |
|
$logo_path = file_create_url('misc/druplicon.png'); |
|
|
|
// Set the image properties. |
|
$image_variables = array( |
|
'path' => $logo_path, |
|
'title' => 'My Custom Logo', |
|
'alt' => 'My Custom Logo', |
|
'width' => '80%', |
|
'height' => '80%', |
|
'attributes' => array('class' => 'my-custom-logo'), |
|
); |
|
|
|
// Use the defualt theme function for image. |
|
// This will return a string HTML for image, given the properties. |
|
$output = theme('image', $image_variables); |
|
|
|
// Add a simple markup for image's caption/tagline. |
|
$output .= '<div><p>' . t('Drupal The Kraken!') . '</p></div>'; |
|
|
|
// Comment out the codes below if you want to use Drupal's Render API. |
|
// Render array is a bit longer but it is the recommended approach |
|
// since it allows alteration in other hooks. |
|
// |
|
/* |
|
$output = array(); |
|
|
|
// Render Array for the Image. |
|
$output['image_variables'] = array( |
|
'#theme' => 'image', |
|
'#path' => $logo_path, |
|
'#attributes' => array( |
|
'title' => 'My Custom Logo', |
|
'alt' => 'My Custom Logo', |
|
'width' => '80%', |
|
'height' => '80%', |
|
'class' => 'my-custom-logo', |
|
), |
|
); |
|
|
|
// Render Array for the text markup. |
|
// #prefix and suffix will enclose the contents of #markup. |
|
$output['tagline_variables'] = array( |
|
'#markup' => t('Drupal The Kraken!'), |
|
'#prefix' => '<div><p>', |
|
'#suffix' => '</p></div>', |
|
); |
|
*/ |
|
|
|
return $output; |
|
} |