Using the_set, hook block and hook menu variables to save the config values, then print to your custom template
I am trying to 1) implement a hook menu and variable_set in a block grab and request and store the config values from the user, 2) then use retrieve the config values, and 3) pass them to the template using the hook theme when the page is displayed.
However, I need to press a little on step two and three!
// ===================== file: my_module.php
function my_module_block($op = 'list', $delta = 0, $edit = array())
{
switch($op)
{
case 'list':
$blocks[0] = array(
'info' => t('Find Something'), // required value - this shows up in your list of blocks
'region' => 'left', // default region on the page
'weight' => 0, // position the block vertically within its column.
'visibility' => 1, // permit the block to be displayed for a given user.
'status' => TRUE, // enabled
);
return $blocks;
break;
// case configure
case 'configure':
// not used ?
// case save (save configuration values)
case 'save':
variable_set('my_module_text_bottom', $edit['my_module_text_bottom']);
variable_set('my_module_text_top', $edit['my_module_text_top']);
break;
}
}
function my_module_menu(){
$items = array();
// add menu items
$items['my_module'] = array(
// add a menu item here...
);
// administration setting - callback my_module_admin
$items['admin/settings/my_module'] = array(
'title' => 'Lookup on Something',
'description' => 'Description of my module settings page',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
// setup administrative default values (see: site configiration)
function my_module_admin() {
$form = array();
$form['my_module_text_top'] = array(
'#type' => 'textarea',
'#title' => t('Text of top of page'),
'#default_value' => variable_get('my_module_text_top', 'my_module_text_top: This is configurable text found in the module configuration.'),
'#size' => 1024,
'#maxlength' => 1024,
'#description' => t("text above the Find a Retailer block."),
'#required' => TRUE,
);
$form['my_module_text_bottom'] = array(
'#type' => 'textarea',
'#title' => t('Text at bottom of page'),
'#default_value' => variable_get('my_module_text_bottom', 'my_module_text_bottom: This is configurable text found in the module configuration.'),
'#size' => 1024,
'#maxlength' => 1024,
'#description' => t("text below the Find a Retailer block."),
'#required' => TRUE,
);
return system_settings_form($form);
}
// registering a theme
function my_module_theme(){
return array(
'my_module_show' => array(
'arguments' => array('content' => "hello"),
'template' => 'my_module_show'
),
);
}
// implementing a theme
function theme_my_module_show($content){
$output = '<ul>$content</ul>';
return $output;
}
function my_module(){
$output = '';
$variables = "";
$output .= theme('my_module_show', $variables);
return $output;
}
// ===================== file: my_module_show.tpl.php
print $text1;
print $text2;
+2
a source to share
1 answer
I have adjusted your code a bit, with some comments in it. Hope it helps you.
function my_module_theme(){
return array(
'my_module_show' => array(
'arguments' => array('text1' => NULL, 'text' => NULL), // define all arguments, no values required, keys serve as labels in theme function/template
'template' => 'my_module_show'
),
);
}
function my_module(){
$output = '';
//use variable_get
$text1 = variable_get('my_module_text_top', '');
$text2 = variable_get('my_module_text_bottom', '');
$output .= theme('my_module_show', $text1, $text2); //pass values as arguments to theme functions
return $output;
}
More info:
http://api.drupal.org/api/function/variable_get/6
hook_theme (also in api)
+1
a source to share