自定义模块表单问题

发布于 2024-12-14 18:40:29 字数 1178 浏览 3 评论 0原文

我正在努力尝试以编程方式显示要在节点页面视图区域中显示的表单。我的“简单模块”中有以下代码。

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
}

该代码可以工作,但只能通过我的管理菜单中定义的链接进行。

我想要做的是让表单在视图模式下在特定节点上显示并提交。这样就造成了访问节点时有一个表单需要填写的效果。

I am struggling in my attempts to programmatically display a form to display in my node-page view area. I have the following code in my "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
}

This code works but only from a defined link in my administration menu.

What I want to do is get the form to display and submit on a specific node in view mode. So it creates the effect that there is a form to fill when visiting the node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尤怨 2024-12-21 18:40:31

当然你可以使用

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

Surely you can use

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

£噩梦荏苒 2024-12-21 18:40:30

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

You can implement hook_nodeapi() and attach your form using 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,
    );
  }

}

You can use the #weight key to specify where in relation to the other content on the page your form will appear. Also you'll need to clear Drupal's caches when you implement this hook to make sure it's picked up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文