From f47da9bf0c2f648839d5712037dcd71115875dee Mon Sep 17 00:00:00 2001
From: HollyIT <jamie@hollyit.net>
Date: Sun, 13 Mar 2011 21:13:33 -0400
Subject: [PATCH] Issue #823380 by intoxination: read more fix

---
 modules/field/modules/text/text.module |   67 ++++++++++++++++++++++++++------
 modules/node/node.module               |   32 ++++++++++++---
 2 files changed, 80 insertions(+), 19 deletions(-)
 mode change 100644 => 100755 modules/field/modules/text/text.module
 mode change 100644 => 100755 modules/node/node.module

diff --git a/modules/field/modules/text/text.module b/modules/field/modules/text/text.module
old mode 100644
new mode 100755
index 89c605c..440ca86
--- a/modules/field/modules/text/text.module
+++ b/modules/field/modules/text/text.module
@@ -263,9 +263,12 @@ function text_field_formatter_view($entity_type, $entity, $field, $instance, $la
       foreach ($items as $delta => $item) {
         $output = _text_sanitize($instance, $langcode, $item, 'value');
         if ($display['type'] == 'text_trimmed') {
-          $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
+          $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length'], true);
+          $element[$delta] = array('#markup' => $output['summary'], '#readmore' => $output['trimmed']);
+        } else {
+          $element[$delta] = array('#markup' => $output);
         }
-        $element[$delta] = array('#markup' => $output);
+        
       }
       break;
 
@@ -273,12 +276,13 @@ function text_field_formatter_view($entity_type, $entity, $field, $instance, $la
       foreach ($items as $delta => $item) {
         if (!empty($item['summary'])) {
           $output = _text_sanitize($instance, $langcode, $item, 'summary');
+          $element[$delta] = array('#markup' => $output);
         }
         else {
           $output = _text_sanitize($instance, $langcode, $item, 'value');
-          $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length']);
+          $output = text_summary($output, $instance['settings']['text_processing'] ? $item['format'] : NULL, $display['settings']['trim_length'], true);
+          $element[$delta] = array('#markup' => $output['summary'], '#readmore' => $output['trimmed']);
         }
-        $element[$delta] = array('#markup' => $output);
       }
       break;
 
@@ -341,10 +345,13 @@ function _text_sanitize($instance, $langcode, $item, $column) {
  *   The desired character length of the summary. If omitted, the default
  *   value will be used. Ignored if the special delimiter is present
  *   in $text.
+ * @param $return_status
+ *   Set to true if you would like an array returned, including 'text' and
+ *   'trimmed'. If trimming occurs, array key trimmed will be set to true.
  * @return
- *   The generated summary.
+ *   The generated summary or an array containing the keys summary and trimmed if $return_status is true.
  */
-function text_summary($text, $format = NULL, $size = NULL) {
+function text_summary($text, $format = NULL, $size = NULL, $return_status = FALSE) {
 
   if (!isset($size)) {
     // What used to be called 'teaser' is now called 'summary', but
@@ -357,12 +364,28 @@ function text_summary($text, $format = NULL, $size = NULL) {
 
   // If the size is zero, and there is no delimiter, the entire body is the summary.
   if ($size == 0 && $delimiter === FALSE) {
-    return $text;
+    if ($return_status){
+      return array(
+        'summary' => $text,
+        'trimmed' => false
+      );
+    } else {
+      return $text;
+    }
+    
   }
 
   // If a valid delimiter has been specified, use it to chop off the summary.
   if ($delimiter !== FALSE) {
-    return substr($text, 0, $delimiter);
+    $teaser = substr($text, 0, $delimiter);
+    if ($return_status){
+      return array(
+        'summary' => $teaser,
+        'trimmed' => false
+      );
+    } else {
+      return $teaser;
+    }
   }
 
   // We check for the presence of the PHP evaluator filter in the current
@@ -371,13 +394,27 @@ function text_summary($text, $format = NULL, $size = NULL) {
   if (isset($format)) {
     $filters = filter_list_format($format);
     if (isset($filters['php_code']) && $filters['php_code']->status && strpos($text, '<?') !== FALSE) {
-      return $text;
+      if ($return_status){
+        return array(
+            'summary' => $text,
+            'trimmed' => true
+          );
+        } else {
+          return $text;
+      }
     }
   }
 
   // If we have a short body, the entire body is the summary.
   if (drupal_strlen($text) <= $size) {
-    return $text;
+    if ($return_status){
+      return array(
+        'summary' => $text,
+        'trimmed' => false
+      );
+    } else {
+      return $text;
+    }
   }
 
   // If the delimiter has not been specified, try to split at paragraph or
@@ -440,8 +477,14 @@ function text_summary($text, $format = NULL, $size = NULL) {
   if (isset($filters['filter_htmlcorrector'])) {
     $summary = _filter_htmlcorrector($summary);
   }
-
-  return $summary;
+  if ($return_status){
+    return array(
+      'summary' => $summary,
+      'trimmed' => true
+    );
+  } else {
+    return $summary;
+  }
 }
 
 /**
diff --git a/modules/node/node.module b/modules/node/node.module
old mode 100644
new mode 100755
index c052f4b..44c20f0
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -1362,6 +1362,7 @@ function node_build_content($node, $view_mode = 'full', $langcode = NULL) {
   entity_prepare_view('node', array($node->nid => $node));
   $node->content += field_attach_view('node', $node, $view_mode, $langcode);
 
+
   // Always display a read more link on teasers because we have no way
   // to know when a teaser view is different than a full view.
   $links = array();
@@ -1370,15 +1371,32 @@ function node_build_content($node, $view_mode = 'full', $langcode = NULL) {
     '#pre_render' => array('drupal_pre_render_links'),
     '#attributes' => array('class' => array('links', 'inline')),
   );
+  // Loop through all fields checking for #access and #readmore. If both items set to true
+  // then we can add the readmore link.
   if ($view_mode == 'teaser') {
-    $node_title_stripped = strip_tags($node->title);
-    $links['node-readmore'] = array(
-      'title' => t('Read more<span class="element-invisible"> about @title</span>', array('@title' => $node_title_stripped)),
-      'href' => 'node/' . $node->nid,
-      'html' => TRUE,
-      'attributes' => array('rel' => 'tag', 'title' => $node_title_stripped),
-    );
+    foreach ($node->content as $item){
+      if (isset($item['#access']) && $item['#access'] == 1){
+        foreach ($item as $key=>$value){
+          // check to see if the key is a property or child. We want children only here
+          if ($key === '' || $key[0] !== '#') {
+            if (isset($value['#readmore']) && $value['#readmore'] == TRUE){
+              $node_title_stripped = strip_tags($node->title);
+              $links['node-readmore'] = array(
+                'title' => t('Read more<span class="element-invisible"> about @title</span>', array('@title' => $node_title_stripped)),
+                'href' => 'node/' . $node->nid,
+                'html' => TRUE,
+                'attributes' => array('rel' => 'tag', 'title' => $node_title_stripped),
+              );
+              // Readmore only needs to be added once, so we can go ahead and continue out of both
+              // loops.
+              continue 2;
+            }
+          }
+        }
+      }
+    }
   }
+
   $node->content['links']['node'] = array(
     '#theme' => 'links__node__node',
     '#links' => $links,
-- 
1.7.3

