Problem/Motivation
We have some error with Drupal 10.2.x and php 8.2.x when comment module is enabled on node page edit
Undefined array key 0 in Drupal\comment\Plugin\Field\FieldWidget\CommentWidget->formElement() (line 71 of core/modules/comment/src/Plugin/Field/FieldWidget/CommentWidget.php).
Undefined array key 0 in Drupal\comment\CommentFieldItemList->get() (line 25 of core/modules/comment/src/CommentFieldItemList.php).
Steps to reproduce
There appear to be situations where comment field default values are not available. In OP's case this occurred when they had comments showing on an a node/*/edit page.
Proposed resolution
The patch proposed by OP makes changes to CommentFieldItemList.php and CommentWidget.php:
In CommentFieldItemList.php a check has been added to ensure $field_default_value[0] exists before accessing it.
In CommentWidget.php, the current comment status is stored in a variable as $status. Then there is a check which ensures that if there is a $field_default_value[0], to also check that this differs from $status, and only in that case $status will be updated to with the default value.
Remaining tasks
- Reproduce the error
- Check that this fixes the error
- Review
- Merge
User interface changes
N/A
API changes
N/A
Data model changes
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | undefined_array_comment_module_2_3453896.patch | 1.83 KB | nicolas s. |
Issue fork drupal-3453896
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
nicolas s. commentedComment #3
nicolas s. commentedComment #4
nicolas s. commentedComment #9
nicolas s. commentedComment #10
nicolas s. commentedComment #11
smustgrave commentedThanks for reporting
Moving to 11.x as the current development branch, MR will have to be updated as well.
Issue summary should follow standard issue template
There are a test scenario that can be added to show the issue and make sure it doesn't come back.
Thanks.
Comment #12
jacobupal commentedComment #13
jacobupal commentedI updated the summary to match the issue template, as this solved my problem too, so I'd love for it to be moved along.
I'm getting this error in a more convoluted scenario; I have three different types of comment in a fieldgroup, rendering on a page where there are also editable fields. I'm pleased to say the proposed change also handled my case and I stopped receiving needless error messages.
Comment fields do not need to have default values and this patch seems to handle the situation well.