简体   繁体   English

如何以编程方式将表单添加到 Drupal 7 中的节点?

[英]How to programmatically add a form to a node in Drupal 7?

I need to add a programmatic form to a node in Drupal 7. How to attach the form to the node?我需要向 Drupal 7 中的节点添加一个编程表单。如何将表单附加到节点?

function addtabexample_form($node, &$form_state) {
  $type = node_type_get_type($node);

  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => check_plain($type->title_label), 
    '#default_value' => !empty($node->title) ? $node->title : '', 
    '#required' => TRUE, 
    '#weight' => -5,
  );

  $form['field1'] = array(
    '#type' => 'textfield', 
    '#title' => t('Custom field'), 
    '#default_value' => $node->field1, 
    '#maxlength' => 127,
 );


  return $form;
}

You can follow this code sample, using hook_node_view ()您可以按照此代码示例,使用hook_node_view ()

function [YOUR_MODULE]_node_view($node, $view_mode, $langcode)
{
    $my_form = drupal_get_form('addtabexample_form', $node);
    $node->content['my_form_attached'] = array(
        '#markup' => drupal_render($my_form),
        '#weight' => 10,
    );
}

Your code has some problems that would require some rewriting... First, I'd suggest reading Form API Quickstart , which is a decent source to get the job done.您的代码有一些需要重写的问题......首先,我建议阅读Form API Quickstart ,这是完成工作的一个不错的来源。

I'm not sure how you get the $node object to this.我不确定你是如何获得 $node 对象的。 You have $node in the function parameters and $form as a return value...你有 $node 在函数参数和 $form 作为返回值......

See http://drupal.org/node/197122 for an example (I added the D7 part) of a form that could be embeded in a node.有关可以嵌入节点的表单的示例(我添加了 D7 部分),请参见http://drupal.org/node/197122 But doing that is ultra bad - you will face function redeclare problems, indexing problems, and a whole lot of troubles.但是这样做是非常糟糕的——您将面临函数重新声明问题、索引问题和一大堆麻烦。

I know this is not an actual answer but I don't know how to write this in 500 chars.我知道这不是一个实际的答案,但我不知道如何用 500 个字符来写这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM