7

Which hook/filter do I need to use to modify the 'add node' page title (This Title)? For example, when viewing "/node/add/" I want to change the title from 'Create ' to 'Add New '

15

There are a few different ways you could do this, but the way you asked for would utilize hook_form_alter():

<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'mynodetype_node_form') {
    drupal_set_title('My new title');
  }
}
?>

This would be in a custom module named custom.module... drupal_set_title(), when called anywhere in the page load, will set the title appropriately. You're basically hooking into the node form rendering process, and saying 'set the title to what I want' at that point.

If you'd like to further differentiate what the title would be depending on whether the user's creating a new node or editing an existing node, use Devel module's dpm() function to print out the $form_state array, and check to see if there's a node ID already or not—if there is, you are editing a node; if not, you're creating a new node. Set the title appropriately.

While hook_form_alter() would probably be the most robust page (in case someone was viewing the node add form from somewhere other than node/add/[type]), you could also check the path in hook_init() and set the title there, or from a variety of other places (in Drupal 7, hook_page_alter() perhaps).

  • How can I tell if a form is new, or being edited? For example, if I wanted a new node to say "Add a New node" and if the node is being edited, "Edit This Node" Thank you for the answer above though, it is immensely helpful! – Citricguy Jul 8 '11 at 0:11
  • Check if the node has an nid already in the $form array. I forget which value has the nid in it, but it's in there, or the $form_state... if there is an nid, the user's editing the node. If there isn't, the node is a node add form... – geerlingguy Jul 10 '11 at 6:57
4

I had the same problem and I had to write a module for that.

You can find my contributed my module Node Add Title for Drupal 7

Node Add Title will let you change the title of the node creation pages and the node editing pages.

Hope it can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.