php - 如何在 drupal 模块中创建表单?

标签 php drupal drupal-7 drupal-modules drupal-fapi

我在网上搜索一个教程来创建一个显示在页面上的表单, 当我们使用模块和 block 显示内容时,我们应该在模块内显示一个表单吗? 由于我是 drupal 的新手,所以我对 drupal 形式一无所知。 我下载并安装了示例表单模块。但我不知道这个表格会在哪里显示。 我从这里下载 http://drupal.org/node/1121110

最佳答案

即使您是 drupal 新手,也没有那么复杂。在这个例子中我所要做的就是使用 hook_menu () 并知道来自 drupal form api reference 的可用表单项.

下面是您尝试执行的操作的示例。

/**
 * Implementation of hook_menu()
 */
function mymodule_menu()
{
    $items = array();

    $items['my-custom-page-path'] = array(
        'title'             => 'My Page Title',
        'description'       => t(''),
        'access callback'   => 'user_access',
        'access arguments'  => array('access content'),
        'page callback'     => 'drupal_get_form',
        'page arguments'    => array('mymodule_form_id'),
    );

    return $items;
}

function mymodule_form_id($form, &$form_state)
{
    $form = array();

    $form['my_textfield'] = array(
        '#type'         => 'textfield',
        '#title'        => t('Text Field'),
        '#description'  => t(''),
        '#weight'       => 20,
        '#required'     => TRUE,
        '#size'         => 5,
        '#maxlength'    => 5,
    );

    $form['submit'] = array(
        '#type'         => 'submit',
        '#value'        => t('Save settings'),
        '#weight'       => 10000,
    );

    return $form;
}

/**
* Form validation callback
*/
function mymodule_form_id_validate($form, &$form_state)
{
    // notice adding "_validate" to the form id
}

/**
* Form submission callback
*/
function mymodule_form_id_submit($form, &$form_state)
{
    // notice adding "_submit" to the form id
}

关于php - 如何在 drupal 模块中创建表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11591857/

相关文章:

php - 面向对象的 PHP : Loading data from DB to create objects?

PHP文件上传停止

php - 选择 mysql 表中的所有列但不显示重复结果,SELECT DISTINCT id

Drupal:不稳定的模块是否会引入即使在模块被删除后仍然存在的错误?

drupal - 如何在 Drupal 7 中使用代码创建 View ?

drupal - 如何从页面节点 Drupal 隐藏标题

javascript - PHP - 从完全加载的外部页面加载 HTML

Drupal View : Display recent nodes created by user on profile page

php - Drupal - 如何以编程方式更新 CCK NodeReference 字段?

drupal-7 - 我如何在我的模块drupal 7中包含一个tpl文件