views:

24

answers:

1

When a user gets an Access Denied page, how can you use drupal_add_css to load a particular stylesheet? Is this possible?

+2  A: 

It is possible. The following code can add a CSS file when Drupal returns a access denied page.
Take in mind that when Drupal returns such error, the URL shown in the browser doesn't change; in example, if I am trying to access /admin/content, and I am not allowed to access that page, the shown URL doesn't change. You need to check the headers output from Drupal to verify if it output the access denied header.

function custom_module_preprocess_page(&$variables) {
  if (preg_match('@HTTP/1\.[01]\x20+403[^a-zA-Z0-9]@', drupal_get_headers())) {
    $variables['css'] = drupal_add_css($css_file_to_add);
    $variables['styles'] = drupal_get_css();
  }
}
kiamlaluno
Thanks I will give that a shot.
Kevin
Code similar to the one I wrote here is used in an existing third-party module; the difference is that the module (Nodewords) is adding meta tags, but the code to detect if Drupal has output an access denied page is the same, and it was suggested from a member of the Drupal security team to resolve a security issue reported for that module.
kiamlaluno
Worked. Except I had to do themename_preprocess_page instead.
Kevin

related questions