You are here

function _conditional_styles_theme in Conditional Stylesheets 6

When the theme registry is rebuilt, we also build the conditional stylesheets.

1 call to _conditional_styles_theme()
conditional_styles_theme in ./conditional_styles.module
Implements HOOK_theme().

File

./conditional_styles.theme.inc, line 34
Allows themes to add conditional stylesheets.

Code

function _conditional_styles_theme($existing, $type, $theme, $path) {

  // Process the conditional stylesheets for every active theme.
  $themes = list_themes();
  foreach (array_keys($themes) as $theme) {

    // We only need to process active themes.
    if ($themes[$theme]->status || $GLOBALS['theme'] == $theme) {
      $paths = conditional_styles_paths_to_basetheme($theme);

      // Grab all the conditional stylesheets.
      $stylesheets = array();

      // Start with the base theme and travel up the chain to the active theme.
      foreach ($paths as $theme_name => $path) {

        // Look at the conditional-stylesheets defined in the theme's .info file.
        if (!empty($themes[$theme_name]->info['conditional-stylesheets'])) {
          foreach ($themes[$theme_name]->info['conditional-stylesheets'] as $condition => $css) {

            // Allow the theme to override its base themes' styles.
            foreach ($css as $media => $files) {
              foreach ($files as $file) {
                $stylesheets[$condition][$media][$file] = $path;
              }
            }
          }
        }
      }

      // Render the stylesheets to link elements.
      $conditional_styles = $conditional_styles_rtl = '';
      if (!empty($stylesheets)) {
        $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
        $base_path = base_path();
        foreach ($stylesheets as $condition => $css) {

          // Each condition requires its own set of links.
          $output = $output_rtl = '';
          foreach ($css as $media => $files) {
            foreach ($files as $file => $path) {

              // Don't allow non-existent stylesheets to clutter the logs with 404.
              if (file_exists("./{$path}/{$file}")) {
                $link = "<link type=\"text/css\" rel=\"stylesheet\" media=\"{$media}\" href=\"{$base_path}{$path}/{$file}{$query_string}\" />\n";
                $output .= $link;
                $output_rtl .= $link;
                $file_rtl = str_replace('.css', '-rtl.css', $file);
                if (file_exists("./{$path}/{$file_rtl}")) {
                  $output_rtl .= "<link type=\"text/css\" rel=\"stylesheet\" media=\"{$media}\" href=\"{$base_path}{$path}/{$file_rtl}{$query_string}\" />\n";
                }
              }
            }
          }
          if ($output) {
            $conditional_styles .= "<!--[{$condition}]>\n{$output}<![endif]-->\n";
            $conditional_styles_rtl .= "<!--[{$condition}]>\n{$output_rtl}<![endif]-->\n";
          }
        }
      }

      // Save the stylesheets for later retrieval.
      if ($conditional_styles) {
        if (db_is_active()) {
          variable_set('conditional_styles_' . $theme, $conditional_styles);
          variable_set('conditional_styles_' . $theme . '_rtl', $conditional_styles_rtl);
        }
        else {
          $GLOBALS['conf']['conditional_styles_' . $theme] = $conditional_styles;
          $GLOBALS['conf']['conditional_styles_' . $theme . '_rtl'] = $conditional_styles_rtl;
        }
      }
      elseif (db_is_active()) {
        variable_del('conditional_styles_' . $theme);
        variable_del('conditional_styles_' . $theme . '_rtl');
      }
    }
  }

  // Return nothing.
  return array();
}