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
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
}
}
-
this is better! but I personally think that having multiple admins is not good from the security perspective.– AjitSCommented Sep 11, 2012 at 5:50
-
1This is not meaning we are in admin pages, just check the current user role is
admin_role
in everywhere– YuseferiCommented 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.– TeeganCommented Dec 1, 2014 at 6:39
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
}
}
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 .
}
}