Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

/**
 * A more flexible version of core's semi-broken theme_get_setting().
 * 
 * The setting is obtained from the first value found in the following sources:
 * - the saved values from the theme's settings form
 * - the saved values from the global theme settings form
 * - the default theme-specific settings defined in the theme's .info file
 *
 * The list above will cycle through each connected sub-theme to their parents
 * when the $get parameter is set to 'cascade'. theme_get_setting() is designed
 * to fall back automatically but it is broken.
 * 
 * @see theme_get_settings()
 *  http://api.drupal.org/api/search/7/theme_get_setting
 * @see issue: inconsistent theme data
 *  http://drupal.org/node/761608
 *
 * @param $setting_name
 *  The name of the setting to be retrieved.
 * @param $get
 *  (string) Can be a theme key which will restricted the setting to a
 *  specific theme. Using 'cascade' will check each theme until a value is
 *  found with sub-themes taking precedence. This value defaults to the active
 *  theme.
 */
function theme_setting($setting_name, $get = NULL) {
  $cache = &drupal_static(__FUNCTION__, array());

  if (!isset($get)) {
    $get = $GLOBALS['theme_key'];
  }

  if (empty($cache)) {
    $themes = list_themes();
    $active_theme  = $GLOBALS['theme_key'];

    $active_themes = array();
    while (isset($active_theme)) {
      $active_themes[$active_theme] = $active_theme;
      $active_theme = isset($themes[$active_theme]->base_theme) ? $themes[$active_theme]->base_theme : NULL;
    }
    
    foreach ($active_themes as $theme) {
      $settings = variable_get('theme_' . $theme . '_settings', array());
      $settings += variable_get('theme_settings', array());
      $settings += isset($themes[$theme]->info['settings']) ? $themes[$theme]->info['settings'] : array();

      foreach ($settings as $setting => $value) {
        $cache["$theme:$setting"] = $value;
        if (!isset($cache["cascade:$setting"]) && isset($cache["$theme:$setting"])) {
          $cache["cascade:$setting"] = $cache["$theme:$setting"];
        }
      }
    }
  }

  return isset($cache["$get:$setting_name"]) ? $cache["$get:$setting_name"] : NULL;
}