Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 7 - Custom Block example
<?php
/**
* Implements hook_block_info().
*/
function custom_block_block_info() {
$blocks = array();
$blocks['states_map'] = array(
'info' => t('States map'),
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function custom_block_block_configure($delta='') {
$form = array();
switch($delta) {
case 'custom_block' :
// Text field form element
$form['text_body'] = array(
'#type' => 'text_format',
'#title' => t('Enter your text here in WYSIWYG format'),
'#default_value' => variable_get('text_variable', ''),
);
break;
}
return $form;
}
/**
* Implements hook_block_save().
*/
function custom_block_block_save($delta = '', $edit = array()) {
switch($delta) {
case 'custom_block' :
// Saving the WYSIWYG text
variable_set('text_variable', $edit['text_body']['value']);
break;
}
}
/**
* Implements hook_block_view().
*/
function custom_block_block_view($delta='') {
$block = array();
switch($delta) {
case 'custom_block' :
$block['content'] = _custom_block_block_view();
break;
}
return $block;
}
/**
* Custom function to assemble renderable array for block content.
* Returns a renderable array with the block content.
* @return
* returns a renderable array of block content.
*/
function _custom_block_block_view() {
$block = array();
// Capture WYSIWYG text from the variable
$text = variable_get('text_variable', '');
// Block output in HTML with div wrapper
$block = array(
'image' => array(
'#prefix' => '',
'#type' => 'markup',
'#markup' => $image,
),
'message' => array(
'#type' => 'markup',
'#markup' => $text,
'#suffix' => '',
),
);
return $block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.