I'd like to somehow change the default title of Node Forms.

For instance, the defualt title of a Node form is "Submit *content-type*". Is there anyway to change this using hook_form_alter? I've tried, but it seems like the actual title of a node form (and I don't mean the $title variable of the content type you are creating) is set from core. Anyone have any ideas?

Comments

David_Rothstein’s picture

I'm not sure if this is the best way to do it, but putting the following in hook_form_alter seems to work:

if (isset($form['type']) && $form_id == $form['type']['#value'] .'_node_form') {
  drupal_set_title('New title goes here.');
}
marcelomg’s picture

This is how you change the title with hook_form_alter:

Put the following code inside your custom module (if you don't have a custom module, you should create one to use hook_form_alter).

<?php
function yourModule_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'the_form_id':
      drupal_set_title('Your New Title');
      break;
  }
}
?>

Good luck

David_Rothstein’s picture

That works for most forms, and in the case of Drupal 7 it should work for the node form too (you can use 'node_form' as the form ID). Or even better, you can implement a form-specific alter hook with 'node_form' in the function name, like this:

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function yourmodule_form_node_form_alter(&$form, $form_state) {
  drupal_set_title(t('New title for the node form'));
}

However, neither of those options will work in Drupal 6, because there is no single base "node form" defined by Drupal, rather a separate one for each content type. So you have to use something more like the code I posted in my comment from four years ago :)

crantok’s picture

If a required field is left empty, so that form validation fails, then Drupal shows the form again with the errors highlighted. When the form is re-shown, the form_alter function is not called again. (Tested in Drupal 7.28 .) I assume this is because any alterations to the form are preserved. Unfortunately, this means "side-effects" like drupal_set_title() are lost.

Edit: I experimented with using hook_boot, hook_init and hook_exit. In the end I found hook_page_alter to be the most reliable place to put the call to drupal_set_title().

malberts’s picture

There is an issue regarding this: https://www.drupal.org/node/671574

kkalaskar’s picture

function yourmodulename_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'thisformid'){

$form['#after_build'][] = 'yourmodulename_after_build'; // hook_form_alter.

}

}

function yourmodulename_after_build($form, &$form_state) {
  drupal_set_title(t('TITLE'));
  return $form;
}