The Content Creation Form

Writerslife.org Website

Automatic Content Generation with Article Spinner

Get Instant Access

In Chapter 4, we created the Quote content type. Creating new content was easy. The form displayed in Create content | Quote was the same form used by Create content | Page and Create content | Story.

Now, however, we have a more complex content type. We need a form that will display all the biography fields: Biography of (stored as the document's title), Overview (stored in the document's body), Dates, Life, and Works.

To specify how the form for our new content type ought to look, we will implement hook_form(). Drupal will call this hook whenever a user creates a new biography.

In the last chapter, we looked at the Forms API (FAPI) in some detail. Here, we will make use of many of the same features. Since we have seen this API before, I will keep the explanation brief.

function biography_form(&$node) {

// Existing files: title (Biography of) and body (Overview) if ($type->has _title) { $form['title'] = array( '#type' => 'textfield',

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

$type->body_label, $type->min_word_count

// Our custom fields: Dates, Life, Works.

// (See FAPI docs for specification) $form['dates'] = array( '#type' => 'textfield', '#size' => 50, '#maxlengh' => 12 7, '#title' => t('Dates'),

'#description' => t('Birth and death dates.'), '#default_value' => isset($node->dates) ? $node->dates : '',

$form['life'] = array( '#type' => 'textarea', '#title' => t('Life'), '#cols' => 50, '#rows' => 15,

'#description' => t('A description of this person\'s life.'), '#default_value' => isset($node->life) ? $node->life : '',

$form['works'] = array( '#type' => 'textarea', '#title' => t('Works'), '#cols' => 50, '#rows' => 5,

'#description' => t('An annotated bibliography of this person\'s works.'),

'#default_value' => isset($node->works) ? $node->works : '',

return $form;

Our biography_form() hook implementation is passed one parameter. The $node variable (which is passed by reference) will contain the base node object for our new content. This node is initialized with default information.

Part of the job of this hook is to override existing defaults with defaults more appropriate for our content type.

Also, at this point Drupal doesn't have any biography-specific fields set. Therefore, the second purpose of this function will be to configure extra default data for a biography content type.

Let's start with the first.

Overriding hook_form() Defaults

Most of the contents of this function should look familiar. It is the first section, if anything, that should appear uncommon for a form definition. The following code is responsible for overriding existing defaults.

// Existing files: title (Biography of) and body (Overview) if ($type->has _title) { $form['title'] = array( '#type' => 'textfield',

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

$type->body_label, $type->min_word_count

The first thing this code does is retrieve information about the type of node that we are working with.

At first, this might seem silly. After all, we know that this node is a biography node. But the object returned by node_get_type() is richer than that. It returns an object that contains some important information about the configuration of the biography content type.

The $type object contains attributes matching those we created in biography_node_ info(). But the values of these fields might be different. While our biography_ hook_info() function sets the defaults for title_label, body_label, has_body and so on, an administrator can override these values.

Using node_get_type(), we can get the latest information about this content type's configuration—including any changes the administrator has made over and above our initial setup with biography_hook_info().

With this information, we can begin to define the first two fields of our form. If this form is to have a title (that is, if $type->has_title is true), then we define the title's text field:

if ($type->has _title) { $form['title'] = array( '#type' => 'textfield',

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

Note that we use $type->title_label to set the field's title. If an administrator has not made a change, the #title field should be set to Biography of.

A similar setup is used for the body (Overview) field. If $type->has_body is TRUE, then a form field is configured for the body, as well.

Drupal uses a sophisticated set of elements to display the body field—elements including a separator bar to delimit what part of the body is to be treated as the Summary, a text area field, and the Input format submenu. To conveniently create all of these fields, Drupal provides the node_body_field() function.

Generally speaking, the node_body_field() function should only be T

called to create the node's body field. It is not suitable for use to create I

additional custom fields. For more information on node_body_field(), I

see the Drupal API notes: http://api.drupal.org/api/function/ I

node_body_field/6. J

Now the title (Biography of) and body (overview) fields are correctly configured. Next, we need to add a few new fields to our form.

Adding New hook_form() Form Elements

As we saw earlier, the first purpose of the hook_form() hook was to give us a chance to override Drupal's defaults for our new content type.

The second purpose is to give us the opportunity to specify form elements for our content type.

Accomplishing this is simply a matter of using FAPI to define the desired form elements:

$form['dates'] = array( '#type' => 'textfield', '#size' => 50, '#maxlengh' => 12 7, '#title' => t('Dates'),

'#description' => t('Birth and death dates.'), '#default_value' => isset($node->dates) ? $node->dates : '',

$form['life'] = array( '#type' => 'textarea', '#title' => t('Life'), '#cols' => 50, '#rows' => 15,

'#description' => t('A description of this person\'s life.'), '#default_value' => isset($node->life) ? $node->life : '',

$form['works'] = array( '#type' => 'textarea', '#title' => t('Works'), '#cols' => 50, '#rows' => 5,

'#description' => t('An annotated bibliography of this person\'s works.'), '#default_value' => isset($node->works) ? $node->works : '',

return $form;

The form elements created above do not differ markedly from the forms we created in the last chapter. In a nutshell, this code creates three additional form fields:

1. A text field for entering a person's dates (Dates)

2. A text area for adding a biography (Life)

3. A text area for adding a bibliography or list of accomplishments (Works)

These three fields correlate to the three identically named columns we created in the bibliography database at the beginning of this chapter.

Once the form data structure is complete, we return it. When Drupal renders it, it will look something like the following screenshot:

Home Create content

Create Biography

Biography of: *

► Menu settings Overview:

Was this article helpful?

+1 0
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