如何在 Drupal 7 中创建自定义块?
How to create custom block in Drupal 7?
我想创建一个自定义块,我想在其中显示自定义表单。
表单将只包含两个元素。
- 输入框(
#centimeter
)
- 提交按钮
单击此提交按钮后,我想将此厘米转换为英寸并在此表单下方显示结果,我想在侧边栏的块中显示此内容。
我的问题:
- 如何做到这一点?
- 我尝试遵循 link 但不知道在哪里编写代码 - 我应该在某个文件夹中创建任何 php 文件吗?
- 如果我想将此块显示为包含某些 url(例如
/measurement/convert-cm-to-inches
的页面,该怎么办
http://kahthong.com/2013/06/create-your-own-custom-drupal-block-programmatically
希望对您有所帮助。
<?php
/**
* Implements hook_block_view().
*/
function your_module_block_view($block_name = '')
{
// in my example I show the form only in the front page.
// You can show it where you want, obviously
if (!drupal_is_front_page())
{
return NULL;
}
$form = drupal_get_form('your_module_form');
$block = array
(
// 'subject' => t('Subject'),
'content' => $form,
);
return $block;
}
/**
* Implements hook_form().
*/
function your_module_form($form, &$form_state)
{
// now I add a text field to the form
// with a label and fixed dimensions (you never know...)
$form['text'] = array
(
'#title' => t('Label for the text box'),
'#type' => 'textfield',
'#size' => 32,
'#maxlength' => 128,
);
// now I add also a button
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Submit'),
);
// and now I assign a my function as handler of the submit event
$form['#submit'][] = 'your_module_submit_handler';
return $form;
}
function your_module_submit_handler($form, &$form_state)
{
// this function will be executed after the click
// event of the user on the "submit" button.
// here I only print a message
// you can access a database, redirect, or whatever you want, obviously
drupal_set_message(t('Ok!'));
}
?>
我想创建一个自定义块,我想在其中显示自定义表单。
表单将只包含两个元素。
- 输入框(
#centimeter
) - 提交按钮
单击此提交按钮后,我想将此厘米转换为英寸并在此表单下方显示结果,我想在侧边栏的块中显示此内容。
我的问题:
- 如何做到这一点?
- 我尝试遵循 link 但不知道在哪里编写代码 - 我应该在某个文件夹中创建任何 php 文件吗?
- 如果我想将此块显示为包含某些 url(例如
/measurement/convert-cm-to-inches
的页面,该怎么办
http://kahthong.com/2013/06/create-your-own-custom-drupal-block-programmatically
希望对您有所帮助。
<?php
/**
* Implements hook_block_view().
*/
function your_module_block_view($block_name = '')
{
// in my example I show the form only in the front page.
// You can show it where you want, obviously
if (!drupal_is_front_page())
{
return NULL;
}
$form = drupal_get_form('your_module_form');
$block = array
(
// 'subject' => t('Subject'),
'content' => $form,
);
return $block;
}
/**
* Implements hook_form().
*/
function your_module_form($form, &$form_state)
{
// now I add a text field to the form
// with a label and fixed dimensions (you never know...)
$form['text'] = array
(
'#title' => t('Label for the text box'),
'#type' => 'textfield',
'#size' => 32,
'#maxlength' => 128,
);
// now I add also a button
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Submit'),
);
// and now I assign a my function as handler of the submit event
$form['#submit'][] = 'your_module_submit_handler';
return $form;
}
function your_module_submit_handler($form, &$form_state)
{
// this function will be executed after the click
// event of the user on the "submit" button.
// here I only print a message
// you can access a database, redirect, or whatever you want, obviously
drupal_set_message(t('Ok!'));
}
?>