Hi,

I dont want to change the entire theme. I just want a different stylesheet for different sections.

Is that possible?

Comments

hass’s picture

Status: Active » Fixed

Yes, but not with sections module.

jbernal.web.dev’s picture

Status: Fixed » Active

Hi pareen -

I'm new to Sections and have only gotten as far as browsing through the Sections module code in sections.module. But I know you don't need Sections to accomplish what you want to do (so my explanation here doesn't involve Sections.) There is a built-in Drupal function to add a CSS file to a theme called drupal_add_css(). When combined with path checking, you can invoke your CSS file for only certain paths. Both path checking and drupal_add_css() can be combined in your theme's page preprocessor, or yourthemename_preprocess_page() to produce the kind of behavior you're looking for.

Remember that if your theme name is flowers then the preprocess function would be named flowers_preprocess_page(&$vars) You can include preprocess functions in your theme's template.php file. If your theme doesn't have a template.php file you can simply create the file in your theme's directory.

So you would have:

//template.php
//&$vars is simply the aggregated variables available at this point in the template building process

flowers_preprocess_page(&$vars) {

/**
 * add additional CSS files using drupal_add_css()
 * but only if my path contains "tulips"
 * drupal_add_css() returns an array of stylesheets
 * at this point in building the page template, $vars['styles'] has already been built
 * so call drupal_get_css() again in order to re-build $vars['styles']
 * after the call to drupal_get_css(), $vars['styles'] will be repopulated with
 * your additional stylesheet
 */

 if( module_exists('path') ) {
    $alias = drupal_get_path_alias( $_GET['q'] );

    $check_path = "tulips"; //we are looking for any pages that fall in the "tulips" path, e.g., "/tulips/boulder-co-tulip-festival"
    if( strpos( $check_path, $alias ) !==FALSE ) {

       $addcsspath = path_to_theme() . "/drupaladd.css";
       $css = drupal_add_css( $addcsspath, "theme","all" );
       //uncomment this line below to see how Drupal builds its internal CSS array
       //print_r( $css );
       $vars['styles'] = drupal_get_css(); //rebuild the styles variable with an array that includes all previous CSS files and the newer CSS file

    }
}

}//flowers_preprocess_page()

Notice that $vars['styles'] is available to your theme's page.tpl.php and other page template files as simply $styles.

hass’s picture

Status: Active » Fixed

Send me an email if you need some paid consulting, please.

pareen’s picture

Thanks Jose.

It works!

However, I was looking out for some way I could accomplish this without getting into php.

Is there any module that adds css classes based on url/path?

@hass: thanks

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.