7

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?

1
  • My current site is D6, but I am starting the transition to to D7, so answers for both will be useful.
    – daphshez
    Commented Mar 4, 2011 at 16:22

4 Answers 4

1

I see a few of ways this could be done:

  1. Custom module - someone has even written it for you - http://drupal.org/node/240610#comment-1096024
  2. Using string overrides - change Anonymous to [empty string]
  3. Remove it from the comment module - Content management -> Content types and click edit for your node type and enable "Anonymous posters may not enter their contact information"
  4. Use javascript - someone has also written this as well - http://drupal.org/node/240610#comment-2051378

I would probably opt for option 1 as this provides greater flexibility in altering the forms to suit other requirements in the future

3
  • I used option 1.
    – daphshez
    Commented Mar 6, 2011 at 10:51
  • Also, option 3 doesn't really answer my question - I want to allow anonymous users to leave comments.
    – daphshez
    Commented Mar 6, 2011 at 11:04
  • Finally, note that if you use the realname module, it will override these changes.
    – daphshez
    Commented Mar 6, 2011 at 11:08
3

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

3
  • 2
    If the code is specific for a single form, then I would implement 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.
    – avpaderno
    Commented Mar 3, 2011 at 17:09
  • hook_form_FORM_ID_alter() isn't always a good alternative to hook_form_alter() as it has timing issues, it doesn't run at the same time as hook_form_alter() and can often lead to not getting the correct version of the form to alter, at least that's been my experience.
    – Decipher
    Commented Mar 3, 2011 at 19:27
  • thanks. The $form_id is "comment_form". There's some more detailed instructions for this specific form altering at drupal.org/node/240610#comment-1096024.
    – daphshez
    Commented Mar 6, 2011 at 10:53
2

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

1

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.

2
  • 1
    And if the user has JavaScript disabled, what then?
    – Decipher
    Commented Mar 5, 2011 at 0:07
  • @Decipher: I would say that the box still saying 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.
    – Paul Jones
    Commented Mar 5, 2011 at 2:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.