Drupal 7 hook_node_view adds form to node content

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 is the form not showing? Am I doing something wrong? The form object is populated. I can do #markup => "Something" and it works.

+6
source share
2 answers

The return from drupal_get_form is actually a rendering array, so you can just do this:

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

If you want to do it in another way, although the form should be a visualized β€œelement”, therefore the key should not be prefixed with # :

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

All records in the rendering array with the key with the prefix # are considered properties, and those that are not "children" are displayed recursively.

+7
source

Clive's answer in my case does not work. 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 right way.

+4
source

Source: https://habr.com/ru/post/900059/


All Articles