From 4936f4df125a72405d5713a4f677ef49cd831ed4 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Tue, 22 Aug 2006 07:43:33 +0000
Subject: [PATCH] - Patch #76666 by hunmonk: custom block titles.

---
 CHANGELOG.txt                 |  1 +
 modules/block/block.module    | 53 +++++++++++++++++++----------------
 modules/system/system.install | 32 ++++++++++++++++++++-
 3 files changed, 61 insertions(+), 25 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 0d411f2c42a7..95b05a041d4e 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -19,6 +19,7 @@ Drupal x.x.x, xxxx-xx-xx (development version)
     * reorganized the settings pages.
 - block system:
     * extended the block visibility settings with a role specific settings.
+    * made it possible to customize all block titles.
 - poll module:
     * optionally allow people to inspect all votes.
     * optionally allow people to cancel their vote.
diff --git a/modules/block/block.module b/modules/block/block.module
index 670a4b81032b..49c7046b8f41 100644
--- a/modules/block/block.module
+++ b/modules/block/block.module
@@ -122,7 +122,7 @@ function block_block($op = 'list', $delta = 0, $edit = array()) {
     case 'list':
       $blocks = array();
 
-      $result = db_query('SELECT bid, title, info FROM {boxes} ORDER BY title');
+      $result = db_query("SELECT bid, title, info FROM {boxes} bx INNER JOIN {blocks} bl ON bx.bid = bl.delta WHERE module = 'block' ORDER BY title");
       while ($block = db_fetch_object($result)) {
         $blocks[$block->bid]['info'] = $block->info ? check_plain($block->info) : check_plain($block->title);
       }
@@ -141,7 +141,6 @@ function block_block($op = 'list', $delta = 0, $edit = array()) {
 
     case 'view':
       $block = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid = %d', $delta));
-      $data['subject'] = check_plain($block->title);
       $data['content'] = check_markup($block->body, $block->format, FALSE);
       return $data;
   }
@@ -180,10 +179,11 @@ function _block_rehash() {
           $block['pages'] = $old_blocks[$module][$delta]->pages;
           $block['custom'] = $old_blocks[$module][$delta]->custom;
           $block['throttle'] = $old_blocks[$module][$delta]->throttle;
+          $block['title'] = $old_blocks[$module][$delta]->title;
         }
         // Otherwise, use any set values, or else substitute defaults.
         else {
-          $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
+          $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0, 'title' => '');
           foreach ($properties as $property => $default) {
             if (!isset($block[$property])) {
               $block[$property] = $default;
@@ -202,7 +202,7 @@ function _block_rehash() {
 
   // Reinsert new set of blocks into table.
   foreach ($blocks as $block) {
-    db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']);
+    db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle, title) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d, '%s')", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle'], $block['title']);
   }
   db_unlock_tables();
 
@@ -367,7 +367,7 @@ function theme_block_admin_display($form) {
 }
 
 function block_box_get($bid) {
-  return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
+  return db_fetch_array(db_query("SELECT bx.*, title FROM {boxes} bx INNER JOIN {blocks} bl ON bx.bid = bl.delta WHERE module = 'block' AND bid = %d", $bid));
 }
 
 /**
@@ -378,16 +378,25 @@ function block_admin_configure($module = NULL, $delta = 0) {
   $form['module'] = array('#type' => 'value', '#value' => $module);
   $form['delta'] = array('#type' => 'value', '#value' => $delta);
 
-  $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
+  $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
+
+  $form['block_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Block specific settings'),
+    '#collapsible' => TRUE,
+  );
+  $form['block_settings']['title'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Block title'),
+    '#maxlength' => 64,
+    '#description' =>  $module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>&lt;none&gt;</em> to display no title, or leave blank to use the default block title.'),
+    '#default_value' => $edit['title'],
+    '#weight' => -18,
+  );
+
 
   // Module-specific block configurations.
   if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
-    $form['block_settings'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Block specific settings'),
-      '#collapsible' => TRUE,
-    );
-
     foreach ($settings as $k => $v) {
       $form['block_settings'][$k] = $v;
     }
@@ -398,7 +407,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
   drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
 
   // Standard block configurations.
-
   $form['user_vis_settings'] = array(
     '#type' => 'fieldset',
     '#title' => t('User specific visibility settings'),
@@ -492,7 +500,7 @@ function block_admin_configure_validate($form_id, $form_values) {
 
 function block_admin_configure_submit($form_id, $form_values) {
   if (!form_get_errors()) {
-    db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['module'], $form_values['delta']);
+    db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['title'], $form_values['module'], $form_values['delta']);
     db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_values['module'], $form_values['delta']);
     foreach (array_filter($form_values['roles']) as $rid) {
       db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_values['module'], $form_values['delta']);
@@ -550,14 +558,6 @@ function block_box_form($edit = array()) {
     '#required' => TRUE,
     '#weight' => -19,
   );
-  $form['title'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Block title'),
-    '#default_value' => $edit['title'],
-    '#maxlength' => 64,
-    '#description' => t('The title of the block as shown to the user.'),
-    '#weight' => -18,
-  );
   $form['body_filter']['#weight'] = -17;
   $form['body_filter']['body'] = array(
     '#type' => 'textarea',
@@ -579,10 +579,10 @@ function block_box_save($edit, $delta = NULL) {
   }
 
   if (isset($delta)) {
-    db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
+    db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta);
   }
   else {
-    db_query("INSERT INTO {boxes} (title, body, info, format) VALUES  ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
+    db_query("INSERT INTO {boxes} (body, info, format) VALUES  ('%s', '%s', %d)", $edit['body'], $edit['info'], $edit['format']);
   }
   return TRUE;
 }
@@ -690,6 +690,11 @@ function block_list($region) {
           }
         }
         if (isset($block->content) && $block->content) {
+          // Override default block title if a custom display title is present.
+          if ($block->title) {
+            // Check plain here to allow module generated titles to keep any markup.
+            $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
+          }
           $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
         }
       }
diff --git a/modules/system/system.install b/modules/system/system.install
index b8561783bfd0..ef59aef5ee54 100644
--- a/modules/system/system.install
+++ b/modules/system/system.install
@@ -32,7 +32,8 @@ function system_install() {
         custom tinyint DEFAULT '0' NOT NULL,
         throttle tinyint DEFAULT '0' NOT NULL,
         visibility tinyint DEFAULT '0' NOT NULL,
-        pages text DEFAULT '' NOT NULL
+        pages text DEFAULT '' NOT NULL,
+        title varchar(64) DEFAULT '' NOT NULL
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
 
       db_query("CREATE TABLE {boxes} (
@@ -3047,6 +3048,35 @@ function system_update_1005() {
   return $ret;
 }
 
+function system_update_1006() {
+  // Add a customizable title to all blocks.
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+    $ret[] = update_sql("ALTER TABLE {blocks} ADD title VARCHAR(64) NOT NULL DEFAULT ''");
+      break;
+    case 'pgsql':
+      db_add_column($ret, 'blocks', 'title', 'varchar(64)', array('default' => '', 'not null' => TRUE));
+      break;
+  }
+  // Migrate custom block titles to new column.
+  $boxes = db_query('SELECT bid, title from {boxes}');
+  while ($box = db_fetch_object($boxes)) {
+    db_query("UPDATE {blocks} SET title = '%s' WHERE delta = %d and module = 'block'", $box->title, $box->bid);
+  }
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql('ALTER TABLE {boxes} DROP title');
+      break;
+    case 'pgsql':
+      $ret[] = update_sql('ALTER TABLE {boxes} DROP COLUMN title');
+      break;
+  }
+  return $ret;
+}
+
 /**
  * @} End of "defgroup updates-4.7-to-x.x"
  * The next series of updates should start at 2000.
-- 
GitLab