Skip to content

Instantly share code, notes, and snippets.

@micahredding
Created December 4, 2013 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahredding/7795335 to your computer and use it in GitHub Desktop.
Save micahredding/7795335 to your computer and use it in GitHub Desktop.
Block examples
# New Code, which includes configure and variable set
function psod_settings_block_info() {
$blocks = array();
$blocks['trainers'] = array(
'info' => 'Trainers',
'cache' => DRUPAL_NO_CACHE,
);
$blocks['employees'] = array(
'info' => 'Employees',
'cache' => DRUPAL_NO_CACHE,
);
$blocks['partners'] = array(
'info' => 'Partners',
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function psod_settings_block_view($delta = '') {
$block = array();
$block['subject'] = NULL;
$block['content'] = psod_settings_variable_get($delta, '');
return $block;
}
function psod_settings_block_configure($delta = '') {
$form = array();
$form['content'] = array(
'#type' => 'textarea',
'#title' => t('Text'),
'#description' => t('This text will appear in the block.'),
'#default_value' => psod_settings_variable_get($delta, ''),
);
return $form;
}
function psod_settings_block_save($delta = '', $edit = array()) {
$new_value = $edit['content'];
$current_value = psod_settings_variable_get($delta);
if($new_value == $current_value) {
// then do nothing
} else {
psod_settings_variable_set($delta, $new_value);
}
}
/* Helper Functions */
function psod_settings_variable_default($keys) {
$variable_name = psod_settings_variable_namer($keys);
$defaults = array(
'trainers' => 'sss',
'employees' => 'aaa',
'partners' => 'yyy',
);
if(isset($defaults[$variable_name])) {
return $defaults[$variable_name];
} else {
return '';
}
}
function psod_settings_variable_set($keys, $value) {
$variable_name = psod_settings_variable_name($keys);
variable_set($variable_name, $value);
}
function psod_settings_variable_get($keys, $default = '') {
$variable_name = psod_settings_variable_name($keys);
return variable_get($variable_name, $default);
}
function psod_settings_variable_name($keys) {
$prefix = 'psod_settings_';
return $prefix . psod_settings_variable_namer($keys);
}
function psod_settings_variable_namer($keys) {
if(!is_array($keys)) {
return $keys;
}
$variable_name = '';
foreach($keys as $index => $key) {
if($index > 0 ) {
$variable_name .= '_';
}
$variable_name .= $key;
}
return $variable_name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment