using d7 dev (2010-May-30) and received following error: Notice: Undefined index: format in comment_preview() (line 1966 of ***modules/comment/comment.module).

Steps to recreate error:
(1) manage comment fields of a content type, edit comment_body. (2) select "Plain text" under Text processing. (3) leave defaults and/or change "format" under Comment Display. (4) view an existing node and create/edit comment. (5) click preview button.

Note: This only occurs with settings above, and clicking preview button. Line 1966 (as of 5/30/10) from comment.module is: $comment->format = $comment->comment_body[LANGUAGE_NONE][0]['format'];. Similar is also on line 2093.

Thanks a million!

Comments

dinknaround’s picture

Update: I commented out line 1966 //$comment->format = $comment->comment_body[LANGUAGE_NONE][0]['format']; and it stopped the error and allowed the proper formatting during preview, as plain text and stripping html by field module/text module. Although I notice that the textarea box retains the html (but that's probably a whole other issue). As far as similar on line 2093, haven't touched it yet.

aspilicious’s picture

Tried a fresh installation??
With the new manage field screen you need a fresh install.

dinknaround’s picture

Yes, this was on a fresh install of the D7-dev released on 5/30. I did notice the new options when managing a node content's fields. I'm not an expert, but I really think it has something to do with the code in the comment.module's function comment_preview. Don't understand why this line is needed $comment->format = $comment->comment_body[LANGUAGE_NONE][0]['format'];. The proper format gets picked up and set in variables without that line. I didn't notice comment.module creating a custom format types.

If you referring to a D7-dev after 5/30, I'll try it out.

dinknaround’s picture

aspilicious - I did a fresh install today (6/3/2010). And still have the same error. So basically, when managing the comment field's body, "Plain text" can "NOT" be selected a format, and matching the field's display view as "Plain text" does not fix the error to undefined index.

aspilicious’s picture

I'll try to reproduce

aspilicious’s picture

Notice: Undefined index: format in comment_preview() (line 1960 of C:\xampp\htdocs\drupal\modules\comment\comment.module).

bingo...

Ok let's make those steps more readable

1) go to manage comment fields of article
2) edit comment_body.
3) select "Plain text" under Text processing
4) Change "format" under Comment Display. ==> Plain Text
5) view an existing node and create comment.
6) click preview button.

dinknaround’s picture

OMG Yes! Thank you, I'll remember to keep it simple in the future.
As for the issue, anybody out there with a solution?

yched’s picture

This $content->format sounds like a thing from the past. The format of the text in the 'comment_body' field is not an inherent data of the comment itself. On a quick inspection (but this should probably be double checked), no other part of comment.module makes use of it.

On a related note, comment_submit() also assumes a) that there is a 'comment_body' field, and b) that it is 'formatted text' and not 'plain text'. Both assumptions can be wrong.

damien tournoud’s picture

Note that $comment->format is *not even* in the schema; we might just be able to remove those lines completely.

damien tournoud’s picture

About comment_submit(): we need to generate the subject from the comment itself. Could we just call comment_build_content(), convert it to plain-text and use the begining of that?

yched’s picture

"Note that $comment->format is in the schema"
Really ? I can't see this.

[edit: Damien fixed his post]

"About comment_submit(): we need to generate the subject from the comment itself. Could we just call comment_build_content(), convert it to plain-text and use the begining of that?"
Sounds good to me.

damien tournoud’s picture

"Note that $comment->format is in the schema"
Really ? I can't see this.

That was a typo.

dinknaround’s picture

Actually, I currently have the $comment->format commented out (a hack, I know, bad) but it works with no problems for me. Doesn't comment module just hook into fields defined in field module...which I believe picks up the proper formatting? I'm in NO WAY an expert, but it's working for me this way.

dinknaround’s picture

Title: Undefined index: format in comment_preview line 1966 » Undefined index: format in comment_preview line 2065
Version: 7.x-dev » 7.0

Back again, still same issue, now with official d7 release.

It's the line from comment_preview()
$comment->format = $comment->comment_body[LANGUAGE_NONE][0]['format'];

Did a variable dump, found that ['value'] and ['format'] will be applied with the module's default settings. But only ['value'] will be applied if settings changed to "plain text".

And yes, I tried this by changing both format settings in edit field and manage display, and then changing one but not the other.

sTaX-1’s picture

