0

I know how to add css to individual form types using:

function theme_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'formtype_form':
      global $theme;
      $form['#attached']['css'] = array(
        drupal_get_path('theme', 'projectname') . '/css/new.css',
      );
    break;
 }
}

but how can I restrict this attached css to users with a single role?

1 Answer 1

1

You need to call $user object and compare user role with current logged in user.

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

  switch ($form_id) {
    case 'formtype_form':
      // change user_role to whatever role you want to restrict this css
      if (in_array('user_role', array_values($user->roles))) {
        $form['#attached']['css'] = array(
          drupal_get_path('theme', 'projectname') . '/css/new.css',
        );
      }
      break;
  }
}
2
  • Why is the $theme global there but is not used?
    – rooby
    May 11, 2013 at 8:32
  • @rooby $theme is useless with the current code, but maybe the questioner need it for a reason.
    – Aboodred1
    May 11, 2013 at 9:01

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.