1 color.module color_css_alter(&$css)

Implements hook_css_alter().

Replaces style sheets with color-altered style sheets.

File

core/modules/color/color.module, line 173
Allows users to change the color scheme of themes.

Code

function color_css_alter(&$css) {
  global $theme_key;
  $preview_theme = _color_preview_theme();
  if ($preview_theme) {
    // Convert stylesheets for the preview.
    $theme_info = color_get_info($preview_theme);
    if ($theme_info) {
      $theme_path = backdrop_get_path('theme', $preview_theme);
      if (isset($_SESSION['color'][$theme_key])) {
        $preview_data = $_SESSION['color'][$theme_key];
        $paths = array(
          'source' => $theme_path . '/',
          'map' => array(),
        );
        foreach ($theme_info['css'] as $file) {
          $filename = _color_get_file_path($file, $preview_theme);
          $stylesheet = file_get_contents($filename);
          $paths['source'] = backdrop_get_path('theme', _color_get_theme_from_file($file, $preview_theme));
          $base = base_path() . dirname($filename) . '/';
          _backdrop_build_css_path(NULL, $base);

          $stylesheet = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_backdrop_build_css_path', $stylesheet);
          $stylesheet = _color_rewrite_stylesheet($preview_theme, $theme_info, $paths, $preview_data['palette'], $stylesheet);

          foreach ($css as $path => $data) {
            if ($path == $filename) {
              $css[$path]['type'] = 'inline';
              $css[$path]['preprocess'] = 'false';
              $css[$path]['data'] = $stylesheet;
              break;
            }
          }
        }
      }
    }
  }
  if (!isset($preview_data)) {
    // Override stylesheets.
    $color_paths = theme_get_setting('color.stylesheets');

    if (!empty($color_paths)) {
      $info = color_get_info($theme_key);

      // Get the original paths of the  files we are replacing.
      $old_paths = array();
      foreach ($info['css'] as $file) {
        // The files are already passed through backdrop_basename() in
        // color_get_info, so we don't need to do that here.
        $old_paths[$file] = _color_get_file_path($file, $theme_key);
      }

      // Replace the stylesheets if there are matching files.
      foreach ($color_paths as $color_path) {
        $basename = backdrop_basename($color_path);
        if (array_key_exists($basename, $old_paths)) {
          // Replace the path to the new css file.
          // This keeps the order of the stylesheets intact.
          $css[$old_paths[$basename]]['data'] = $color_path;
        }
      }
    }
  }
}