I have the same issue.
But if I allow filtred or plain text/html it works and the 'format'-key exist.

mattyoung’s picture

#9:

>Note that $comment->format is *not even* in the schema; we might just be able to remove those lines completely.

This fix the problem here. But the real problem is the "format" key should always be there. If not, how to know what the format is on comment validate?

Also, the "format" key is in the node body. Things should work the same way here.

EDIT: never mind. I see what's going on from #6. So if text processing is "Plain text", there is no format.

dinknaround’s picture

Well, if I'm understanding this right, $comment->format is in the schema. See see field_data_comment_body in the db, the field is comment_body_format. Does the comment module code pretty much mimic the node module? This issue doesn't happen when selecting plain text as the format for the body field in node type edit form. And the field_data_body table also has body_format. Doesn't the text.module handle the formatting?

dinknaround’s picture

Title: Undefined index: format in comment_preview line 2065 » Undefined index: format in comment_preview line 2065 and line 2183

Well, I still can't figure why on line 2065 the comment.module has $comment->format = $comment->comment_body[LANGUAGE_NONE][0]['format'];. Is it to ensure format type gets added to the db at field_data_comment_body? Doesn't the field module handle this when it's functions are invoked in comment_save? And default in db is NULL I believe.

Then on line 2183 the comment.module has $comment->subject = truncate_utf8(trim(decode_entities(strip_tags(check_markup($comment->comment_body[LANGUAGE_NONE][0]['value'], $comment->comment_body[LANGUAGE_NONE][0]['format'])))), 29, TRUE);. This is to populate an empty $comment->subject, (the subject field on the form). Since this is a textfield, and basically the title of a published comment, shouldn't this be "plain text" anyway?

Here is a really quick workaround I'm using right now instead of hacking the core, I use a small custom module for a few of my alters anyway. Anyone have an opinion of this? Or a better way?

<?php
function myalters_form_comment_form_alter(&$form, &$form_state, $form_id) {
  if ($form_state['field']['comment_body']['und']['instance']['settings']['text_processing'] == 0) {
    $form['comment_body']['und'][0] += array(
      'format' => array(
        '#type' => 'value',
        '#value' => 'plain_text',
      ),
    );
  }
}
?>
droplet’s picture

StatusFileSize
new1.11 KB

$content->format sounds like a thing from the past.
$comment->format is *not even* in the schema

Agreed

This fix the problem here. But the real problem is the "format" key should always be there. If not, how to know what the format is on comment validate?

if not there, check_markup called filter_fallback_format and returns right filters.

#8 .On a related note, comment_submit() also assumes a) that there is a 'comment_body' field, and b) that it is 'formatted text' and not 'plain text'. Both assumptions can be wrong.

I guess we need to add a variable for it to set the right fields

add a temporary patch for it, somebody may needed.

mattyoung’s picture

if not there, check_markup called filter_fallback_format and returns right filters

#8 .On a related note, comment_submit() also assumes a) that there is a 'comment_body' field, and b) that it is 'formatted text' and not 'plain text'. Both assumptions can be wrong

We need to somehow know all of these to write comment validate code. The same applies for node validate. Most people probably will not know and write buggy code that works most of the time. Ideally, these should be documented. For node validate, we can put this information in hook_node_validate(). Unfortunate, there is no hook_comment_validate(). Maybe we should put this back in so these thing can be documented there. See #512492: Remove hook_comment_validate() and #974118: hook_comment($op == 'validate') has gone.

agence web coheractio’s picture

I think patch in #19 shall read
$comment->comment_body[LANGUAGE_NONE][0]['format'] = NULL;
(without the ' ' around NULL)

Laurent

yugongtian’s picture

My content type do not use subject ,and I delete the comment body field .
Create a new text field .to limit Maximum length *

But when I try to replay the content get ero. I search it in here.

I was try # 19 or # 21 ,but get also this ero

Notice: Undefined index: value in comment_submit() (line 2185 of E:\wamp\www\test9\modules\comment\comment.module).

And I try to delete 2183 line it's ok : )

$comment->subject = truncate_utf8(trim(decode_entities(strip_tags(check_markup($comment->comment_body[LANGUAGE_NONE][0]['value'], $comment->comment_body[LANGUAGE_NONE][0]['format'])))), 29, TRUE);

I 'm not coder so,any helps ?Let me know why?

Thanks.

