From 583ec546711dccf8fc07871ce280856e3a1739ce Mon Sep 17 00:00:00 2001
From: Kyle Taylor <kyletaylored@gmail.com>
Date: Sun, 2 Nov 2014 16:59:13 -0600
Subject: [PATCH] Add option for appending existing block classes

- block_class 7.x-1.x, this will add the class to the block in the Manage Blocks display. Block Class will add it's global class either way when loading normally.
- block_class 7.x-2.x, Blocker actually replaces the CSS classes from block_class instead of adding to them. This way we can actually choose if we want to append them or not.
---
 blocker.admin.inc            | 10 +++++++
 blocker.install              | 42 +++++++++++++++++++++++++-
 blocker.module               | 71 ++++++++++++++++++++++++++++++++++++++------
 modules/blocker_ui.admin.inc | 13 +++++++-
 4 files changed, 125 insertions(+), 11 deletions(-)

diff --git a/blocker.admin.inc b/blocker.admin.inc
index bd6d457..8857ebc 100644
--- a/blocker.admin.inc
+++ b/blocker.admin.inc
@@ -8,6 +8,16 @@
 function blocker_admin_settings() {
   $theme = variable_get('theme_default', 'bartik');
 
+  if (module_exists('block_class')) {
+    $form['blocker_block_class_version'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Block Class major version'),
+      '#description' => t('The current version of the Block Class module. Some functionality depends on the version.'),
+      '#default_value' => variable_get('blocker_block_class_version', blocker_block_class_version()),
+      '#disabled' => TRUE,
+    );
+}
+
   $form['blocker_regions'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Available regions'),
diff --git a/blocker.install b/blocker.install
index c043af7..1bcefe0 100644
--- a/blocker.install
+++ b/blocker.install
@@ -64,6 +64,13 @@ function blocker_schema() {
         'default' => '',
         'not null'    => TRUE,
       ),
+      'block_class_check' => array(
+        'description' => 'Check to use existing CSS classes.',
+        'type'        => 'int',
+        'size'        => 'tiny',
+        'not null'    => TRUE,
+        'default'     => 0,
+      ),
       'row' => array(
         'type'        => 'varchar',
         'length'      => 255,
@@ -98,12 +105,20 @@ function blocker_schema() {
 }
 
 /**
+ * Implements hook_install()
+ */
+function blocker_install() {
+  blocker_block_class_version();
+}
+
+/**
  * Implements hook_uninstall()
  */
 function blocker_uninstall() {
   $variables = array(
     'blocker_regions',
     'blocker_blocks',
+    'blocker_block_class_version',
   );
   foreach($variables as $variable) {
     variable_del($variable);
@@ -134,4 +149,29 @@ function blocker_update_7100() {
     )
   );
   
-}
\ No newline at end of file
+}
+
+/**
+ * Add option to append existing block classes .
+ */
+function blocker_update_7101() {
+  // Add fields for block_class module.
+  db_add_field('blocker', 'block_class_check',
+    array(
+      'type'        => 'int',
+      'size'        => 'tiny',
+      'not null'    => TRUE,
+      'default'     => 0,
+      'description' => 'Check to use existing CSS classes.',
+    )
+  );
+
+  // Get current major version of block_class
+  if (module_exists('block_class')) {
+    $info = drupal_parse_info_file(drupal_get_path('module', 'block_class') . '/block_class.info');
+    $version = explode('-', $info['version']);
+    $current_version = intval($version[1]);
+    variable_set('blocker_block_class_version', $current_version);
+  }
+  
+}
diff --git a/blocker.module b/blocker.module
index 2cf64f2..ca6c058 100644
--- a/blocker.module
+++ b/blocker.module
@@ -39,6 +39,9 @@ function blocker_get_blocks_list($region, $object, $account) {
 
   // Unset static cache as it is storing the wrong results.
   drupal_static_reset('block_list');
+
+  // Get block_class version
+  $bc_version = variable_get('blocker_block_class_version',blocker_block_class_version());
   
   // Now add in blocks from this module.
   // We only want to display the blocks on the main entity page.
@@ -52,14 +55,28 @@ function blocker_get_blocks_list($region, $object, $account) {
       if ($blocker->title) {
         $block->title = $blocker->title;
       }
-      $block->css_class = $blocker->css_class;
+      
+      // Append existing block_class if checked.
+      if ($blocker->block_class_check) {
+        if ($bc_version < 2) {
+          // If using version 1, use the function.
+          $block->css_class = blocker_block_class($block, $blocker);
+        } else {
+          // Or just append the classes.
+          $block_class = (isset($block->css_class)) ? $block->css_class : '';
+          $block->css_class = trim($block_class.' '.$blocker->css_class);
+        }
+      } else {
+        // Replace the class. In block_class 7x-1.x, it will add it's block class regardless.
+        $block->css_class = $blocker->css_class;
+      }
+
       $block->row = $blocker->row;
       $block->row_class = $blocker->row_class;
       $block->brid = $blocker->brid; // Save off brid to use later for edit link.
       $blocks[$block->module . '_' . $block->delta] = $block;
     }
   }
-  
   return $blocks;
 }
 
@@ -95,14 +112,31 @@ function blocker_page_build(&$page) {
       $theme = variable_get('theme_default', 'bartik');
       $object_ids = entity_extract_ids($object->entity_type, $object);
       $blockers = blocker_get_blocks($object->entity_type, $object_ids[0], $theme);
+      // Get block_class version
+      $bc_version = variable_get('blocker_block_class_version',blocker_block_class_version());
       // Load in the block information by region.
       $blocks = array();
       foreach($blockers as $brid => $blocker) {
         $block = block_load($blocker->module, $blocker->delta);
         $block->weight = $blocker->weight;
-        $block->css_class = $blocker->css_class;
         $block->row = $blocker->row;
         $block->row_class = $blocker->row_class;
+        
+        // Append existing block_class if checked.
+        if ($blocker->block_class_check) {
+          if ($bc_version < 2) {
+            // If using version 1, use the function.
+            $block->css_class = blocker_block_class($block, $blocker);
+          } else {
+            // Or just append the classes.
+            $block_class = (isset($block->css_class)) ? $block->css_class : '';
+            $block->css_class = trim($block_class.' '.$blocker->css_class);
+          }
+        } else {
+          // Replace the class. In block_class 7x-1.x, it will add it's block class regardless.
+          $block->css_class = $blocker->css_class;
+        }
+
         if ($blocker->title == '<none>') {
           $block->title = '';
         }
@@ -313,11 +347,30 @@ function blocker_form_block_admin_configure_alter(&$form, &$form_state, $form_id
  */
 function blocker_preprocess_block(&$variables, $hook) {
   $block = &$variables['block'];
-  if (isset($block->css_class)) {
-    foreach(explode(' ' , $block->css_class) as $class) {
-      if (!in_array($class, $variables['classes_array'])) {
-        $variables['classes_array'][] = $class;
-      }
-    }
+  // Use class application from block_class module
+  if (isset($block->css_class) && $block->css_class != '') {
+    $variables['classes_array'] = array_merge($variables['classes_array'], explode(' ', $block->css_class));
+  }
+}
+
+/**
+ * Get current major version of the block_class module.
+ */
+function blocker_block_class_version() {
+  if (module_exists('block_class')) {
+    // Get current major version of block_class
+    $info = drupal_parse_info_file(drupal_get_path('module', 'block_class') . '/block_class.info');
+    $version = explode('-', $info['version']);
+    $current_version = intval($version[1]);
+    variable_set('blocker_block_class_version', $current_version);
+    return $current_version;
   }
 }
+
+/**
+ * Helper function for adding classes depending on the version of block_class.
+ */
+function blocker_block_class(&$block, &$blocker) {
+    module_load_include('module', 'block_class');
+    return trim(block_class($block).' '.$blocker->css_class);
+}
diff --git a/modules/blocker_ui.admin.inc b/modules/blocker_ui.admin.inc
index 8ff9e4e..fe8435d 100644
--- a/modules/blocker_ui.admin.inc
+++ b/modules/blocker_ui.admin.inc
@@ -80,13 +80,24 @@ function blocker_ui_edit_form($form, $form_state, $blocker) {
   );
   
   if (module_exists('block_class')) {
-    $form['css_class'] = array(
+    $form['block_class'] = array(
+      '#type' => 'fieldset',
+      '#collapsible' => FALSE,
+      '#collapsed' => FALSE,
+    );
+    $form['block_class']['css_class'] = array(
       '#type' => 'textfield',
       '#title' => t('CSS class(es)'),
       '#default_value' => isset($blocker->css_class) ? $blocker->css_class : '',
       '#description' => t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.'),
       '#maxlength' => 255,
     );
+    $form['block_class']['block_class_check'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Append existing CSS class(es)'),
+      '#default_value' => isset($blocker->block_class_check) ? $blocker->block_class_check : 0,
+      '#description' => t('If this block has existing CSS classes, you can choose to have them appended.'),
+    );
   }
   
   if (module_exists('block_row')) {
-- 
1.9.3 (Apple Git-50)

