Rearrange Form Elements in template.php
I want to change the way the 'Add Page' form appears. In order to override the default page form, you'll have to register your own theme function in either a module or inside template.php.
I am going to use template.php. To get the internal name of the form, just view the html source of your 'Add Page' page and search for 'form_id'. The form_id of the form we're after is 'page_node_form'.
function garland_theme () {
return array(
'page_node_form' => array(
'arguments' => array('form' => NULL),
),
);
}I have registered the function. Note the form_id and the fact that $form should be passed as an argument.
Now I will create the overriding function that will be called instead of the default page_node_form. My function here is not very useful. It's only intended to illustrate how to do these things. What I'm going to do is move the 'title' field below the 'body' field. And while I'm at it I will change the label above the body field to include the users fullname. Fullname is a field I included in the user profile.
function garland_page_node_form($form) {
// if devel module is installed you'll get a nicely formatted representation of $form
dsm($form);
// $user will be needed to pull in the fullname variable
global $user;
// load the users profile data
profile_load_profile($user);
// grab the field 'fullname'
$fullname = $user->profile_fullname;
// change the size of the title field
$form['title']['#size'] = 30;
// change the 'title' or label of the title field to include the username
$form['title']['#title'] = t("Please give me a title, @me", array('@me' => $fullname));
// here is the cool part, call drupal_render on individual form elements.
// drupal_render() remembers elements already printed and
// so will not print that element again.
// Basically just pull the title part out and save it to a variable.
$title = drupal_render($form['title']);
// and then save the rest to another variable
$rest_of_form = drupal_render($form);
// then return it all marked up the way you want.
return $rest_of_form . $title;
}Simple and very effective way of rearranging forms.