phpdrupaldrupal-7drupal-fapidrupal-forms

How to programmatically add a form to a node in Drupal 7?


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

Solution

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