Customizing the Node Form for Our Node Type

So far you've got the metadata defined for your new node type, and the menu callback and access permissions defined. Next you need to build the node form so that users can enter jokes.

You do that by implementing hook_form(): /**

function joke_form($node) {

// Get metadata for this node type

// (we use it for labeling title and body fields).

'#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5

$form['body_filter']['body'] = array( '#type' => 'textarea',

'#title' => check_plain($type->body_label), '#default_value' => $node->body, '#rows' => 7, '#required' => TRUE

$form['body_filter']['filter'] = filter_form($node->format);

$form['punchline'] = array( '#type' => 'textfield', '#title' => t('Punchline'), '#required' => TRUE, '#default_value' => $node->punchline, '#weight' => 5

return $form;

Note If you're unfamiliar with the Form API, see Chapter 10.

As the site administrator, you can now navigate to Create content >■ Joke and view the newly created form. The first line inside the preceding function returns the metadata information for this node type. node_get_types() will inspect $node->type to determine the type of node to return metadata for (in our case the value of $node->type will be joke). Again, the node metadata is set within hook_node_info(), and you set it earlier in joke_node_info().

The rest of the function contains three form fields to collect the title, body, and punchline. An important point here is how the #title keys of title and body are dynamic. Their values are inherited from hook_node_info(), but can also be changed by the site administrators at as long as the locked attribute defined in hook_node_info() is FALSE.

Article Marketing Gold

Article Marketing Gold

Discover How You Can Use The Huge Power of Article Marketing To Drive Highly Targeted Traffic To Your Website and Instantly Become THE Person Your Prospects Want To Buy From.

Get My Free Ebook

Post a comment