Remove unwanted form elements (example: removing Your name: <username> from comment forms)
These code snippets illustrate how to unset (remove) any undesired form element from a specific form. If you don't want to only remove elements from a form, you can also alter existing values or add new elements to a form using these methods. The example in this lesson uses the Comment form, removing the Your name: <username> (which appears only to logged in users) as well as the title above the textarea of the form. There are two ways you can accomplish this, either using hook_theme() in your template.php, or using hook_form_alter() in a custom module. Both methods are very useful, and you will learn a lot by attempting both.
Using hook_theme() in template.php
Instead of this lesson repeating already existing (and excellent) information, instead please see Using hook_theme() to modify forms and Lullabot: modifying forms. Below is the code for this particular example. Add the following code snippet to your template.php file. Replace MYTHEME with the name of your own theme (e.g. garland) and clear the theme registry after adding the code (see Administer > Site configuration > Performance). Do not include the opening and closing php tags.
<?php
/**
* Implementation of hook_theme().
*/
function MYTHEME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function MYTHEME_comment_form($form) {
unset($form['_author']);
unset($form['comment_filter']['comment']['#title']);
return drupal_render($form);
}
?>Using hook_form_alter() in a custom module
Many Drupal developers/themers create a small "personal" module to paste or add various bits of code into which have to be applied from a module rather than within the theme template files (some things cannot be modified from the theme layer, while they can be modified from within a module). It's useful to name the module customsite or something along those lines (rather than a site-specific name), as you can then easily recycle the code for future Drupal sites without needing to adjust anything.
If you do not yet have a personal module, you should create one now following the steps below. If you do have a Custom module, simply paste in the function from the code below for altering the form, and you're done.
First make a folder at sites/all/modules/customsite which is where you will save the following two files.
Make a file called customsite.info and paste in the following code:
name = Custom Site functions
description = Custom functions for this site.
core = 6.x
version = "6.x-1.0"Make a file called customsite.module and paste in the following code:
<?php
// $Id: customsite.module
/**
* @file
* Custom functions for this site.
*/
/**
* Unset these form elements from the comment_form using hook_form_alter
*/
function customsite_form_comment_form_alter(&$form, &$form_state) {
unset($form['_author']);
unset($form['comment_filter']['comment']['#title']);
}In the above code the "Comment:*" title that usually shows directly above the main textarea of the form was also removed using the second unset line (since the text "Post new comment" or "Reply" already shows above the form, removing this extra title helps simplify the form (the textarea does not really need a title: its purpose is already self-evident without the additional text).
After saving these two files, go to Administer > Site building > Modules (admin/build/modules) and enable the new "Custom Site functions" module that you've created. Go view a comment form to ensure that it worked as expected.
You can identify other forms throughout the site and alter them in a similar fashion. Viewing the source of the rendered page in your browser will often reveal the form ID with which you can locate the code in a module that is generating the form. Search the module's code for that ID and it should bring you directly to (or at least close to) the part of the code you wish to alter. More tips on finding the form ID are available in Lullabot's Modifying forms article.
The "Changing Forms with hook_form_alter()" pages of the Pro Drupal Development book are also very helpful in learning more about altering forms.

Thanks. This was a huge help.
Thanks. This was a huge help. I would also like to make the following changes to the comment form. Any idea how? Not knowing much php yet I really can only cut/paste snippets and hope...
Remove the "Preview" button.
Change the "Save" label on Save-button to "Post" (without using string overrides, which will make the change everywhere)
I'm also trying to make these same two changes to a create-content form that I built with CCK. The create-content form automatically comes with a form element title that I don't want above each cck field. Is their a similar approach for altering content-creation forms?
Thanks again.
great stuff. would like what
great stuff. would like what else can be done with custom module. can this be done with templating also? (template.php)
to remove other fields and preveiw button
add
unset($form['mail']);
unset($form['homepage']);
unset($form['preview']);
Yes I think so
I have limited experience with it so far, but yes I think a lot (if not all) can be done in template.php (came up in a recent discussion)...
There's various code examples in that thread, and also here are the links to the lessons:
http://adaptivethemes.com/using-hook-theme-to-modify-drupal-forms
http://www.lullabot.com/articles/modifying-forms-5-and-6
One example I found yesterday of something that couldn't be done this way (again, to my limited knowledge in this area) was adding a #redirect to the contact form (it however works in a module).
In case it helps, here's the snippet I used (in customsite.module):
function customsite_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'comment_form':
$form['comment_filter']['smileys']['#weight'] = 10;
break;
case 'contact_mail_page':
$form['intro']['#value'] = t('This is some text');
$form['#redirect'] = 'node/2';
break;
}
}
I'll update the wording of this documentation page (to explain if/how template.php can be used for this) once I have a stronger grasp on the subject.
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides