i need to load a diff stylesheet depending upon which page the user is on.

when using: themename_preprocess_page(){kpr(&$variables;)}

i thought i'd see a pageId or pageTitle in which i could use for a conditional statement but i'm not seeing those variables. am i overlooking something? what can i use to uniquely identify a page so i can the use drupal_add_css()?

i guess i could do at the node level? but the stylesheet i need to load is really a page level thing.

can someone advise on the best way to do? big thanks in advance

  • i think maybe my theme was somehow not outputting a page title variable, i see that now when switching page to Bartik. – TopTomato May 16 '14 at 0:31

You may have trouble adding CSS with drupal_add_css in hook_preprocess_page because the $styles variable has already been built.

Better to add CSS in hook_preprocess_html or hook_page_build

Note, you should be able to target specific pages in CSS by using body classes.


body.page-node-type-article #content{

But yes, to check the path, you can use

$arg = arg($index); // system path component
$path = current_path(); // full base system path ($_GET['q']);
$alias = $_SERVER['REQUEST_URI']; // full alias only
| improve this answer | |

You can use arg to identify specific page.

It returns a component of the current Drupal path.

When viewing a page at the path "admin/structure/types", for example, arg(0) returns "admin", arg(1) returns "structure", and arg(2) returns "types".

For more info go through the documentation.

| improve this answer | |
  • thanks i will try. for some reason it seems that 'page title' is not part $variables in my custom theme. not sure why? – TopTomato May 16 '14 at 0:44
  • if i do echo arg(1) it outputs an integer – TopTomato May 16 '14 at 0:50
  • No, It will return menu callback's second part. – Rupesh May 16 '14 at 0:56

I'm using:

if ( preg_match('(^comment/reply/[0-9]*)', current_path())  === 1 ) {}
if ( preg_match('(^node/[0-9]*$)', current_path())  === 1 ) {}
if ( current_path() == 'search' ) {}

Using themename_preprocess_html, I think works fine. But I'm not sure if you need this exactly. However, is an alternative.

| improve this answer | |

hook_init() is best function to make changes at page level. You can use arg() function to get arguments of current page. Even you can also use menu_get_object() to get all possible values on rendered page.

| improve this answer | |
  • hook_init() gets called for every non-cached request that Drupal handles, which isn't always an HTML page. There are better options for pages, like hook_page_build(). – mpdonadio May 16 '14 at 15:01

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.