0

I have a theme which has multiple theme options in its settings page. Is there a way to assign particular theme settings to specific Drupal paths?

2
  • 1
    The theme system should be able to do that, but AFAIK, no contributed theme has this built-in. To do it, you need to create a subtheme of the theme - and hack the path dependencies it into that. Apr 7, 2015 at 16:31
  • Sounds like subtheme is my only option!
    – caustic
    Apr 7, 2015 at 17:04

2 Answers 2

0

For Drupal 7, you can do this by taking advantage of the fact that theme settings are cached by drupal_static(). See theme_get_setting(). I think the same approach would work for Drupal 8 too, but haven't tried it.

// Implement HOOK_init.
// Here we override one of the Bootstrap theme settings.
function MY_MODULE_init() {
  // Only do this if we're actually on the relevant page.
  if ($_GET['q'] == PATH_TO_THE_PAGE) {
    // Override the Bootstrap theme setting 'Smart form descriptions (via Tooltips)'.
    // This because we want to display form input #description as text, not tooltip.
    // This will also affect any other forms on the current page, no easy solution for that.

    // Set the name of the theme we're interested in (the current theme in this case).
    $theme_name = $GLOBALS['theme_key'];    
    // Read the setting first to ensure that the drupal_static cache is populated.
    $setting = theme_get_setting('bootstrap_forms_smart_descriptions', $theme_name);
    // Get a reference to the cached settings.
    $cached_settings = &drupal_static('theme_get_setting');
    // Unset the 'bootstrap_forms_smart_descriptions' setting from the cache.
    unset($cached_settings[$theme_name]['bootstrap_forms_smart_descriptions']);
  }
}

I should add, this is arguably a bit of a hack, as it depends on the internals of drupal_static() and theme_get_setting() i.e. not documented API functionality - unless you consider the code to be the documentation :)

-1

You can do this with the ThemeKey Module.

ThemeKey allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties. It can also be easily extended to support additional properties exposed by other modules. In combination with Drupal's theme inheritance you can easily achieve features like:

  • individually-styled channels
  • a front-page / "splash" screen
  • a date/time-selected Christmas theme
  • mobile themes for different auto-detected mobile devices
  • special themes for "limited" or "old" browsers
  • content, user, or role -specific themes
  • indicating your environment (production, staging, testing, sandbox, … )
  • testing your redesign safely on a live server

And unlike other theme switching modules, ThemeKey should play well with internal and external page caches, like Boost or Varnish, even for anonymous users.

2
  • 2
    I think the OP actually wants different settings for a single theme, depending on path, not theme switching. Apr 7, 2015 at 16:21
  • @AlfredArmstrong is correct - I don't want to switch themes, but rather assign specific settings of said theme based on different paths. The only way I can see this possible with ThemeKey would be to replicate the theme in question and rename it, giving each replicate its own settings, and then assign that way. Not good.
    – caustic
    Apr 7, 2015 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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