geerlingguy’s picture

Version: 7.0 » 7.x-dev

I'm hitting this error on a couple of my sites too:

Notice: Undefined index: format in comment_submit() (line 2161 of ~/d7-site/modules/comment/comment.module).

Bumping issue back to -dev, against which patches should be made.

Possible duplicate / merge-able issue: #1170948: Error Notice with Comments.

lyricnz’s picture

Status: Active » Needs review
StatusFileSize
new1013 bytes

Here's the patch from issue #1170948: Error Notice with Comments which fixes it. Closing other as duplicate.

RedTop’s picture

implemented patch, works beautifully.

D7.2

marcingy’s picture

Status: Needs review » Reviewed & tested by the community
yched’s picture

Status: Reviewed & tested by the community » Needs work

You can't run check_markup() if format is NULL.
If the field is "formatted", check_markup() should be used, if not check_plain() should be used.

geerlingguy’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Probably should fix 8.x first, then backport.

lyricnz’s picture

Status: Needs work » Needs review
StatusFileSize
new1022 bytes

Updated patch, $comment_body is just an alias to make the following code saner.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me. Tested with latest -dev and the notice is gone.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Well! That code is certainly much easier to read now.

However, it'd be nice to get #6 captured as an automated test.

lyricnz’s picture

Status: Needs work » Needs review
StatusFileSize
new1.04 KB

Status: Needs review » Needs work

The last submitted patch, comment-format-test.patch, failed testing.

lyricnz’s picture

Status: Needs work » Needs review
StatusFileSize
new2.03 KB

Patch in #32 is a test, that fails without the patch. Attached is #29+#32

lyricnz’s picture

StatusFileSize
new2.09 KB

Swapped ternary for if/else, and updated test description comment. :Q

catch’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, comes with a nice test too.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Perfect, thanks!

Committed to 8.x and 7.x.

geerlingguy’s picture

aspilicious’s picture

Webchick KILLS you if you remove that tag. The strange tag switching in webchicks post is something on drupal.org. She didn't do it. :)

But we can remove the needs tests.

geerlingguy’s picture

... but it's already been backported... right? Or does she use that tag for release tracking?

webchick’s picture

I like to keep it there because it's issue metadata that's important for historical reasons, so I can see how many "needs backport" issues got fixed. We also don't remove "Performance" just because an issue got fixed, for example. :)

Status: Fixed » Closed (fixed)

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

BeaPower’s picture

Hi is this a fix if you removed comment form? - using drupal 7

katd’s picture

Um, the fix for preview still seems to be missing?

Patricia_W’s picture

Version: 8.x-dev » 7.8

I'm getting this error in Preview mode:

Notice: Undefined index: format in comment_preview() (line 2043 of /home/content/p/w/a/pwarwick/html/modules/comment/comment.module).

_kash_’s picture

Notice: Undefined index: format in comment_submit() (line 2164 of /xxxxxxx/modules/comment/comment.module).

mototribe’s picture

I get an error: Undefined index: format in comment_preview() (line 2043 .... when previewing a comment (plain text format)
Drupal 7.8

geerlingguy’s picture

Version: 7.8 » 8.x-dev

Still needs to be fixed in head first...

no2e’s picture

I think there is a fix which needs testing:
#1371682: Undefined index: format in comment_preview line 2053

chi’s picture

StatusFileSize
new11.55 KB

Sorry, crossposting.

negativefix’s picture

Version: 8.x-dev » 7.17

Came across the same issue with the clean installation of drupal via drush. Although I don't desire, I had to switch text processing of comment body field to 'Filtered Text' to get rid of the error message.

simon147’s picture

Version: 7.17 » 7.20
Status: Closed (fixed) » Active

I confirm comment #51: That this bug seems to still exist (Drupal 7.20). Only thing: Instead of line 2065 it's now line 2069.

lyricnz’s picture

Status: Active » Closed (fixed)

Please don't reopen ~2 year old issues - create a new one, or add comments to one of the open duplicates.

David_Rothstein’s picture

I think this could legitimately be reopened since the original bug was never fixed. The bug was about comment_preview(), but the committed patches only addressed comment_submit().

Nonetheless, there's a new issue at #2077901: Comment Preview Error for Plain Text (Undefined index: format in comment_preview()) so might as well just use that one. But I think there's a lot of discussion here that is relevant for that issue...