在Drupal中创建一个非常简单的表单 [英] Create a VERY simple form in Drupal
问题描述
我需要做的只是一个表单:
All I need to do is have a form that does this:
- 用户在文本框中输入邮政编码
- 提交用户被重定向到mysite.com/[user postcode]
就是这样!我知道验证等也是可取的,但是我现在只需要做这个工作。我不介意如果它是编码或利用Drupal表单API(实际上我更喜欢前者!)。
That's it! I know validation etc. would be desirable as well, but I just need to get this working for now. I don't mind if it's harcoded or utilises the Drupal form API (actually I'd prefer the former!).
我知道这是死的简单,但不幸的是我来自前端的背景,并有一点了解这种事情:(
I know this is dead simple, but unfortunately I'm coming from a front-end background and have a bit to learn about this sort of thing :(
干杯!
推荐答案
表单API 很简单和自定义模块,您将使用Form API构建一个表单,并添加一个更改的提交处理程序最后,您需要创建一种访问表单的方法(通过创建菜单项或创建块)。
This is pretty easy with the Form API and a custom module. You'll build a form using the Form API and add a submit handler that changes the redirect for the form to whatever you'd like. Finally, you'll need to create a way to access the form (either by creating a menu item or by creating a block).
这是一个实现一个您想要的窗体的示例:您将要阅读Form API参考,以查看构建表单时所需的所有选项,还提供了两种访问方式形式:
Here's an example that implements a form like you want: you'll want to peruse the Form API reference to see all the options you have when building a form. It also provides two ways to access the form:
- 使用 为
http://example.com/test
- 使用
hook_block()
提供一个包含可以在块管理页面上添加和移动的表单的块。
- Using
hook_menu()
to provide a page for the form athttp://example.com/test
- Using
hook_block()
to provide a block containing the form that you can add and move around on the block administration page.
示例代码:
// Form builder. Form ID = function name
function test_form($form_state) {
$form['postcode'] = array(
'#type' => 'textfield',
'#title' => t('Postcode'),
'#size' => 10,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Go'),
);
return $form;
}
// Form submit handler. Default handler is formid_submit()
function test_form_submit($form, &$form_state) {
// Redirect the user to http://example.com/test/<Postcode> upon submit
$form_state['redirect'] = 'test/' . check_plain($form_state['values']['postcode']);
}
// Implementation of hook_menu(): used to create a page for the form
function test_menu() {
// Create a menu item for http://example.com/test that displays the form
$items['test'] = array(
'title' => 'Postcode form',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
// Implementation of hook_block(): used to create a movable block for the form
function test_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list': // Show block info on Site Building -> Blocks
$block['postcode']['info'] = t('Postcode form');
break;
case 'view':
switch ($delta) {
case 'postcode':
$block['subject'] = t('Postcode');
$block['content'] = drupal_get_form('test_form');
break;
}
break;
}
return $block;
}
更多信息:
- 创建模块 - 教程:Drupal 6.x
- Form API快速入门指南
- 表单API参考
-
hook_menu()
API参考 -
hook_block()
API参考 - Drupal 6中
hook_block
示例
- Creating modules - a tutorial: Drupal 6.x
- Form API Quickstart Guide
- Form API Reference
hook_menu()
API referencehook_block()
API reference- Example of
hook_block
in Drupal 6
这篇关于在Drupal中创建一个非常简单的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!