From 0e0b909fec155bf828387cd680d4d6a8048dc0d1 Mon Sep 17 00:00:00 2001
From: Mike Shiyan <dev@mike.pp.ua>
Date: Fri, 16 Jul 2015 00:52:13 +0300
Subject: [PATCH] Issue #2536288 by pingwin4eg: Remove checking if
 (styleswitcher_list())

---
 styleswitcher.admin.inc | 38 +++++++++++--------------
 styleswitcher.module    | 76 +++++++++++++++++++++++--------------------------
 2 files changed, 51 insertions(+), 63 deletions(-)

diff --git a/styleswitcher.admin.inc b/styleswitcher.admin.inc
index a6d7515..72175af 100644
--- a/styleswitcher.admin.inc
+++ b/styleswitcher.admin.inc
@@ -86,27 +86,23 @@ function styleswitcher_admin($form, &$form_state) {
  */
 function styleswitcher_admin_validate($form, &$form_state) {
   $values = &$form_state['values']['styleswitcher_styles'];
+  $styles = styleswitcher_list(TRUE);
 
-  if ($styles = styleswitcher_list(TRUE)) {
-    foreach ($styles as $name => $style) {
-      if ($name == $values['default'] || $name == styleswitcher_default_style_key()) {
-        // Automatically enable the default style and the style which was
-        // default previously because we will not get the value from that
-        // disabled checkbox.
-        $values['enabled'][$name] = 1;
-      }
-
-      $styles[$name]['weight'] = $values['weight'][$name];
-      $styles[$name]['status'] = !empty($values['enabled'][$name]);
-      $styles[$name]['is_default'] = ($values['default'] == $name);
+  foreach (array_keys($styles) as $name) {
+    if ($name == $values['default'] || $name == styleswitcher_default_style_key()) {
+      // Automatically enable the default style and the style which was default
+      // previously because we will not get the value from that disabled
+      // checkbox.
+      $values['enabled'][$name] = 1;
     }
 
-    uasort($styles, 'drupal_sort_weight');
-  }
-  else {
-    $styles = NULL;
+    $styles[$name]['weight'] = $values['weight'][$name];
+    $styles[$name]['status'] = !empty($values['enabled'][$name]);
+    $styles[$name]['is_default'] = ($values['default'] == $name);
   }
 
+  uasort($styles, 'drupal_sort_weight');
+
   form_set_value($form['styleswitcher_styles'], $styles, $form_state);
 }
 
@@ -431,20 +427,18 @@ function _styleswitcher_weight_delta() {
  *   (optional) Function to call on array of styles weights. Defaults to 'max'.
  *
  * @return mixed
- *   A result of $function call or FALSE if there is no styles.
+ *   A result of $function call.
  */
 function _styleswitcher_weight_max($function = 'max') {
-  $weights = &drupal_static(__FUNCTION__ . ':weights');
-
-  if (!isset($weights)) {
-    $weights = array();
+  $weights = &drupal_static(__FUNCTION__ . ':weights', array());
 
+  if (!$weights) {
     foreach (styleswitcher_list(TRUE) as $style) {
       $weights[] = $style['weight'];
     }
   }
 
-  return $weights ? $function($weights) : FALSE;
+  return $function($weights);
 }
 
 /**
diff --git a/styleswitcher.module b/styleswitcher.module
index b49cfb1..7e84077 100644
--- a/styleswitcher.module
+++ b/styleswitcher.module
@@ -99,33 +99,31 @@ function styleswitcher_block_view($delta) {
 }
 
 /**
- * Prepares variables for html templates.
+ * Prepares variables for html templates: Adds the dynamic CSS to every page.
  *
  * Default template: html.tpl.php.
  *
  * @param array $variables
- *   An associative array with html variables.
+ *   An associative array with html variables. Not used in this preprocessor.
  */
 function styleswitcher_preprocess_html(array &$variables) {
-  if (styleswitcher_list(TRUE)) {
-    // Use drupal_add_css() instead of drupal_add_html_head_link() because we
-    // need our alternative stylesheets to overwrite the most other css on the
-    // page and in standard html.tpl.php $styles go after $head. Besides this
-    // method properly handle hrefs using file_create_url().
-    drupal_add_css('styleswitcher/css', array(
-      'basename' => 'styleswitcher.active.css',
-      // Use the latest standard group to be after the most of other css.
-      // Some themes (like Omega) use even latter groups to set their grid
-      // layouts hoping they wouldn't be overridden.
-      'group' => CSS_THEME,
-      'weight' => PHP_INT_MAX,
-      // Set 'media' to module name to quickly find our link among other
-      // elements in styleswitcher_pre_render_styles().
-      'media' => 'styleswitcher',
-      // Don't aggregate or compress our css.
-      'preprocess' => FALSE,
-    ));
-  }
+  // Use drupal_add_css() instead of drupal_add_html_head_link() because we need
+  // our alternative stylesheets to overwrite the most other css on the page and
+  // in standard html.tpl.php $styles go after $head. Besides, this method
+  // properly handles hrefs using file_create_url().
+  drupal_add_css('styleswitcher/css', array(
+    'basename' => 'styleswitcher.active.css',
+    // Use the latest standard group to be after the most of other css. Some
+    // themes (like Omega) use even latter groups to set their grid layouts
+    // hoping they wouldn't be overridden.
+    'group' => CSS_THEME,
+    'weight' => PHP_INT_MAX,
+    // Set 'media' to module name to quickly find this link among other elements
+    // in styleswitcher_pre_render_styles().
+    'media' => 'styleswitcher',
+    // Don't aggregate or compress the dynamic CSS.
+    'preprocess' => FALSE,
+  ));
 }
 
 /**
@@ -219,26 +217,22 @@ function styleswitcher_element_info_alter(&$types) {
  * styleswitcher_element_info_alter().
  */
 function styleswitcher_pre_render_styles(array $elements) {
-  // Check if elements not empty & if it contains what we're looking for at all.
-  // It is hard to walk through indexed array to understand is there our link
-  // so just check if we have anything to linking to. If it is then there must
-  // be at least one element in array.
-  $styles = styleswitcher_list(TRUE);
-  if (!empty($styles)) {
-    // Walk in reverse order 'cause our <link> is closer to an end of array.
-    $element = end($elements);
-    do {
-      if (!empty($element['#attributes']['media']) && $element['#attributes']['media'] == 'styleswitcher') {
-        // Set 'media' back to its default value.
-        $element['#attributes']['media'] = 'all';
-        // Add an ID.
-        $element['#attributes']['id'] = 'styleswitcher-css';
-
-        $elements[key($elements)] = $element;
-        break;
-      }
-    } while ($element = prev($elements));
-  }
+  // There must be at least one element in array because the dynamic CSS is
+  // always added to the page's styles. Walk in reverse order because the needed
+  // <link> is closer to end of array.
+  $element = end($elements);
+
+  do {
+    if (!empty($element['#attributes']['media']) && $element['#attributes']['media'] == 'styleswitcher') {
+      // Set 'media' back to its default value.
+      $element['#attributes']['media'] = 'all';
+      // Add an ID.
+      $element['#attributes']['id'] = 'styleswitcher-css';
+
+      $elements[key($elements)] = $element;
+      break;
+    }
+  } while ($element = prev($elements));
 
   return $elements;
 }
-- 
1.9.5.msysgit.0

