執行hook_block()
,在您的表單中使用$form['#submit']
設置自定義提交處理程序,並在您的自定義提交處理程序中將$form_state['redirect']
設置爲您的自定義URL。例如:
function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
$block = array();
switch ($op) {
case 'list':
$block[0]['info'] = t('Custom search form');
break;
case 'view':
switch ($delta) {
case 0:
$block['subject'] = t('Custom search');
$block['content'] = drupal_get_form('mymodule_custom_search_form');
break;
}
break;
}
return $block;
}
function mymodule_custom_search_form($form_state) {
$form = array();
$form['keyword'] = array(
'#type' => 'textfield',
'#title' => t('Keyword'),
'#required' => TRUE,
);
$form['category'] = array(
'#type' => 'textfield',
'#title' => t('Categories'),
'#required' => TRUE,
);
$form['type'] = array(
'#type' => 'textfield',
'#title' => t('Type'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
$form['#submit'] = array('mymodule_custom_search_form_submit');
return $form;
}
function mymodule_custom_search_form_submit($form, &$form_state) {
$redirect_url = 'search/node/';
$redirect_url .= 'type:' . $form_state['values']['type'];
$redirect_url .= ' category:' . $form_state['values']['category'];
$redirect_url .= ' %' . $form_state['values']['keyword'] . '%';
$form_state['redirect'] = $redirect_url;
}
它工作得很好!但是,你可以再告訴我一件事。我有複選框。我如何獲得他們的類別? $形式[ '名稱'] [ '過濾器'] [ 'node_options'] =陣列( \t '#TYPE'=> '複選框', \t '#title'=> T( '默認選項'), \t '#default_value'=> variable_get( 'node_options',0), \t '#options'=>數組( \t \t '31'=> T( '中國'), \t \t '28'=>噸( '南印度'), \t \t '18'=>噸( '披薩'), \t \t \t ), \t'#description'=> t('管理節點權限的用戶將能夠覆蓋這些選項。 – esafwan 2010-07-30 11:26:56