node_example_form

  1. drupal
    1. 4.7
    2. 5
    3. 6 node_example.module
Versions
4.7 – 5 node_example_form(&$node)
6 node_example_form(&$node, $form_state)

Implementation of hook_form().

Now it's time to describe the form for collecting the information specific to this node type. This hook requires us to return an array with a sub array containing information for each element in the form.

Code

developer/examples/node_example.module, line 117

<?php
function node_example_form(&$node) {
  // We need to define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => t('Title'), 
    '#required' => TRUE, 
    '#default_value' => $node->title, 
    '#weight' => -5,
  );
  // We want the body and filter elements to be adjacent. We could try doing
  // this by setting their weights, but another module might add elements to the
  // form with the same weights and end up between ours. By putting them into a
  // sub-array together, we're able force them to be rendered together.
  $form['body_filter']['body'] = array(
    '#type' => 'textarea', 
    '#title' => t('Body'), 
    '#default_value' => $node->body, 
    '#required' => FALSE,
  );
  $form['body_filter']['filter'] = filter_form($node->format);

  // Now we define the form elements specific to our node type.
  $form['color'] = array(
    '#type' => 'textfield', 
    '#title' => t('Color'), 
    '#default_value' => $node->color,
  );
  $form['quantity'] = array(
    '#type' => 'textfield', 
    '#title' => t('Quantity'), 
    '#default_value' => $node->quantity, 
    '#size' => 10, 
    '#maxlength' => 10,
  );

  return $form;
}
?>