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

---
 CHANGELOG.txt     |    1 +
 chessboard.module |  137 ++++++++++++++++++++++++++++-------------------------
 2 files changed, 74 insertions(+), 64 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 0021754..019aedc 100755
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,6 +1,7 @@
 
 Chessboard Renderer 6.x-2.x, xxxx-xx-xx
 ---------------------------
+Issue #1078556 by Eric_A: Simplified render element structure.
 task #1060484 by Eric_A: Removed chessboard_field().
 task #1052734 by Eric_A: Better distinction between generic and filter specific
   code.
diff --git a/chessboard.module b/chessboard.module
index 36e7ebf..31cb583 100644
--- a/chessboard.module
+++ b/chessboard.module
@@ -86,55 +86,53 @@ function chessboard_filter($op, $delta = 0, $format = -1, $text = '', $cache_id
 }
 
 function chessboard_process_chessboard_notation($elements) {
-  foreach (element_children($elements) as $delta) {
-    $item = $elements[$delta]['#item'];
-    preg_match('@^\s*(?:\((.*?)\))?(.*)$@s', $item['value'], $matches);
-
-    // 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;
-
-    // Color of the upper left square: [l]ight (default), or [d]ark
-    $elements[$delta]['#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));
-
-    // 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;
-      }
+  $item = $elements['#item'];
+  preg_match('@^\s*(?:\((.*?)\))?(.*)$@s', $item['value'], $matches);
+
+  // Parameter Parsing
+  //--------------------------------
+  $params =& $matches[1];
+
+  // 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['#square_color_first'] = (strpos($params, 'd') !== FALSE) ? 1 : 0;
+
+  // 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['#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;
 }
@@ -293,18 +291,20 @@ function _chessboard_filter_callback($matches, $set_filter = NULL) {
   $items = array($item);
   $element = _chessboard_filter_formatter_view($items);
   $output = _chessboard_render_elements($element);
-  if (isset($element['#attached']) && filter_format_allowcache($filter->format)) {
-    // Store attached structures so we can process these when cached filtered text is being output.
-    $data['#attached'] = $element['#attached'];
-    $cid = $filter->format . ':chessboard-filter-attached' . md5(serialize($element['#attached']));
-    $expire = time() + (60 * 60 * 24) + (60 * 60);
-    cache_set($cid, $data, 'cache_filter', $expire);
-    $list_cid = $filter->format . ':chessboard-filter-attached-cids';
-    $cache = cache_get($list_cid, 'cache_filter');
-    $cids = isset($cache->data) ? $cache->data : array();
-    $cids[$cid] = TRUE;
-    if (!$cache || $cids != $cache->data || $expire != $cache->expire) {
-      cache_set($list_cid, $cids, 'cache_filter', $expire);
+  foreach (element_children($element) as $delta) {
+    if (isset($element[$delta]['#attached']) && filter_format_allowcache($filter->format)) {
+      // Store attached structures so we can process these when cached filtered text is being output.
+      $data['#attached'] = $element[$delta]['#attached'];
+      $cid = $filter->format . ':chessboard-filter-attached' . md5(serialize($element[$delta]['#attached']));
+      $expire = time() + (60 * 60 * 24) + (60 * 60);
+      cache_set($cid, $data, 'cache_filter', $expire);
+      $list_cid = $filter->format . ':chessboard-filter-attached-cids';
+      $cache = cache_get($list_cid, 'cache_filter');
+      $cids = isset($cache->data) ? $cache->data : array();
+      $cids[$cid] = TRUE;
+      if (!$cache || $cids != $cache->data || $expire != $cache->expire) {
+        cache_set($list_cid, $cids, 'cache_filter', $expire);
+      }
     }
   }
   return $output;
@@ -320,17 +320,26 @@ function _chessboard_filter_page_structures_list_cids($formats) {
   return $cids;
 }
 
+/**
+ * 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

