2010-04-19 55 views
2

我想 1)實現塊勾了勾菜單和variable_set並徵求並從用戶存儲的配置值, 2 )然後使用檢索配置值和 3)當頁面顯示時,使用主題鉤將它們傳遞到模板中。variable_set,鉤塊和鉤菜單保存配置值,然後打印出的自定義模板

但是,我需要在第二步和第三步中推動一下!

// ===================== 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; 
+0

你需要看的主題化部分。這些$ text1和$ text2變量是什麼?他們來自哪裏? Drupal自動完成很多事情,但它無法猜測你在想什麼。 (這個部分已經被主題函數處理了,它應該打印'hello'。) – lazysoundsystem 2010-04-19 12:20:35

+0

它更像是僞代碼。我想你知道Iam想要做什麼。 – bert 2010-04-20 02:19:00

回答

1

我調整了一下你的代碼,有一些評論。希望能幫助你。


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

更多參考:
http://api.drupal.org/api/function/variable_get/6
hook_theme(也在API)

+0

看起來很棒。我明天會測試它! – bert 2010-04-20 02:19:37