Drupal - How to modify the node/page title for a custom content type

Drupal content type FAQ: How do I modify the node title for the "create" form when I add a new content type?

For my How I Sold My Business website, I added a new, custom content type named "Charity". This allows people who have bought my book to suggest a charity. The problem with this is that I want the Charity node title (also referred to as a "page title") to say "Suggest a Charity", not the default "Create Charity".

I haven't found a Drupal module to let you change this page/node title, so I wrote a little module to do it myself. All I had to do was implement the Drupal hook_form_alter function:

/**
 * Implementation of hook_form_alter().
 * Modify the node title for my New Charity form.
 */
function als_hacks_form_charity_node_form_alter(&$form, &$form_state) {

  drupal_set_title('Suggest a Charity');

}

As you can see, all you need to do is create an "alter" function with the right name, then call the drupal_set_title function to change the title to whatever you want it to be. In my case this changes the title on my Create Charity page (node/add/charity) from

Create Charity

to the much more accurate

Suggest a Charity

title.

There may be a better way for non-programmers to do this (i.e., a Drupal module), but at the time of this writing in March, 2012, I couldn't find a module to do this. Hopefully I'm wrong, as this seems like a common need. (I'm surprised it's not a field you can set when creating a new content type.)

Note: This approach works in Drupal 6. I don't know if it works in Drupal 7.

Second Note: The documentation from the Drupal Page Title module suggests that I should use the name "node title" and not "page title" when referring to this form element.

Drupal hook_form_alter function naming syntax

While I'm in the neighborhood, it's important to know that your form "alter" function must be named according to the Drupal naming convention. Specifically the alter form function must be named according to this convention, with each field separated by the underscore character:

modulename + 'form' + formID + 'alter'

So a modulename of "foobar" with a form id of "baz" will look like this:

foobar_form_baz_alter

and its full signature will look like this:

function foobar_form_baz_alter(&$form, &$form_state)

You can find the ID for your form by looking at the source code that's generated for your "Create" page. For instance, I went to my "Create Charity" page at the "node/add/charity" URI, selected "View Source" in my browser, then searched for "form_id" to find the name Drupal assigns to this form. Drupal named my form charity_node_form, so I used that name when creating my function name.

In summary, if you need to override the node title (page title) for your "create" form when you create a new content type in Drupal, I hope this article is helpful.

Post new comment

The content of this field is kept private and will not be shown publicly.