0

I use drupal 7.x and field_group and nultiple registration module. How I can use a template file (.tpl) for user-profile-form only based on roles. in other words when a user with A role login to site and go to user/%/edit page see a specified form with specified fields. and When user loged on with B role have another form for edit information.field permissions control fields but field groups is visible yet because field_group module have a bug with field_group_multiple module.

So , I want to use specified tpl file for user-profile-form (no registration page or profile page - only user-profile-form ) by role . Can you help me ?

1 Answer 1

0

You can simply load $user variable in your template file and check which role current user is having and simply based on the role print your form. for e.g

if ($user->roles[4] == 'role1') {
  print render $form['form_field1'];
  print render $form['form_field2'];
}
if ($user->roles[5] == 'role2') {
  print render $form['form_field_field1'];
  print render $form['form_field_field2'];
}

In the end don't forget to print the following

print drupal_render_children($form);
8
  • I have added fields to user profile in /admin/config/people/accounts/fields page.I don't know programming and php but needs to your help. 1-- I create a file named "user-profile-form.tpl.php" ? 2-- in this line of your codes if ($user->roles[4] == 'role1') { what is 4 ? 3-- My fields is called "group_info_sherkatha" or "field_nameostad" , I should use only these names ? can you send me a step by step guidance and full codes of my file? I want to show fields in div tags.
    – omiti
    Commented May 24, 2017 at 4:33
  • 1
    After creating your user-profile-form.tpl.php file, first thing you should do is check whether the tpl is getting called or not by just doing <?php echo "it works"; ?> then inside php tags just do print_r($form); which will give you the renderable array in which you can find your field name first and then just do print render($form['field_name']); just what I have explained in my answer. Commented May 24, 2017 at 5:29
  • I am sorry for these questions.I must put only <?php echo "it works"; ?> in my tpl file? So i secound step I should write only <?php print_r($form); ?> instead of <?php echo "it works"; ?> in my tpl file? in thirth step I must tell drupal to render fields by role , So in your sample code and in this line "if ($user->roles[5] == 'role2')" what is number 5 ? Is this number neccessary?
    – omiti
    Commented May 27, 2017 at 5:23
  • 1) I must put only <?php echo "it works"; ?> in my tpl file? - It is not required to add above php code in our template, just from the prospective to check whether the file is getting called or not, I suggested you to print that string. If this gets printed that means our template file is getting called. Now after checking you should remove this code. Commented May 29, 2017 at 9:13
  • 2)So i second step I should write only <?php print_r($form); ?> instead of <?php echo "it works"; ?> in my tpl file? -Yes after removing <?php echo "it works"; ?> code you should print $form which will give you a renderable array. This array will have your fields which you need to render one by one separately. After rendering all of your fields you should remove <?php print_r($form) ?> code also. Commented May 29, 2017 at 9:16

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.