Notice: Undefined index: body in read_more_node_view() (line 209 of /is/htdocs/SERVERNAME/www/drupal/sites/all/modules/read_more/read_more.module).

From time to time I find those notices in my watchdog. Don't know what to make of it.

Comments

rolandk’s picture

I am getting the same notice too...

justmagicmaria’s picture

Subscribe. Me also, on a page full of teasers of two types of content: Feed items (from the Feeds module) and Story, a custom content type. Feed item does not have a body field.

Edited to add:

Sorry this is not a patch, but I fixed this with this change to line 209 of read_more.module:

      // Get last position of the last closing marker in teaser.
      if (array_key_exists('body', $node->content) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {

(I added the array_key_exists test.)

Maria

aliad’s picture

StatusFileSize
new1009 bytes

Hopefully this is helpful, since your fix worked for me:

GNU nano 2.3.1 File: read_more.patch Modified

--- read_more.module    2011-04-29 01:12:01.000000000 -0400
+++ read_more.module.b  2011-08-16 08:31:28.801047417 -0400
@@ -206,7 +206,7 @@ function read_more_node_view($node, $vie
       $elements = '(?:' . implode('|', $elements_array) . ')';
 
       // Get last position of the last closing marker in teaser.
-      if (preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {
+      if (array_key_exists('body', $node->content) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OF$
         // Recalculate the position in $teaser. We do this because there may be extra CCK fields appended to the teaser.
         $insert_point = strpos($view_mode, $node->content['body'][0]['#markup']) + $match[0][1];
         // Insert the link.
dddave’s picture

Status: Active » Needs review
darkshared’s picture

I had the same error and could fix it following JustMagicMaria comment.

In my case, the bug appeared after editing the poll content type to add a text camp. Deleting the text camp I just created, makes the error disapear.

PD: sorry for my english, I am still learning :(

Refineo’s picture

I had the same issue and I also fixed it with #2 (thank you !)
There is a typo in #3 patch I belive (or am I wrong ?)

Is:

+      if (array_key_exists('body', $node->content) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OF$

I think it should be:

+      if (array_key_exists('body', $node->content) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE))

Will you commit this patch to 7.x-1.x-dev ?

jackalope’s picture

StatusFileSize
new1001 bytes

Here's a functional patch based on #2. Thanks, Maria and everyone else who worked on this!

dnewkerk’s picture

Status: Needs review » Reviewed & tested by the community

Tested #7 and it works. Thanks! Marking RTBC

femrich’s picture

Unfortunately I am among the patch-challenged. Any idea when this may find its way into 7.x-1.x-dev?

dnewkerk’s picture

femrich, fortunately this patch is really just one changed line. If you aren't able to apply the patch but want to get the fix for the moment, simply open read_more.module, search for this line:

if (preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {

And replace it with this line:

if (array_key_exists('body', $node->content) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {

Though @Todd Nienkerk - can we get this patch added to dev?

femrich’s picture

Thanks, keyz. I'll try this.

vikingew’s picture

Status: Reviewed & tested by the community » Needs review

Sorry to be a pester but I wonder if the correct solution isn't the one used in #1307400: If teaser has no HTML, cannot be inserted inline, solving another problem with this function? I may be wrong but think it's correct to be bold about it possibly avoiding a less favourable commit. The solution suggested also appear a bit to general to me and from what I have read no one have really identified what the problem is, just suggested a solution that (possibly) works for some, but maybe not for all.

In such case the solution looks like this:

-      if (preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {
+      if (isset($node->content['body'][0]['#markup']) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {

I cannot make a proper patch right now so leave that up to someone else, but even better is if #1307400: If teaser has no HTML, cannot be inserted inline could go in as it catches 2 flies in 1 smack so to say ;-)

dnewkerk’s picture

StatusFileSize
new892 bytes

Here is a patch version of yettyn's suggestion in #12. Works for me. Others please try the patch, as well as review whether this solution or the previously suggested one is the correct direction for the module to proceed with. Thanks!

vikingew’s picture

Thanks for that Keyz but actually what I suggested was to use the full code change of #1307400: If teaser has no HTML, cannot be inserted inline as these 2 issue probably is a dupe of each other, but not sure which to set as dupe as this one appear to be older while the other actually identify the problem and gives a correct solution.

Again I can't make a proper patch because I don't have git but in my own drupal svn overlay I have implemented it like this, it also include a small fix where I have changed the Read More html code filed from textfield to textarea because I find the 60 chars drupal give you to be too short if you like to be creative with the link ;-)

web htdocs # svn diff sites/all/modules/read_more/read_more.module
Index: sites/all/modules/read_more/read_more.module
===================================================================
--- sites/all/modules/read_more/read_more.module        (revision 2151)
+++ sites/all/modules/read_more/read_more.module        (working copy)
@@ -104,7 +104,7 @@
     '#collapsible' => FALSE,
   );
     $form['read_more_formatting']['read_more_text'] = array(
-      '#type' => 'textfield',
+      '#type' => 'textarea',
       '#title' =>  t('Link text'),
       '#default_value' => variable_get('read_more_text', READ_MORE_TEXT_DEFAULT),
       '#description' => t('Enter the text you wish to display in the Read More link. Special characters should be encoded (like &amp;amp;raquo; or &amp;amp;amp;). Allowed HTML is listed below.'),
@@ -206,13 +206,15 @@
       $elements = '(?:' . implode('|', $elements_array) . ')';
 
       // Get last position of the last closing marker in teaser.
-      if (preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {
+      if (isset($node->content['body'][0]['#markup']) && preg_match('!</?' . $elements . '[^>]*>\s*$!i', $node->content['body'][0]['#markup'], $match, PREG_OFFSET_CAPTURE)) {
         // Recalculate the position in $teaser. We do this because there may be extra CCK fields appended to the teaser.
         $insert_point = strpos($view_mode, $node->content['body'][0]['#markup']) + $match[0][1];
         // Insert the link.
         $node->content['body'][0]['#markup'] = substr_replace($node->content['body'][0]['#markup'], drupal_render($read_more_link), $insert_point, 0);
-      }
-      else {
+      // In case there is no HTML markup
+      } elseif(isset($node->content['body'][0]['#markup']) && strip_tags($node->content['body'][0]['#markup']) == $node->content['body'][0]['#markup']) {
+        $node->content['body'][0]['#markup'] = rtrim($node->content['body'][0]['#markup']) . drupal_render($read_more_link);
+      } else {
         $display = 'after';
       }
     }

It's the change I have done for my personal taste on the current dev version and it works very well.

john franklin’s picture

Issue summary: View changes
Status: Needs review » Fixed

It looks like an isset($node->content['body'] has been added to the code since this bug was introduced, which may be sufficient to fix this issue.

I'm going to mark this 'fixed', but if you guys think this needs a more robust patch, please reopen it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.