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

前端 未结 2 1498
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 14:33
function example_module_node_view($node, $view_mode, $langcode)
{   
    $f =  drupal_get_form(\'example_module_form\', $node);
    $node->content[\'data_collection_f         


        
2条回答
  •  無奈伤痛
    2021-02-15 15:20

    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.

提交回复
热议问题