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 http://example.com/?q=admin/content/types/joke as long as the locked attribute defined in hook_node_info() is FALSE.

Make Money Writing

Make Money Writing

This Report Will Show You How To Make Money By Providing Writing Services To Other Internet Marketers. Learn how to make money by writing the right way. Grab your copy of this report now and learn. Why writing is a great way to earn money. How to compete with cheap writers, even if you charge a lot more money.

Get My Free Ebook


Post a comment