node_example_form

5 node_example.module node_example_form(&$node)
6 node_example.module 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.

File

developer/examples/node_example.module, line 91
This is an example outlining how a module can be used to define a new node type.

Code

function node_example_form(&$node) {
  $type = node_get_types('type', $node);

  // We need to define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => check_plain($type->title_label), 
    '#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' => check_plain($type->body_label), 
    '#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;
}

Comments

Bug in code

The last line in the body/filter element block should read:

<?php
$form
['body_filter']['format'] = filter_form($node->format);
?>

For some modules, such as WYSIWYG, it is important that the array key here is 'format' like it is in node_content_form().

Login or register to post comments