Reputation: 177
I need to add a programmatic form to a node in Drupal 7. How to attach the form to the node?
function addtabexample_form($node, &$form_state) {
$type = node_type_get_type($node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5,
);
$form['field1'] = array(
'#type' => 'textfield',
'#title' => t('Custom field'),
'#default_value' => $node->field1,
'#maxlength' => 127,
);
return $form;
}
Upvotes: 3
Views: 6915
Reputation: 27043
You can follow this code sample, using hook_node_view()
function [YOUR_MODULE]_node_view($node, $view_mode, $langcode)
{
$my_form = drupal_get_form('addtabexample_form', $node);
$node->content['my_form_attached'] = array(
'#markup' => drupal_render($my_form),
'#weight' => 10,
);
}
Upvotes: 6
Reputation: 4658
Your code has some problems that would require some rewriting... First, I'd suggest reading Form API Quickstart, which is a decent source to get the job done.
I'm not sure how you get the $node object to this. You have $node in the function parameters and $form as a return value...
See http://drupal.org/node/197122 for an example (I added the D7 part) of a form that could be embeded in a node. But doing that is ultra bad - you will face function redeclare problems, indexing problems, and a whole lot of troubles.
I know this is not an actual answer but I don't know how to write this in 500 chars.
Upvotes: 3