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

发布于 2024-12-12 00:36:24 字数 311 浏览 0 评论 0原文

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); 
}

为什么表格不显示?我做错了什么吗?正在填充表单对象。我可以做#markup => “某事”并且它起作用了。

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.

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

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

发布评论

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

评论(2

无声情话 2024-12-19 00:36:24

drupal_get_form 的返回实际上是一个渲染数组本身,因此您可以这样做:

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

如果您确实想以其他方式执行此操作,尽管表单应该是可渲染的“元素”,所以键应该是“不应以 # 为前缀:

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

渲染数组中所有具有以 # 为前缀的键的条目都被视为属性,而那些不是的则被视为“子项”并递归呈现。

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.

沙与沫 2024-12-19 00:36:24

克莱夫的答案对我来说不起作用。我需要调用 drupal_render 并将其作为标记传递。

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

这可行,但我不确定这是否是正确的方法。

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.

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