自定义模块表单问题


Custom module form problems

我正在努力尝试以编程方式显示表单以显示在我的节点页面视图区域。我在我的"simplemodule"中有以下代码:

function simplemodule_newcomer_form($form_state){
    $form   =   array();
    $form['simplemodule_newcomer']['name']  =   array(
        '#type'         =>  'textfield',
        '#title'        =>  t('name'),
        '#description'      =>  t(''),
        '#weight'       =>  -1,
    );
    $form['simplemodule_newcomer']['email'] =   array(
        '#title'        =>  t('email'),
        '#type'         =>  'textfield',
        '#description'      =>  t(''),
        '#weight'       =>  0,
    );
    $form['simplemodule_newcomer']['phone'] =   array(
        '#title'        =>  t('telephone No.'),
        '#type'         =>  'textfield',
        '#description'      =>  t(''),
        '#weight'       =>  0,
    );
    $form['submit_button'] =    array(
        '#type' =>  'submit',
        '#value'    =>  'enter',
    );
    return $form;
}
function simplemodule_newcomer_form_submit($form_id, &$form_state){
    //dealing with submitted data
}

这段代码只能在我的管理菜单中定义的链接中工作。

我想做的是让表单在视图模式下在特定节点上显示和提交。因此,它创建的效果是,当访问节点时,有一个表单要填写。

您可以实现hook_nodeapi()并使用drupal_get_form()附加表单:

function simplemodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($node->nid == $the_nid && $op == 'view') {
    $node->content['my_additional_field'] = array(
      '#value' => drupal_get_form('simplemodule_newcomer_form'), 
      '#weight' => 10,
    );
  }
}

您可以使用#weight键来指定与页面上其他内容相关的表单将出现的位置。此外,在实现这个钩子时,您还需要清除Drupal的缓存,以确保它被拾取。

你当然可以用

hook_form_alter(&$form, &$form_state, $form_id) and 
hook_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)
http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_alter/7

http://drupal.org/node/1011692