簡體   English   中英

如何在Drupal 7中創建自定義塊?

[英]How to create custom block in Drupal 7?

我想創建一個自定義塊,在其中顯示自定義表單。

表單將僅包含兩個元素。

  • 輸入框( #centimeter
  • 提交按鈕

單擊此提交按鈕后,我想將此厘米轉換為英寸,並在此表單下方顯示結果,並希望在側邊欄中以塊顯示。

我的問題:

  1. 這個怎么做?
  2. 我嘗試了以下鏈接,但不知道在哪里寫代碼-我應該在某個文件夾中創建任何php文件嗎?
  3. 如果我想將此塊顯示為帶有一些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!'));
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM