Created
April 6, 2017 18:21
-
-
Save MatthieuScarset/22701f05bd18939e775afcf96ed05621 to your computer and use it in GitHub Desktop.
Drupal 7 - Custom Block example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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