Created
November 8, 2013 18:14
-
-
Save Sanchiz/7375191 to your computer and use it in GitHub Desktop.
Source code for blog article http://sanchiz.net/blog/block-with-form-configuration
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 | |
/** | |
* @file | |
* Example block | |
*/ | |
/** | |
* Implements hook_block_info(). | |
*/ | |
function example_block_block_info() { | |
$blocks['random_numbers'] = array( | |
'info' => t('Random numbers'), | |
); | |
return $blocks; | |
} | |
/** | |
* Implements hook_block_view(). | |
*/ | |
function example_block_block_view($delta) { | |
$block = array(); | |
switch ($delta) { | |
case 'random_numbers': | |
$block['subject'] = t('Random numbers'); | |
$block['content'] = example_block_random_numbers(); | |
break; | |
} | |
return $block; | |
} | |
/** | |
* Implements hook_block_configure(). | |
*/ | |
function example_block_block_configure($delta = '') { | |
$form = array(); | |
switch ($delta) { | |
case 'random_numbers': | |
$form['random_numbers_count'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Count numbers'), | |
'#default_value' => variable_get('random_numbers_count', 5), | |
'#element_validate' => array('element_validate_number'), | |
'#required' => TRUE, | |
); | |
break; | |
} | |
return $form; | |
} | |
/** | |
* Implements hook_block_save(). | |
*/ | |
function example_block_block_save($delta = '', $edit = array()) { | |
switch ($delta) { | |
case 'random_numbers': | |
variable_set('random_numbers_count', $edit['random_numbers_count']); | |
break; | |
} | |
} | |
/** | |
* Generate random numbers. | |
*/ | |
function example_block_random_numbers() { | |
$numbers = array(); | |
$count = variable_get('random_numbers_count', 5); | |
for ($i = 0; $i < $count; $i++) { | |
$numbers[] = rand(0, 1000); | |
} | |
return theme('item_list', array('items' => $numbers)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment