I need a way to either add extra fields or make two fields obligatory when an anonymous user is editing a node. I'll explain:
There is a node type in my site that can be freely edited, modifications are saved as new revisions. To know who edited a node i want to add a mail field and other extra fields that require other info from the anonymous user but these fields don't need to be obgligatory when the nodes are edited by registered users.

I was thinking about using the module suggested in this discussion: How can anonymous users be allowed to post while still maintaining content integrity?
but i require that the mail entered from the user is saved inside the node and also i require an extra field other than the mail.

Is there any way to achieve that?


You could use a the hook_form_alter to get the desired function:

function mymodule_form_alter(&$form, &$form_state) {      
  if($form['type']['#value'] == 'DESIRED_CONTENT_TYPE') {
    if( user_is_anonymous() ){
      //Fields are required for anonymous users
      $form['field_ABC'][LANGUAGE_NONE]['0']['value']['#required'] = TRUE;
      $form['field_DEF'][LANGUAGE_NONE]['0']['value']['#required'] = TRUE;
      //Hide fields for logged in users
      $form['field_ABC']['#access'] = FALSE;
      $form['field_DEF']['#access'] = FALSE;
  • Isn't there a module to do it? Otherwise how do i specify that this has to happen only for a certain content type?
    – valepu
    Dec 9 '14 at 13:48
  • You can use $form['type']['#value'] to get the content type
    – sgoelz
    Dec 9 '14 at 13:52
  • missing a "{" on line 2, but it works, thanks!
    – valepu
    Dec 9 '14 at 14:11
  • great! I added the missing "{" in the example.
    – sgoelz
    Dec 9 '14 at 14:16

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.