Learn how to change an existing Drupal form

Changing an existing form is one of the first things that new Drupal developers will learn how to do. It is a natural extension from site building, where you might need to change one thing on a form created by a core or contributed module. One of the benefits of using the Form API to construct forms is that any module can alter any other modules form. In this tutorial, you are going to learn how to alter a Drupal form.

Add message to Create Article form

Go to Add Content -> Article. This is the form that you are going to alter.

To alter a form, you need to call hook_form_alter(). Add an empty implementation of hook_form_alter(), like so:

  
  1. /**
  2.  * Implements hook_form_alter().
  3.  */
  4. function starting_drupal_dev_form_alter(&$form, &$form_state, $form_id) {
  5.  
  6. }
  7.  

To alter a form, you need to know its ID. There are two easy ways to find a form’s ID.

1. View source

The form ID will be the HTML ID in the form element. Here you can see that the form ID is article-node-form.

Form ID in source code

2. Print all Form IDs in form alter

Inside the hook_form_alter(), print a message to display the form_id.

  
  1. function starting_drupal_dev_form_alter(&$form, &$form_state, $form_id) {
  2. dpm($form_id);
  3. }
  4.  

You will need to clear cache to see the dpm().

$ drush cc all

Form ID in Drupal Message

So the form ID is article_node_form. If you are really observant, you will have noticed that when viewing the source code, the form ID has hyphens instead of underscores. It should always be with underscores, so if you use the view source method, you will need to convert the hyphens to underscores.

We are going to change all the code inside the form alter function.

  
  1. function starting_drupal_dev_form_alter(&$form, &$form_state, $form_id) {
  2. if ($form_id == 'article_node_form') {
  3. $form['field_message'] = array(
  4. '#type' => 'item',
  5. '#title' => t('New article'),
  6. '#markup' => t('You are about to create a new article'),
  7. );
  8. }
  9. }
  10.  

First, you added a condition to check that the form ID is article_node_form. Then you added a new form element, called field_message. This contains a message and is not a normal field like a textfield. You use the type item for this.

From api.drupal.org:

Item
Generate a display-only form element allowing for an optional title and description.

If you reload the form, you should see the message. However, it is displayed below the tag, title, body and image
fields. Not exactly what we want. To fix this, you need to add a weight to the field and weight it above the other fields.

You need to find out the weight of the existing fields. Let’s add the dpm($form) back into and inspect the relevant fields.

  
  1. function starting_drupal_dev_form_alter(&$form, &$form_state, $form_id) {
  2. if ($form_id == 'article_node_form') {
  3. $form['field_message'] = array(
  4. '#type' => 'item',
  5. '#title' => t('New article'),
  6. '#markup' => t('You are about to create a new article'),
  7. );
  8. dpm($form);
  9. }
  10. }
  11.  

Expand the title field, as that is the first field on the rendered form.

Field weight

As you can see, the weight is −5.

If you add a weight that is higher than −5, the new field will be positioned above the title field.

  
  1. function starting_drupal_dev_form_alter(&$form, &$form_state, $form_id) {
  2. if ($form_id == 'article_node_form') {
  3. $form['field_message'] = array(
  4. '#type' => 'item',
  5. '#title' => t('New article'),
  6. '#markup' => t('You are about to create a new article'),
  7. '#weight' => -6,
  8. );
  9. }
  10. }
  11.  

And voila! The message field is now at the top of the form.

New message field

This tutorial is part of the Starting Drupal Development course. If you would like to receive all of the lessons, please add your details in the box below.

To scale the Drupal development learning curve faster, check out my book Master Drupal Development. It will help you master module development faster to become a fully fledged Drupal developer.

Comments

Very good article

Very nice tutorial, this made it very clear.

Hi, good post. I also want to notice that post images are not showing. Regards

Blair Wadman's picture

Thanks for point that out. Will get that sorted asap.

Thank you for the article. Is there any way to change look and feel of forms on the whole site? I am using D7

Blair Wadman's picture

The easiest way would be to target the form element in CSS. E.g:

  1. form {
  2. /* your styles */
  3. }

Or you could target specific types of forms. E,g, to change the look and feel of comment forms:

  1. form.comment-form {
  2. /* your styles */
  3. }

Does that help?

Add new comment