You are here

function styleswitcher_theme_styles in Style Switcher 8.2

Same name and namespace in other branches
  1. 6.2 styleswitcher.module \styleswitcher_theme_styles()
  2. 7.2 styleswitcher.module \styleswitcher_theme_styles()
  3. 3.0.x styleswitcher.module \styleswitcher_theme_styles()

Returns a list of styles provided by a theme.

Parameters

string $theme: Name of the theme to retrieve styles of.

string|null $original_theme: (optional) Name of the theme which the function was originally called with. While iterating over base themes the $theme argument changes but $original_theme keeps the same value.

Return value

array Array of styles. Keys are machine names and each element is a corresponding array of style properties: name, label and path. All properties are mandatory. See styleswitcher_style_load() for their descriptions.

See also

styleswitcher_style_load()

3 calls to styleswitcher_theme_styles()
styleswitcher_default_style_key in ./styleswitcher.module
Finds the default style and returns its key.
styleswitcher_styles_settings in ./styleswitcher.module
Returns a list of styles with theme-specific settings.
styleswitcher_style_load_multiple in ./styleswitcher.module
Returns a list of styles.

File

./styleswitcher.module, line 266
Module's hooks implementations and helper functions.

Code

function styleswitcher_theme_styles($theme, $original_theme = NULL) {
  $styles =& drupal_static(__FUNCTION__, []);

  // Theme can be an empty string if custom style is loading.
  if (!$theme) {
    return [];
  }
  if (isset($styles[$theme])) {
    return $styles[$theme];
  }
  $theme_styles = [];
  $theme_path = drupal_get_path('theme', $theme);
  $themes = Drupal::service('theme_handler')
    ->listInfo();
  $theme_info = isset($themes[$theme]) ? $themes[$theme]->info : [];
  if (!isset($original_theme)) {
    $original_theme = $theme;
  }

  // Search base themes for styleswitcher info.
  if (isset($theme_info['base theme'])) {
    $theme_styles = styleswitcher_theme_styles($theme_info['base theme'], $original_theme);
  }
  if (!empty($theme_info['styleswitcher'])) {
    $info = $theme_info['styleswitcher'];

    // Array of alternatives.
    if (!empty($info['css'])) {
      foreach ($info['css'] as $label => $path) {
        $name = 'theme/' . _styleswitcher_style_name($label);
        $theme_styles[$name] = [
          'name' => $name,
          'label' => $label,
          'path' => UrlHelper::isExternal($path) ? $path : $theme_path . '/' . $path,
        ];
      }
    }

    // Default style.
    if (isset($info['default'])) {
      $default = $info['default'];
    }
    elseif (isset($info['css']['default'])) {
      $default = $info['css']['default'];
      unset($theme_styles['theme/default']);
    }
    if (isset($default)) {
      $default_in_existing = FALSE;

      // Check if Default points to one of existing alternatives.
      foreach ($theme_styles as $name => $style) {
        if ($default == $style['label'] || $default == $style['path'] || $theme_path . '/' . $default == $style['path']) {
          styleswitcher_theme_default_style_key($original_theme, $name);
          $default_in_existing = TRUE;
          break;
        }
      }

      // Default is a path not mentioned in css array.
      if (!$default_in_existing) {
        $defaults['theme/default'] = [
          'name' => 'theme/default',
          'label' => 'Default',
          'path' => UrlHelper::isExternal($default) ? $default : $theme_path . '/' . $default,
        ];

        // Place default style above others.
        $theme_styles = $defaults + $theme_styles;
        styleswitcher_theme_default_style_key($original_theme, 'theme/default');
      }
    }
  }

  // Do not inflate the memory.
  if ($theme == $original_theme) {
    $styles[$theme] = $theme_styles;
  }
  return $theme_styles;
}