When people comment on my blog posts, the system defaults to the name "Anonymous" as the "Your Name" field on the comment form. How can I change this so the field is blank but required. It looks really stupid when all the comments are from "Anonymous (not verified)" Also, is there a way to turn off the "not verified" thing. Again, it looks bad. Most users are not going to create an account just to comment.

Comments

madrush’s picture

I'd be interested in learning how to do this too....

jamesdixon’s picture

Found help for those of you wanting to allow anonymous users to enter their name:

>go to Content management->Content types and click edit for your node type and enable "Anonymous posters must leave their contact information".

http://drupal.org/node/455382

Hope that helps!

francort’s picture

That option doesn't exist on D5, but is clearly the best on D6.

nkcampus’s picture

I tried Drupal 6 but Drupal 5 may be similar. In Drupal 6, I changed
variable_get('anonymous', t('Anonymous'))
to
variable_get('anonymous', t(''))

in line 1349 of comment.module. This seems to be in line 1533 in Drupal 5.6.

It's not a best way, but anyway just works a little.

hannesh’s picture

I've changed the lines in all the comment.module, but to no avail. I still see the "anonymous" name there.

francort’s picture

If you read the entire thread you will read that, changing core module isn't the best way for this problem and there are other ways to do so.

hannesh’s picture

thanks, i've looked at other things, you mean http://drupal.org/node/240610#comment-1028680? it seems to be only for D5, but i run a D6

francort’s picture

I don't know exactly what is your kind of "Anonymous" problem :)
You can change the string for them in admin/settings/site-information

hannesh’s picture

indeed. to specify: the anonymous field needs to be cleared, empty, so that the guest has to invent it. So far, most want to just leave it as it is, which then creates confusion, and paints the pictures, that the comments are completely anonymous.

ps! i run D6.

francort’s picture

Ok, i think i've got it.
Do you want "Your name" field to be empty for anonymous users when comment form is rendered but not allowed to be empty when they submit their comment?

hannesh’s picture

indeed.

francort’s picture

For the content type you want to be commented, go to admin/content/types, select a content type and set the configuration for comment to "Anonymous posters must leave their contact information".
To leave the form field empty, you can use javascript or module approach.
Module approach should look like this:
d6.info

; $Id: Exp $
name = Custom module
description = form alter module
core = 6.x
package = can not be more optional

d6.module

<?php
function d6_form_alter(&$form, $form_state, $form_id){
	if( $form_id == "comment_form" ){
		if( isset($form['name']['#default_value'])) $form['name']['#default_value'] = '';
	}
}
?>

If someone wants to post a javascript approach, even better :)

hannesh’s picture

thank you, i enabled the module, the content types were already configured, but it does not seem to do the trick.
it still shows the same anonymous name and changes, as i change the name for anon. user in site information page.

francort’s picture

I have tried this before posting and it did work.
The little module i have written is just to have the field "name" empty
To make "name" field required, it is necessary to set it on your content type configuration.(for each content type you want to)
To be displayed as you want, it is your theme who's doing the job

When i tried this , i've do it in a D6 drupal with Garland theme.

hannesh’s picture

i changed the folder name for the module, before it was just some letters, but with the name "d6" it works. i did not do anything else.
Thank you!

hannesh’s picture

i got complaints, that there is still the "anonymous" name for some people. Found out now, that it's important to get rid of the older sessions, that have it saved.

calvintennant’s picture

I used this. Thanks.

sydneyshan’s picture

However this should really be a customisable feature with a UI...

I've modified the function above to check against the 'anonymous' Drupal variable which is controlled here: /admin/settings/site-information

d6.module

function d6_form_alter(&$form, $form_state, $form_id){
    if( $form_id == "comment_form" ){
        if( $form['name']['#default_value'] == variable_get('anonymous', t('Anonymous')) ){ $form['name']['#default_value'] = ''; }
    }
}

AppleBag’s picture

Thanks for the code, now I want to check the form value after the comment is submitted, to see if the Name field was left blank, and if so, change it back to Anonymous, just so that the name isn't left blank in case the person tried to do that. I'm using:

function mymodule_form_comment_form_alter(&$form, &$form_state) with the code above in it, but having trouble finding the hook function to use, to check what the user entered, so that I can alter it as necessary. Anyone know?

Drupal Jedi’s picture

francort, your approach works great! Thanks!

daveatkins’s picture

I have been able to clean things up a bit by changing the name of the anonymous user to "Guest" and editing the template files directly to comment out the "not verified" text.

jruz’s picture

for user can leave information
on line 1524 of comment.module

original

    $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous'))

fix

    $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30

for user must leave information
on line 1533 of comment.module

original

    $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')), '#required' => TRUE);

fix

   $form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#required' => TRUE);
RobertNelsonVance’s picture

Could someone provide further example of how to override comment_form?

http://api.drupal.org/api/function/comment_form/5

Specifically, I am looking in the comment.module to replace....

$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', t('Anonymous')), '#required' => TRUE);

with....

$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#required' => TRUE);

I tried adding to my "template.php" file....

function phptemplate_comment_form($form) {
   return _phptemplate_callback('comment-form', array('form' => $form));
}

and created a "comment-form.tpl.php" file....

<?php 
$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#required' => TRUE);
print drupal_render($form);
?>

This eliminates the "Anonymous" pre-fill default value from the field when the page is loaded, but it also results in the following error....

warning: implode() [function.implode]: Invalid arguments passed in /home/.watson/username/domain/includes/form.inc on line 623.

I also tried the following in my "comment-form.tpl.php" file....

<?php 
$form['name']['#default_value'] = 'test';
print drupal_render($form);
?>

