模块开发:hook_form()、hook_menu()以及如何获取$_POST
我想提高我在模块开发方面的知识(离基础还很远),所以我尝试开发一个周边搜索模块。 我现在所实现的是一个包含表单的块:
function perimeter_search_block_view($delta = '') {
// Define an empty array for the block output.
$block = array();
switch($delta) {
case 'perimeter_search_box':
$block['subject'] = t('Perimeter search box');
$block['content'] = drupal_get_form('perimeter_search_form');;
break;
}
return $block;
}
/**
* Implementation of the perimeter search form
* @return array with form data
*/
function perimeter_search_form($form, &$form_state) {
$form = array(
'#action' => 'perimeter-search-results',
'keyword' => array(
'#type' => 'textfield'
),
'location' => array(
'#type' => 'textfield'
),
'perimeter' => array(
'#type' => 'select',
'#title' => t('Perimeter'),
'#options' => array('15 km', '30 km', '60 km', '120 km')
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Start search')
)
);
return $form;
}
我还有一个输出搜索结果的函数:
/**
* Implementation of hook_menu()
* @return defined menu/page items
*/
function perimeter_search_menu() {
$items = array();
// Search results page
$items['perimeter-search-results'] = array(
'title' => t('Perimeter search results'),
'page callback' => 'perimeter_search_results',
'access arguments' => array('view perimeter search'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Processing job search queries
*/
function perimeter_search_results() {
$page_content = t('Search results');
return $page_content;
}
我的(简单?)问题是:如何在我的 perimeter_search_results( ) 功能?
I want to improve my knowledge in module development (which is far away from basic), so I try to develop a perimeter search module.
What I've achieved for now is a block containing a form:
function perimeter_search_block_view($delta = '') {
// Define an empty array for the block output.
$block = array();
switch($delta) {
case 'perimeter_search_box':
$block['subject'] = t('Perimeter search box');
$block['content'] = drupal_get_form('perimeter_search_form');;
break;
}
return $block;
}
/**
* Implementation of the perimeter search form
* @return array with form data
*/
function perimeter_search_form($form, &$form_state) {
$form = array(
'#action' => 'perimeter-search-results',
'keyword' => array(
'#type' => 'textfield'
),
'location' => array(
'#type' => 'textfield'
),
'perimeter' => array(
'#type' => 'select',
'#title' => t('Perimeter'),
'#options' => array('15 km', '30 km', '60 km', '120 km')
),
'submit' => array(
'#type' => 'submit',
'#value' => t('Start search')
)
);
return $form;
}
I also have a function to output the search results:
/**
* Implementation of hook_menu()
* @return defined menu/page items
*/
function perimeter_search_menu() {
$items = array();
// Search results page
$items['perimeter-search-results'] = array(
'title' => t('Perimeter search results'),
'page callback' => 'perimeter_search_results',
'access arguments' => array('view perimeter search'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Processing job search queries
*/
function perimeter_search_results() {
$page_content = t('Search results');
return $page_content;
}
My (simple?) question is: how to get the post data (keyword, location, perimeter) in my perimeter_search_results() function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很简单,您必须为表单创建 _submit 函数,这里有一个示例:
如果您需要验证..
请记住,如果您添加属性 '#required' =>对于表单字段为 TRUE,该字段将自动验证为始终需要该字段,因此如果您只需要编译该字段,则无需对该字段使用验证器。
Easy, you have to create the _submit function for your form, here an example:
And if you need to validate..
Just remember that if you add the attribute '#required' => TRUE to a form field, the field will be automatically validated to always require that field, so you don't need to use the validator for that field if you just need that it get compiled.