简体   繁体   English

Drupal 7 hook_node_view将一个表单添加到节点的内容中

[英]Drupal 7 hook_node_view add a form to the content of a node

function example_module_node_view($node, $view_mode, $langcode)
{   
    $f =  drupal_get_form('example_module_form', $node);
    $node->content['data_collection_form'] = array('#value' => $f, '#weight' => 1); 
}

Why doesn't the form display? 为什么表格不显示? Am I doing something wrong? 难道我做错了什么? The form object is being populated. 正在填充表单对象。 I can do #markup => 'Something' and it works. 我可以做#markup =>'Something'并且它有效。

The return from drupal_get_form is actually a render array itself so you could just do this: drupal_get_form的返回实际上是一个渲染数组本身,所以你可以这样做:

$f = drupal_get_form('example_module_form', $node);
$f['#weight'] = 1;
$node->content['data_collection_form'] = $f;

If you do want to do it the other way though the form should be a renderable 'element', so the key shouldn't be prefixed by # : 如果您确实希望以另一种方式执行此操作,则表单应该是可渲染的“元素”,因此键不应以#为前缀:

$f = drupal_get_form('example_module_form', $node);
$node->content['data_collection_form'] = array(0 => $f, '#weight' => 1);

All entries in a render array with a key prefixed with # are considered properties, while those that aren't are considered 'children' and are recursively rendered. 带有前缀为#的键的渲染数组中的所有条目都被视为属性,而不被视为属性的渲染数组中的所有条目都被视为“子级”并被递归渲染。

Clive answer doesn't work in my case. 克莱夫的回答在我的案例中不起作用。 I needed to call drupal_render and pass it as markup. 我需要调用drupal_render并将其作为标记传递。

$form = drupal_get_form('example_module_form', $node);
$node->content['data_collection_form'] = array(
  '#markup' => drupal_render($form),
  '#weight' => 10,
);

This work, but I'm not sure if this is the correct way. 这项工作,但我不确定这是否正确。

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

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