By DayShallCome on
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
I'm not sure if this is the
I'm not sure if this is the best way to do it, but putting the following in hook_form_alter seems to work:
This is how you change the
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).
Good luck
That works for most forms,
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:
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 :)
When form validation fails
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().
There is an issue regarding
There is an issue regarding this: https://www.drupal.org/node/671574
We can use after_build
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;
}