[英]How to create custom block in Drupal 7?
我想創建一個自定義塊,在其中顯示自定義表單。
表單將僅包含兩個元素。
#centimeter
) 單擊此提交按鈕后,我想將此厘米轉換為英寸,並在此表單下方顯示結果,並希望在側邊欄中以塊顯示。
我的問題:
/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!'));
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.