模块开发:hook_form()、hook_menu()以及如何获取$_POST

发布于 2024-10-24 00:41:21 字数 1733 浏览 1 评论 0原文

我想提高我在模块开发方面的知识(离基础还很远),所以我尝试开发一个周边搜索模块。 我现在所实现的是一个包含表单的块:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2024-10-31 00:41:21

很简单,您必须为表单创建 _submit 函数,这里有一个示例:

function perimeter_search_form_submit($form, &$form_state) {
    /*
     * Your data handling goes here on the $form_state['values']['myfieldname']
     * variable.
     */
     drupal_set_message(t('Awesome, you managed to fill the form!'));
}

如果您需要验证..

function perimeter_search_form_validate($form, &$form_state) {
    if($form_state['values'['myfieldname'] == '') {
      form_set_error('', t('Hey, it doesn't work like that!'));
    }
}

请记住,如果您添加属性 '#required' =>对于表单字段为 TRUE,该字段将自动验证为始终需要该字段,因此如果您只需要编译该字段,则无需对该字段使用验证器。

Easy, you have to create the _submit function for your form, here an example:

function perimeter_search_form_submit($form, &$form_state) {
    /*
     * Your data handling goes here on the $form_state['values']['myfieldname']
     * variable.
     */
     drupal_set_message(t('Awesome, you managed to fill the form!'));
}

And if you need to validate..

function perimeter_search_form_validate($form, &$form_state) {
    if($form_state['values'['myfieldname'] == '') {
      form_set_error('', t('Hey, it doesn't work like that!'));
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文