【问题标题】:Custom module form problems自定义模块表单问题
【发布时间】:2011-11-08 17:26:17
【问题描述】:

我在尝试以编程方式显示要在我的节点页面视图区域中显示的表单时遇到了困难。我的“简单模块”中有以下代码。

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
}

此代码有效,但仅来自我的管理菜单中定义的链接。

我想要做的是让表单以视图模式在特定节点上显示和提交。这样就产生了访问节点时有表单要填写的效果。

【问题讨论】:

    标签: php forms drupal drupal-6 drupal-modules


    【解决方案1】:

    您可以实现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 的缓存以确保它被拾取。

    【讨论】:

    • 感谢您帮助新手 :-)
    【解决方案2】:

    你当然可以使用

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      相关资源
      最近更新 更多