4

I need to provide a way for hiding the name of the user who created a node, if the creator of a node wants to do so. In such cases, it should show Anonymous as the author of the node.

Any suggestions about how to implement this?

3
  • 1
    look at following setting in node form, screencast.com/t/MsI2dX2Gy
    – WaQaR Ali
    Commented Aug 3, 2016 at 13:25
  • 1
    @makbuk as you said "Just showing anonymous user as author." then look at my comment above
    – WaQaR Ali
    Commented Aug 3, 2016 at 14:02
  • Can you update your use case and full story behind this question in this post? To me, there are too many unknown.
    – Jimmy Ko
    Commented Aug 3, 2016 at 15:11

3 Answers 3

3

There are a few things in the original version of this question that really need review ...

Content may add only authorized users on my site.

In Drupal you cannot have content that adds users, so I'm going to assume it's rather the opposite you're looking for, ie: "Content may only be added by authorized users on my site".

I need to implement the functionality (option) of hiding a user name (author of сontent) if user wishes it.

Unless you have special customizations in place in your site, Drupal allows for multiple (authorized) users to author (change) content. So your "if user wish it" is a not clear enough: maybe you mean "the original author" (who first created the node), or maybe you mean "the user who last edited (changed) the content". For the sake of simplicity, I'm going to assume "the original author".

That is, to make him/her "the anonymous author of" сontent.

If you agree with my above assumptions, here is a blueprint of how you could implement this:

  1. Add a custom field to your content type, which is a boolean (true/false, yes/no, 0/1, whatever fits) that can be used when creating a node to have the original node author indicate if author should be shown (yes) or marked as "anonymous" (no).

  2. Use the Rules module to ensure that the "Authored by" value always remains the "original node author" (who created the node). This basically ensures that attempts to change the node author (similar to what is suggested in codenext's answer) will be rejected. Only using Rules should be sufficient to implement this (rather basic) validation. But if needed (to further enhance these validations), add the Rules Forms Support module.

  3. Use a preprocess function in your theme (similar to what is suggested in Zuzuesque's answer) to actually display the node author as requested by ... the original node author.

Note that if the original node author ever want to change his/her mind, that's still possible with this approach: just change the value of the custom field value as desired (after doing so, the logic in step 3. will ensure it's processed accordingly).

0
2

I am not sure if there is a single 'best way' to do this. An easy way would be to add a boolean field to your content type and call it something like 'post anonymously' or 'hide my username'.

Then it depends where you want to show the information. If you are using the Views Module this is particular easy:

  1. Add the field for user name and hide it from display.
  2. Add the boolean field.
  3. Under the no result behavior of the boolean field (check the option to count 0 as an empty field) and output your username + any optional text like authored by.
  4. Rewrite the boolean field and add your text for anonymous authors.

If you are not using Views you will want to use a proprocess function to manipulate the part of the page you display the author name on - e.g.: if you want to change how the author is shown on the node itself use template_preprocess_node(&$variables):

YOURTHEME_preprocess_node(&$variables) {
  $node = $variables['node'];

  if($node->type == 'your_node_type') {
    //change text if author wants to stay anonymous (else keep original)
    if ($node->field_your_bool_fieldname[0]['value'] == 1) {
      $variables['submitted'] = t('Submitted by anonymous on !datetime', array('!datetime' => $variables['date']));
    }
  }
}
4
  • Don't you think, this feature is available by default in drupal.
    – CodeNext
    Commented Aug 3, 2016 at 13:31
  • You will need the Views module (if you want to output your node using views), but I included how to change the display of the username if you only want to use core drupal, e.g. the default node pages.
    – Zuzuesque
    Commented Aug 3, 2016 at 13:34
  • Please can you look at my answer's image. The drupal by default provide, What user name you wan to add or you wan to leave it blank so anonymous will appear. Please let me know if I did not understood. Thanks
    – CodeNext
    Commented Aug 3, 2016 at 13:44
  • 1
    In your approach the user can select ANY user in the system as author - but that's not what makbuk wants if you read his comments. Anonymous (not verified) doesn't look too pretty either.
    – Zuzuesque
    Commented Aug 3, 2016 at 13:49
2

What you need is provided by Default in Drupal,

enter image description here

If I write my name: it will show:

Submitted by codenext on Wed, 07/20/2016 - 11:14

If I leave blank: it will shown:

Submitted by Anonymous (not verified) on Wed, 07/20/2016 - 11:14
3
  • Thank you for your attention. I tried this module, unfortunately, if you activate the option Author By, users can select the name of any registered user (it is not suitable) or leave it blank for display Anonim. In my case, I need only the ability to hide name if the user wants it.
    – makbuk
    Commented Aug 3, 2016 at 12:29
  • But if you are looking for this, It is by default in Drupal. If you create any content, there is a option of leave block for Anonymous, which has to be handled by that author. Are you looking something else?
    – CodeNext
    Commented Aug 3, 2016 at 12:37
  • Thank you for your attention, this option is not active for Authorizing users (it is available to the administrator), it can be enabled by using the module of which you wrote earlier Override Node Options, but in this case, it does not look good on the entry form. In my case it is better to use for this custom field as described in this decision. Thanks again for your help and attention.
    – makbuk
    Commented Aug 3, 2016 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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