From a01d869a2aa7a82ca466987e09d6c52dd239a3be Mon Sep 17 00:00:00 2001
From: Marc van Gend <info@marcvangend.nl>
Date: Sat, 4 May 2013 00:47:06 +0200
Subject: [PATCH] Issue #1961636 by aaronbauman, marcvangend | ttronslien:
 Fixed block delta value lengths grow out of control.

---
 fieldblock.install | 39 ++++++++++++++++-----------------------
 fieldblock.module  | 16 ++++++++++++++--
 2 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/fieldblock.install b/fieldblock.install
index 7144255..b9afca3 100644
--- a/fieldblock.install
+++ b/fieldblock.install
@@ -1,31 +1,9 @@
 <?php
 
-/**
- * Implements hook_install().
- * Double the maximum length of the delta fields because fieldblock delta's
- * can easily get longer than 32 characters.
- */
-function fieldblock_install() {
-  _fieldblock_db_alter_block_delta_length(64);
-}
-
-/**
- * Implements hook_schema_alter().
- * Informs Drupal about the fact that we have altered the database.
- */
-function fieldblock_schema_alter(&$schema) {
-  $schema['block']['fields']['delta']['length'] = 64;
-  $schema['block_role']['fields']['delta']['length'] = 64;
-  $schema['block_node_type']['fields']['delta']['length'] = 64;
-}
-
 /**
  * Implements hook_uninstall().
  */
 function fieldblock_uninstall() {
-  // Restore database alterations.
-  _fieldblock_db_alter_block_delta_length(32);
-
   // Delete variables.
   $entities = entity_get_info();
   // Loop over the entity types.
@@ -45,6 +23,9 @@ function fieldblock_uninstall() {
   }
 }
 
+/**
+ * Legacy helper function to undo drupal core schema alter.
+ */
 function _fieldblock_db_alter_block_delta_length($length) {
   // Alter block table.
   db_drop_unique_key('block', 'tmd');
@@ -90,4 +71,16 @@ function _fieldblock_db_alter_block_delta_length($length) {
       'primary key' => array('module', 'delta', 'type'),
     )
   );
-}
\ No newline at end of file
+}
+
+/**
+ * Update legacy fieldblock deltas to use md5 identifier.
+ * Reset drupal core block schema.
+ */
+function fieldblock_update_7100() {
+  $blocks = db_query("SELECT bid, delta FROM {block} WHERE module = 'fieldblock'");
+  foreach ($blocks as $block) {
+    db_query("UPDATE {block} SET delta = :new_delta WHERE bid = :bid AND delta = :old_delta AND module = 'fieldblock'", array(':new_delta' => md5($block->delta), ':bid' => $block->bid, ':old_delta' => $block->delta));
+  }
+  _fieldblock_db_alter_block_delta_length(32);
+}
diff --git a/fieldblock.module b/fieldblock.module
index 3829595..6e0a735 100644
--- a/fieldblock.module
+++ b/fieldblock.module
@@ -116,7 +116,7 @@ function fieldblock_get_block_list() {
           // Loop over the fields defined in the variable.
           foreach ($fieldblock_settings as $field_name => $field_label) {
             // Build the fieldblock info.
-            $fieldblock_id = $variable_name .'-'. $field_name;
+            $fieldblock_id = md5($variable_name .'-'. $field_name);
             $fieldblocks[$fieldblock_id] = t('@field field (from @type: @bundle: @view_mode)', array(
               '@field' => $instances[$entity_type][$bundle][$field_name]['label'],
               '@type' => $entity_type,
@@ -149,6 +149,7 @@ function fieldblock_block_view($delta = '') {
       $fieldblocks_storage[$delta]['#label_display'] = 'hidden';
     }
     $block['content'] = $fieldblocks_storage[$delta];
+    $block['fieldblock_name'] = $fieldblocks_storage[$delta]['fieldblock_name'];
   }
 
   return $block;
@@ -184,9 +185,20 @@ function fieldblock_field_attach_view_alter(&$output, $context) {
   // and store the field's render array for later use.
   foreach ($fieldblock_settings as $field_name) {
     if (isset($output[$field_name])) {
-      $fieldblock_id = $variable_name .'-'. $field_name;
+      $fieldblock_name = $variable_name .'-'. $field_name;
+      $fieldblock_id = md5($fieldblock_name);
       $fieldblocks_storage[$fieldblock_id] = $output[$field_name];
       hide($output[$field_name]);
+      $fieldblocks_storage[$fieldblock_id]['fieldblock_name'] = $fieldblock_name;
     }
   }
 }
+
+/**
+ * Give fieldblocks a meaningful html id in spite of the hashed block deltas.
+ */
+function fieldblock_preprocess_block(&$variables) {
+  if ($variables['block']->module == 'fieldblock') {
+    $variables['block_html_id'] = drupal_html_id('block-' . $variables['block']->fieldblock_name);
+  }
+}
-- 
GitLab