In the comment form field (core comment module) I would to leave the comment author name empty, in order to encourage commentators to come up with original epithets.
What's the easiest way to do it?
In the comment form field (core comment module) I would to leave the comment author name empty, in order to encourage commentators to come up with original epithets.
What's the easiest way to do it?
I see a few of ways this could be done:
I would probably opt for option 1 as this provides greater flexibility in altering the forms to suit other requirements in the future
hook_form_alter()
http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_alter/7
Allows you to hook in to and alter any form, including setting the default text to empty. Here's some more help on what the valid elements are:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html
Best way to start is to determine the form ID by creating a mymodule_form_alter()
function and printing out the $form_id
using print
so it appears at the top of the page. (Or better still, use the devel module to print it out in a Drupal status message). Once you know the form ID, you can add an if
to make sure you only act on that form and then alter the array to remove the default value you don't want.
Look at pretty much any module for an example implementation. Many modules use this hook. I can't think of any specific example off the top of my head, but there are loads.
EDIT: Here's a good Lullabot blog post on the matter... there's not much different between Drupal 6 and Drupal 7 in this regard, AFAIK: http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6
hook_form_FORM_ID_alter()
, which is invoked before every hook_form_alter()
implementations, and doesn't need to check the value of a parameter passed to the hook function.
Just a note for those who decide to go with a custom module, but get Anonymous in the Name input box for no apparent reason, and their hook_form_alter
(or hook_form_comment_form_alter
) is okay.
Make sure to remove the comment_info_name
cookie, which the comment
's module Javascript uses to store the previously used name. It took me some time to find out...
It's all covered in one place here: http://jan.tomka.name/blog/how-remove-anonymous-name-comment-form
This is the kind of simple tweak that I'd do with JavaScript.
$(document).ready(function() {
var editbox = $('#edit-name');
if (editbox.val() == 'Anonymous' {
editbox.val('');
}
});
Pop that somewhere in your theme and the name field is cleared when the page is loaded.
Anonymous
with JavaScript disabled would fall under the category of graceful degradation. The question was "What's the easiest way to do it?", and I think adding a few lines of JavaScript to a template is easier than writing a module.
Commented
Mar 5, 2011 at 2:24