1

I have used a hook_form_alter to alter a form fields value randomly and its working fine, but the problem is that when i open this webform in admin panel its just change the value which i dont. Its should keep the same value which is submitted by frontend user. i want this hook to work in case of frontend only not for admin.

3 Answers 3

2

If you have multiple admins, then check with user role, lets have admin_role as a Admin role

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  global $user;

  if(in_array('admin_role', $user->roles)) {
    return;
  }
 else {
    //alter fields logic goes here
  }
}
3
  • this is better! but I personally think that having multiple admins is not good from the security perspective.
    – AjitS
    Commented Sep 11, 2012 at 5:50
  • 1
    This is not meaning we are in admin pages, just check the current user role is admin_role in everywhere
    – Yuseferi
    Commented Sep 11, 2012 at 6:14
  • This is going to apply everywhere not just in the admin area. And it won't work if there are multiple roles which can access the admin area.
    – Teegan
    Commented Dec 1, 2014 at 6:39
2

you could use the global variable $user to achieve it.

<?php

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  global $user;

  if($user->uid == 1) {
    return;
  }
  else {
    //alter fields logic goes here
  }
}
0
2

you should implement some check for the user roles of admin like this.

function modulename_form_alter(&$form, &$form_state, $form_id) {
  global $user;

  $adminroleid = 1 ; // It should be id for admin role, you should change this according to your's admin role id.

  if(array_key_exists($adminroleid, $user->roles)) {
    return;
  }
 else {
    // write your's form alter fields logic here .
  }
}

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.