drupaldrupal-blocksdrupal-hooks

Using variable_set, hook block and hook menu to save config values then print out in custom template


I am trying to 1) implement the hook menu and variable_set in the block hook and to solicit and store configuration values from user, 2) then use retrieve configuration values and 3) pass them out into a template using theme hook when page is shown.

However I need a bit of a push 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;

Solution

  • I adjusted your code a bit, with some comments in it. Hope that helps you out.

    
    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; 
    }
    

    For further reference :
    http://api.drupal.org/api/function/variable_get/6
    hook_theme (also in the api)