Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.