Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm trying to add a CSS class to some certain pages for some certain menu styling depending on the current alias path. It works well with the following function but I'm wondering is there another way to split the current alias parts so I can use them as arguments? Here's my present function:

function mytheme_preprocess_html(&$vars) {

  $path = drupal_get_path_alias($_GET['q']);
  $aliases = explode('/', $path);

  if (isset($aliases[2]) && $aliases[2] == 'foo') {
    $vars['classes_array'][] = drupal_clean_css_identifier($aliases[2]);
  }
}
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You should be using Drupal's arg function: https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7

Something like this should work:

function mytheme_preprocess_html(&$vars) {

    if (arg(2, drupal_get_path_alias()) == 'foo') {
        $vars['classes_array'][] = drupal_clean_css_identifier(arg(2, drupal_get_path_alias()));
    }
}

Added drupal_get_path_alias() as an arguement of arg() as suggested by @koivo

share|improve this answer
 
bummer, I thought the comments would remain after I undeleted it... –  Scott Joudry Aug 20 '13 at 14:53
add comment

There was one answer from a guy which now is deleted suggesting to use arg(2) instead. I commented after trying it out that arg() doesn't seem to work with alias paths. Well, I now found out it does and that's how by passing the Drupal alias path into the arg() function:

if (arg(2, drupal_get_path_alias()) == 'foo') {
        $vars['classes_array'][] = drupal_clean_css_identifier(arg(2, drupal_get_path_alias()));
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.