From 14514bdf0033e02a902ab10c590fcdfba264db86 Mon Sep 17 00:00:00 2001
From: Eric Alvares <erica@344430.no-reply.drupal.org>
Date: Tue, 22 Mar 2011 03:24:07 +0100
Subject: [PATCH] Issue #1099546 by Eric_A: Moved image resource knowledge out of template_preprocess_chessboard().

---
 chessboard.module     |   74 +++++++++++++++++++++++++++++++++++++++++++++++++
 chessboard_render.php |   73 +-----------------------------------------------
 2 files changed, 75 insertions(+), 72 deletions(-)

diff --git a/chessboard.module b/chessboard.module
index 31cb583..e5436e5 100644
--- a/chessboard.module
+++ b/chessboard.module
@@ -1,4 +1,35 @@
 <?php
+function chessboard_border_filename($border) {
+  // $border: T, B, L, R, TL, TR, BL, BR
+  switch ($border) {
+    case 'T':
+    case 'B':
+      return 'h.png';
+    case 'L':
+    case 'R':
+      return 'v.png';
+    default:
+      return 'c.png';
+  }
+}
+
+function chessboard_piece_filename($piece, $square_color) {
+  // $piece        : KQBNRPkqbnrp-
+  // $square_color : 0 1
+  switch ($piece) {
+    case 'x':
+      $name = 'xx';
+      break;
+    case '-':
+      $name = '';
+      break;
+    default:
+      $name = strtolower($piece) . (ctype_lower($piece) ? 'd' : 'l');
+      break;
+  }
+
+  return $name . ($square_color ? 'd' : 'l') . '.png';
+}
 
 /**
  * Implements hook_help().
@@ -134,6 +165,30 @@ function chessboard_process_chessboard_notation($elements) {
       $file++;
     }
   }
+
+  $image_directory_path = $elements['#image_directory_path'];
+  $size_attributes = $elements['#dimensions'];
+  $groups = array('square', 'border-h', 'border-v', 'border-c');
+  $test_paths = array(
+    'square' => $image_directory_path . '/' . chessboard_piece_filename('-', 0),
+    'border-h' => $image_directory_path . '/' . chessboard_border_filename('T'),
+    'border-v' => $image_directory_path . '/' . chessboard_border_filename('R'),
+    'border-c' => $image_directory_path . '/' . chessboard_border_filename('TR'),
+  );
+  foreach ($test_paths as $group => $test_path) {
+    if (!isset($size_attributes[$group]) && is_file($test_path) && (list($width, $height) = @getimagesize($test_path))) {
+      $size_attributes[$group] = array(
+        'width' => $width,
+        'height' => $height,
+      );
+    }
+  }
+  foreach ($groups as $group) {
+    if (!isset($size_attributes[$group])) {
+      $size_attributes[$group] = array();
+    }
+  }
+  $elements['#dimensions'] = $size_attributes;
   return $elements;
 }
 
@@ -339,8 +394,27 @@ function _chessboard_filter_formatter_view($items) {
           drupal_get_path('module', 'chessboard') . '/chessboard.css' => array('media' => 'all', 'preprocess' => FALSE),
         ),
       ),
+      '#dimensions' => array(
+        'square' => array(
+          'width' => 40,
+          'height' => 40,
+        ),
+        'border-h' => array(
+          'width' => 40,
+          'height' => 4,
+        ),
+        'border-v' => array(
+          'width' => 4,
+          'height' => 40,
+        ),
+        'border-c' => array(
+          'width' => 4,
+          'height' => 4,
+        ),
+      ),
       '#pre_render' => array('chessboard_process_chessboard_notation'),
       '#theme' => 'chessboard',
+      '#image_directory_path' => drupal_get_path('module', 'chessboard') . '/default',
       '#item' => $item,
       '#prefix' => '<div class="chessboard">',
       '#suffix' => '</div>',
diff --git a/chessboard_render.php b/chessboard_render.php
index acc0eb8..ffdf84b 100644
--- a/chessboard_render.php
+++ b/chessboard_render.php
@@ -1,57 +1,15 @@
 <?php
-
 /**
  * @file
  * Theme related functions for the Chessboard Renderer module.
  */
 
-function chessboard_border_filename($border) {
-  // $border: T, B, L, R, TL, TR, BL, BR
-  switch ($border) {
-    case 'T':
-    case 'B':
-      return 'h.png';
-    case 'L':
-    case 'R':
-      return 'v.png';
-    default:
-      return 'c.png';
-  }
-}
-
-function chessboard_piece_filename($piece, $square_color) {
-  // $piece        : KQBNRPkqbnrp-
-  // $square_color : 0 1
-  switch ($piece) {
-    case 'x':
-      $name = 'xx';
-      break;
-    case '-':
-      $name = '';
-      break;
-    default:
-      $name = strtolower($piece) . (ctype_lower($piece) ? 'd' : 'l');
-      break;
-  }
-
-  return $name . ($square_color ? 'd' : 'l') . '.png';
-}
-
 function template_preprocess_chessboard(&$variables) {
   static $tables_xhtml = array();
 
-  if (!isset($variables['element']['#image_directory_path'])) {
-    $variables['element']['#image_directory_path'] = drupal_get_path('module', 'chessboard') . '/default';
-  }
   $image_directory_path = $variables['element']['#image_directory_path'];
   if (!isset($tables_xhtml[$image_directory_path])) {
     $groups = array('square', 'border-h', 'border-v', 'border-c');
-    $test_paths = array(
-      'square' => $image_directory_path . '/' . chessboard_piece_filename('-', 0),
-      'border-h' => $image_directory_path . '/' . chessboard_border_filename('T'),
-      'border-v' => $image_directory_path . '/' . chessboard_border_filename('R'),
-      'border-c' => $image_directory_path . '/' . chessboard_border_filename('TR'),
-    );
     $class_attributes = array(
       'square' => array(
         'class' => 'chessboard-square',
@@ -66,36 +24,7 @@ function template_preprocess_chessboard(&$variables) {
         'class' => 'chessboard-border-c',
       ),
     );
-    $default_image_directory_path = drupal_get_path('module', 'chessboard') . '/default';
-    if ($image_directory_path === $default_image_directory_path) {
-      $size_attributes = array(
-        'square' => array(
-          'width' => 40,
-          'height' => 40,
-        ),
-        'border-h' => array(
-          'width' => 40,
-          'height' => 4,
-        ),
-        'border-v' => array(
-          'width' => 4,
-          'height' => 40,
-        ),
-        'border-c' => array(
-          'width' => 4,
-          'height' => 4,
-        ),
-      );
-    }
-    foreach ($test_paths as $group => $test_path) {
-      if (!isset($size_attributes[$group]) && is_file($test_path) && (list($width, $height) = @getimagesize($test_path))) {
-        $size_attributes[$group] = array(
-          'width' => $width,
-          'height' => $height,
-        );
-      }
-    }
-
+    $size_attributes = $variables['element']['#dimensions'];
     $group_attributes = array();
     foreach ($groups as $group) {
       if (!isset($size_attributes[$group])) {
-- 
1.7.4

