Hello,

I have create a form using the web form module. I have added 5 fields for public users. When public users submit the form, role can see the form result and edit no problem.

I like to add 3 additional fields for a specific role only. When public users use the form they will not see the 3 fields. How can I add these 3 additional fields for a role only?

Thanks.

Comments

Stefan Lehmann’s picture

It's not possible with the Webform module alone. You could give this module a try here: https://www.drupal.org/project/webform_component_roles

But be aware that the webform module had a major update recently. I suspect that this module above only works with the old version branch 7-3.x ..

I like cookies!

webdev100’s picture

What other option do I have besides installing the module? I use web form 7x4

Anonymous users first submit a form that they want to use our product? After that, a role will talk to the users on phone and finalize the deal. Then, role needs to be able to look up the user from the web form system and add some info for the user. How can the role add info to the users? Examples of info: what price was finalized, how many people will use the products, etc. These info are not given to anonymous users.

Currently, once anonymous users submit the form, after finalize the deal, role simply enter the user information in a new form. What that means, form1 for users. form2 for role. Form1 has 5 fields. Form2 has Form1 fields plus 3 other fields. Once users submit the form, role copy and paste their info from form 1 to form 2 plus enter other info in form2. I am trying to avoid the extra work.

Stefan Lehmann’s picture

The only way I could think of (without doing custom coding) is hiding these special fields in your frontend theme via CSS on the webform submission form.

I'm pretty sure you can target an edit submission form and an initial submission form separately via the form classes, so it should be possible to show these fields just for the Admin role, as only they are able to edit submissions.

This of course is not secure in any way, as the CSS could easily be adjusted by your frontend users.

I like cookies!

ravibarnwal’s picture

You can alter the web form using hook_form_alter();

Example :

<?php
/**
 * Implements hook_form_alter().
 */
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  // To get the currently logged in user.
  global $user;
  // To target the required form.
  if ($form_id == 'FORM_ID') {
    // Set the role ID of the role that can edit.
    $rid = ROLE_ID_FOR ROLE USER NEEDS TO EDIT;
    // If user doesn't have the role in question.
    if (isset($user->roles[$rid])) {
      // Deny access to the field.
      $form['FIELD_NAME']['#access'] = FALSE;
    }
  }
}
?>

?>

webdev100’s picture

I found something easy. Simply edit the webform component. There is an option for PRIVATE. Just select the option. If the role has access to form results page, they can see the fields that are private. Anonymous users will not see the private fields unless they have access to webform results page. Now, I added altogether 5 fields. Anonymous can see 3 fields and role can see/edit all.

Stefan Lehmann’s picture

Oh .. I didn't know about that. nice find. :-)

I like cookies!