From 59613bf87986d4366f8dc404f0cdb127bf4ea75a Mon Sep 17 00:00:00 2001
From: Eric Alvares <erica@344430.no-reply.drupal.org>
Date: Wed, 2 Mar 2011 14:12:55 +0100
Subject: [PATCH] Issue #1078556 by Eric_A: Simplified render element structure.

---
 chessboard.module                          |   76 +++++++++++++--------------
 chessboard_filter/chessboard_filter.module |   25 ++++++---
 2 files changed, 54 insertions(+), 47 deletions(-)

diff --git a/chessboard.module b/chessboard.module
index 4cd58af..63fce2b 100644
--- a/chessboard.module
+++ b/chessboard.module
@@ -1,55 +1,53 @@
 <?php
 
 function chessboard_process_chessboard_notation($elements) {
-  foreach (element_children($elements) as $delta) {
-    $item = $elements[$delta]['#item'];
-    preg_match('@^\s*(?:\((.*?)\))?(.*)$@s', $item['value'], $matches);
+  $item = $elements['#item'];
+  preg_match('@^\s*(?:\((.*?)\))?(.*)$@s', $item['value'], $matches);
 
-    // Parameter Parsing
-    //--------------------------------
-    $params =& $matches[1];
+  // Parameter Parsing
+  //--------------------------------
+  $params =& $matches[1];
 
-    // Number of files: any integer (default: 8)
-    $elements[$delta]['#file_max'] = preg_match('@[0-9]+@', $params, $m) ? $m[0] : 8;
+  // Number of files: any integer (default: 8)
+  $elements['#file_max'] = preg_match('@[0-9]+@', $params, $m) ? $m[0] : 8;
 
-    // Color of the upper left square: [l]ight (default), or [d]ark
-    $elements[$delta]['#square_color_first'] = (strpos($params, 'd') !== FALSE) ? 1 : 0;
+  // Color of the upper left square: [l]ight (default), or [d]ark
+  $elements['#square_color_first'] = (strpos($params, 'd') !== FALSE) ? 1 : 0;
 
-    // Borders
-    $elements[$delta]['#border'] = array('T' => (strpos($params, 'T') !== FALSE),
-                    'B' => (strpos($params, 'B') !== FALSE),
-                    'L' => (strpos($params, 'L') !== FALSE),
-                    'R' => (strpos($params, 'R') !== FALSE));
+  // Borders
+  $elements['#border'] = array('T' => (strpos($params, 'T') !== FALSE),
+                  'B' => (strpos($params, 'B') !== FALSE),
+                  'L' => (strpos($params, 'L') !== FALSE),
+                  'R' => (strpos($params, 'R') !== FALSE));
 
-    // Render the board in XHTML syntax
-    //----------------------------------
-    $board_value =& $matches[2];
-    $board_value = strip_tags($board_value);
-    $elements[$delta]['#board'] = array();
-    $file = $rank_d = 0;
-    for ($i=0; isset($board_value[$i]); $i++) {
-      // Ignore unknown characters
-      if (strpos('12345678KQBNRPkqbnrp-x', $board_value[$i]) === FALSE) {
-        continue;
-      }
+  // Render the board in XHTML syntax
+  //----------------------------------
+  $board_value =& $matches[2];
+  $board_value = strip_tags($board_value);
+  $elements['#board'] = array();
+  $file = $rank_d = 0;
+  for ($i=0; isset($board_value[$i]); $i++) {
+    // Ignore unknown characters
+    if (strpos('12345678KQBNRPkqbnrp-x', $board_value[$i]) === FALSE) {
+      continue;
+    }
 
-      if ($file >= $elements[$delta]['#file_max']) {
-        $file = 0;
-        $rank_d++;
-      }
+    if ($file >= $elements['#file_max']) {
+      $file = 0;
+      $rank_d++;
+    }
 
-      if (strpos('12345678', $board_value[$i]) !== FALSE) {
-        // Multiple empty squares
-        for ($j=0; $j<$board_value[$i]; $j++) {
-          $elements[$delta]['#board'][$rank_d][$file] = '-';
-          $file++;
-        }
-      }
-      else {
-        $elements[$delta]['#board'][$rank_d][$file] = $board_value[$i];
+    if (strpos('12345678', $board_value[$i]) !== FALSE) {
+      // Multiple empty squares
+      for ($j=0; $j<$board_value[$i]; $j++) {
+        $elements['#board'][$rank_d][$file] = '-';
         $file++;
       }
     }
+    else {
+      $elements['#board'][$rank_d][$file] = $board_value[$i];
+      $file++;
+    }
   }
   return $elements;
 }
diff --git a/chessboard_filter/chessboard_filter.module b/chessboard_filter/chessboard_filter.module
index 16176c0..31b60ce 100755
--- a/chessboard_filter/chessboard_filter.module
+++ b/chessboard_filter/chessboard_filter.module
@@ -197,17 +197,26 @@ function _chessboard_filter_callback($matches, $set_filter = NULL) {
   return $output;
 }
 
+/**
+ * Build a renderable array for a chessboard value.
+ *
+ * @param $items
+ *   Array of values.
+ *
+ * @return
+ *   A renderable array for the $items, as an array of child elements keyed
+ *   by numeric indexes starting from 0.
+ */
 function _chessboard_filter_formatter_view($items) {
-  $element = array(
-    '#attached' => array(
-      'css' => array(
-        drupal_get_path('module', 'chessboard') . '/chessboard.css' => array('media' => 'all', 'preprocess' => FALSE),
-      ),
-    ),
-    '#pre_render' => array('chessboard_process_chessboard_notation'),
-  );
+  $element = array();
   foreach ($items as $delta => $item) {
     $element[$delta] = array(
+      '#attached' => array(
+        'css' => array(
+          drupal_get_path('module', 'chessboard') . '/chessboard.css' => array('media' => 'all', 'preprocess' => FALSE),
+        ),
+      ),
+      '#pre_render' => array('chessboard_process_chessboard_notation'),
       '#theme' => 'chessboard',
       '#item' => $item,
       '#prefix' => '<div class="chessboard">',
-- 
1.7.4

