3

I have a Drupal 7.7 installation for which I'm activating comments using the standard Comments module. Everything works just fine except for a annoying quirk.

The admin username is "Christoffer", and for some reason anonymous users can not use the name "Christoffer" for their comments. If I add an additional character or change the spelling it works but it seems that an anonymous user cannot have the same name as a registred user. Can I allow it somehow?

Thanks

  • As of right now, the current version of D7 is 7.26. I can only assume that you are from the future. – emc Mar 11 '14 at 19:03
3

It is not possible. The form validation handler for the comment form (comment_form_validate()) doesn't allow to the anonymous users to choose a username that has been already used by a registered user. The function contains the following code:

  elseif ($form_state['values']['is_anonymous']) {
    // Validate anonymous comment author fields (if given). If the (original)
    // author of this comment was an anonymous user, verify that no registered
    // user with this name exists.
    if ($form_state['values']['name']) {
      $query = db_select('users', 'u');
      $query->addField('u', 'uid', 'uid');
      $taken = $query
        ->condition('name', db_like($form_state['values']['name']), 'LIKE')
        ->countQuery()
        ->execute()
        ->fetchField();
      if ($taken) {
        form_set_error('name', t('The name you used belongs to a registered user.'));
      }
    }

The code avoid the confusion there could be between anonymous users that reports the link to their homepage and an authenticated user, when the anonymous users would use a username already in use.

To remove that limitation (which I don't think it's a good idea), you should implement hook_form_comment_form_alter() to add your own validation handler (with $form['#validate'][] = 'mymodule_comment_form_validate', or similar code. Your form validation handler should be a copy of the one used from the Comment module, except the part that reports an error when an authenticated user is already using that username.
There is another problem, though; the form validation handler used by the Comment module contains the following code:

  if (!empty($form_state['values']['cid'])) {
    // Verify the name in case it is being changed from being anonymous.
    $account = user_load_by_name($form_state['values']['name']);
    $form_state['values']['uid'] = $account ? $account->uid : 0;
    // …
  }

In your case, if an anonymous user would use "Christoffer" as username, and that is already used by an authenticated user, the code would load the user object for the authenticated user, and the comment would be attributed to the authenticated user, not the anonymous user.

There could be also other code in the Comment module that takes the assumption there cannot be two users with the same username, which is a restriction present in Drupal.

| improve this answer | |
  • So I could hook into the function and allow the name anyway, right? – Christoffer Aug 4 '11 at 14:40
  • I expanded my answer. – kiamlaluno Aug 4 '11 at 15:18
  • 1
    Using $form['#validate'][] = 'mymodule_comment_form_validate' only adds the new function to end of the array; it doesn't replace the what was already there. To do this, you need to either use unset($form['#validate']) which empties the array entirely (this works if there are no validation functions that you do want to keep), or loop through and removing the instance of comment_form_validate that already exists there. For my site, the only users are only a few admins, and it doesn't use comment editing or web sites or anything that would need to be connected to a user. This works for me. – Cassandra Gelvin Nov 14 '12 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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