Drupal 7 hook_node_view add a form to the content of a node

若如初见. 提交于 2019-12-03 13:10:16

The return from drupal_get_form is actually a render array itself so you could just do this:

$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.

$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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!