Add section variable to page template
| Project: | Sections |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
In most cases, I don't want to change the theme for my different sections, or even the template. I just want to add a CSS file for section specific styles, or add a class to my body tag. To facilitate this, it would be helpful to have a section variable available to theme_preprocess_page().
To satisfy my immediate needs, I added the following to the bottom of the if() statement in sections_preprocess_page():
<?php
// Create section variable.
$variables['section'] = array('sid' => $section->sid, 'name' => $string_clean);
?>However, I expect I'm going to have several sub-sections in my site and this will only give me the name of one section. What I really need (and I assume I'm not alone) is an array with the names of all the sections my page falls under. That way I could have a series of CSS files that load for a page, each one refining the previous.
For instance, if I have a section for /exhibits* and a section for /exhibits/permanent-exhibits/air-space*, then when I'm on the /exhibits/permanent-exhibits/air-space/mission-planets page my template could load both section-exhibits.css and section-air-space.css (in that order).

#1
Hmmm, that a very cool feature to have, I've done it a few times using themeName_proprocess_page and a fair amount of PHP, but would be very nice to have a module with a UI that could handle it.
#2
At this point I'd be happy if the Sections module just provided the variable to make my programming in theme_preprocess_page() easier. But if you want to get fancy, I could see adding the ability to attach a CSS file to each section within the UI, and a check-box to set whether it should include CSS files from all parent sections or just the one. You'd have to make sure that these files loaded after any theme based CSS files though, as they will most likely be overrides of the default theme styles (at least that's what I'm doing).
#3
You can add a stylesheet only theme with a base theme that adds only one CSS file. See minelli theme how this works. Sections is a theme switcher... aside there is a module named css* that allows you to add extra CSS per URL or page... cannot remember. Additional Sections DEV have the ability to have more template suggestions. By this way you are also able to archive your requirement. Additional to this it's very easy to add an additional class to the body tag... hmmmm - see YAML or Garland theme how this works. I hope this are enough variants... and there are much more...
#4
CSS module lets you add some custom CSS to a node, for styling that one node.
Sections module is a part solution but if you already have a subtheme then you need to add sub-subthemes which is problematic because the order the stylesheets load in is reversed for sub-subthemes (sub-subtheme stylesheets load before the subthemes...).
I've generally gone with adding body classes but this does not scale like the OP asks, the idea being able to feed a particular stylesheet to one section only, so you cut down on bandwidth and get really granular control over CSS management on large projects.
I tend to think the easiest method is just use drupal_add_css and code it yourself as a custom solution to the particular site you are working on, which is what I do on the big ones:)
#5
I'm not aware that sub-subtheme stylesheets load before the subthemes... I wonder how minelli CSS overriding could work then... I need to test myself.
#6
@hass, either this has been fixed or only happens with some themes, I've had it happen in the past but havent tested for a while, which I just did with Genesis and had no issues... which is great!
#7
If it don't work it could theoretically have something to do with system table weight and the theme name, but I have never tried it... the only idea I could come up is a sub-subtheme name is B and subtheme is C... but I'm only guessing. If sub-subtheme have a base theme of subtheme it should really work as the base theme should be executed first and then the sub-subtheme.
If I understood you correctly - the order issue is no longer an issue? That's fine...
#8
I looked at the CSS module, but as jmburnz points out in #4, this is a node specific solution, and I am definitely looking for a way to define sections within my site.
I also tried using sub-themes with the Sections module to add my style sheets, and the method did hold some promise, but ultimately it didn't work. To start with, creating sub-themes is an involved process and seems a little overboard, considering I'm just trying to add a single CSS file with one or two style declarations in it. But what ultimately did me in is that a bunch of my code broke. Anything using the path_to_theme() function to find it's assets was now pointing at the wrong theme.
My next step is to try using the template suggestions, but this seems like a messy workaround. My goal is to have a single page.tpl.php file. Now I will need several page templates, each of which will contain a drupal_add_css() function and then include my base page.tpl.php file. This means I will be using a template as a crutch to add business logic to my theme.
What I would like to be able to do is include functions in my template.php file like the following:
<?php
function theme_preprocess_page(&$vars, $hook) {
// Add a css file for the current section
if ($vars['section']) :
$cssFile = path_to_theme().'/css/section-'.$vars['section']['name'].'.css';
drupal_add_css($cssFile, 'theme', 'all', FALSE);
$vars['styles'] = drupal_get_css();
endif;
// Add a css files for the current section
// and all parent sections
if ($vars['sections']) :
foreach ($vars['sections'] as $section) {
$cssFile = path_to_theme().'/css/section-'.$section['name'].'.css';
drupal_add_css($cssFile, 'theme', 'all', FALSE);
}
$vars['styles'] = drupal_get_css();
endif;
}
?>
This capability seems consistent with the overall goals of the Sections module, so I am resubmitting it as a feature request. I believe that having these variables available would open the Sections module up to a wide variety of uses by crafty developers.
In addition to these variables, I would still love to see the UI enhanced to support CSS files, as I outlined in #2 above, but that is a much lower priority.