and this didn't seem to do anything.

I am guessing that, because I am still learning this, it is a syntax error on my part.

I tried following the examples on http://api.drupal.org/api/file/developer/topics/forms_api.html/5 but am apparently missing something.

Thanks for any help!

francort’s picture

To change the values of comment-form it is better to do a small custom module and use hook_form_alter

francort’s picture

There are more than one way to do so. It is not a good thing to do , to change modules directly and it must be avoid.
One way to do it without any programming acknowledge, is this:

1.- Enable "Locale" module
2.- Go to Administer › Site configuration > Localization and add a custom lenguaje and set it as default( keep english active anyway )
3.- Go to "manage strings"( same page ) and search for the string you want to change( e.g. Anonymous )
4.- Change the caption of that label with something you like ( one space if you don't want it to appear , o something like "user" )

Always if there are text in a module or theme like: t('some text') , you can change that text throught Locale module

Late reply, but in other way one will be changing that over and over again in every update

wintervanilla’s picture

francort - I've had to use the comment.module edit because I couldn't get your solution to work. I know that editing core modules is generally not good - ie. a pain when updating.. so I'd like to use the locale solution - do you know of a good explanation for using this module? or can you spell it out for me a little more explicitly?

Thanks!
Andrew

WorldFallz’s picture

another option might be the http://drupal.org/project/stringoverrides module

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

_
Care about the future of the Drupal.org forums? Please join our conversation and show support for improving the forums infrastructure.

francort’s picture

In which step did you get stuck?
What Locale module does is to relate strings( one per language ) to every string in a Drupal system that is a parameter of t() function. That strings can be imported from a file ( .po file ) or by written them by hand.
So, just give me a clue to know how to help you :)

rkarajgi’s picture

Go to /admin/settings/site-information, there is a form field which lets you override the "Anonymous" string.

I hope this helps.

----------------------------------------------------------
- Rajeev Karajgikar, Drupaler in South Bay Area

----------------------------------------------------------
- R Karajgikar

RobertNelsonVance’s picture

This is an ok solution if you want to change "anonymous" site-wide to something like "user". You probably wouldn't, however, want to replace "anonymous" site-wide with a space, simply because on pages that display post information, it would display as "Submitted by on 09/24/2008 - 22:15", for example.

I want to keep the anonymous user "anonymous", I want it to say "anonymous" on the pages that allow users to post anonymously, but I also want the "Your name" field on the comment form to load blank so it forces users to enter something of their own rather than just clicking submit and the form passing because "anonymous" was already filled in the field.

For my case, your solution doesn't help.

I also don't quite understand why I would need to create a custom module for this. It seems like it should be more simple. See my post above (http://drupal.org/node/240610#comment-1018916) for how I was trying to modify this in my theme.

Thanks! Robert

francort’s picture

In a module that transform a little other module. In this case, one form in comment.
It ss the more "clear" way to do it, as code standard and sometimes in performance too. Besides that, you'll find documentation to help you if you are in the common path....but enought of this...
I've called this module d5 ( you can call it as you want )

d5.info

; $Id: Exp $
name = Una prueba
description = solo form alter
core = 5.x
package = can not be more optional

d5.module

function d5_form_alter($form_id, &$form) {
	if( $form_id == "comment_form"){
		if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
			$form['name'] = array('#type' => 'textfield', '#title' => t('Your name'), '#maxlength' => 60, '#size' => 30, '#required' => TRUE);
		    $form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'), '#required' => TRUE);		
		    $form['homepage'] = array('#type' => 'textfield', '#title' => t('Homepage'), '#maxlength' => 255, '#size' => 30, '#default_value' => $edit['homepage']);
  		}
	}
}

And also remember to set in admin->content->comment->settings Anonymous commenting: as Anonymous posters must leave their contact information

i hope this would work for you :)

rkarajgi’s picture

OK - I got what you are trying to do.

javascript approach may be best to have the word "anonymous" blanked out and user forced to enter some name.

You can
- Either alter theme (page.tpl.php) to load the your javascript code on all pages - bit of extra load on all pages which do not provide comment feature
- Or you can write a custom module that loads js only where comment is available

The js code should be straight - I dont have any ready - but if you are already loading jquery then life is easier.

I hope this helps.

----------------------------------------------------------
- Rajeev Karajgikar, Drupaler in South Bay Area

----------------------------------------------------------
- R Karajgikar

gobioasis’s picture

page.tpl.php - in header section add following code.

<script type="text/javascript">	
    $(function(){var a = $('#comment-form').find('#edit-name');if($(a).val() == '<?=variable_get('anonymous', t('Anonymous'));?>'){$(a).val('');}});
</script>
sanderc’s picture

This javascript snippet is the best way for my implementation of Drupal, but the website I am developing uses lots of different page-[whatever].tpl.php files. Therefore I don't want to put it in my page.tpl.php. So I placed the snippet in an external .js file.

However, then the php-variable obviously does not work, and I had to replace the variable with the fixed string 'Anonymous'. Is there any way to get the PHP variable and pass it to javascript?

(Sorry for this comment on such an old topic, but well: it is probably better than starting a new one.)

francort’s picture

Yes, there is a way.
On your php code, you can add something like this(on some preprocess function)

//...code...
$settings[my_variable] = 'this is my variable!!!';
drupal_add_js($settings, 'setting')
//...code...

So in your js code you'll have
Drupal.settings.my_variable ( if you use an alert on that you'll see "this is my variable!!!" )

This is explained on a lot of sites if you need more information and I leave a link for one I though can be helpful

Cheers,
Francisco

doublejosh’s picture