5

Is it possible to extract the PHP string for user type, when the user is not an administrator user?

I need to discern between staff or non-staff users. I am aware of $is_admin, but in this case I would need something like $is_staff.
I need this so I can apply CSS styles to hide/show some fields that I want to be visible only to staff users.

Is anyone aware if there's anything that might help me?

5

If $user is the user object, then $user->roles is an array containing all the roles the user has. Drupal doesn't have the concept of user type, and the user roles are the closest it has.

If you want to check if the user has the "editor" role, for example, you can use code similar to the following one:

$global $user;

if (in_array(array_values($user->roles), "editor")) {
  // The user has the "editor" role.
}

Depending on what you are trying to achieve, you could also be interested in checking if the user has a specific permission, which is what is normally done in Drupal, where instead of checking the role assigned to a user, it is checked which permission the user has (using user_access()).

As for hiding a form field, this can be done implementing hook_form_alter(), or hook_form_FORM_ID_alter() and altering the property "#access" of the form field.

Supposing that "example_form" is the form ID for the form you want to modify, and "file" is the ID for the form field to change, then you can use code similar to the following one ("mymodule" is the name of the module; change it to match the name of the module that contains the hook):

function mymodule_form_example_form_alter(&$form, &$form_state) {
  $form['file']['#access'] = user_access('administer file');
}

"Administer file" is the permission that the user needs to have. Alternatively, if you want to only check if the user has the "editor" role, you can use code similar to the following one:

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

  $form['file']['#access'] = in_array(array_values($user->roles), "editor");
}

The global variable contains the user object for the currently logged in user.

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.