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

Issue fork drupal-3453896

Command icon 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

Nicolas S. created an issue. See original summary.

nicolas s.’s picture

nicolas s.’s picture

nicolas s.’s picture

Status: Active » Needs review

Nicolas S. changed the visibility of the branch 3453896-undefined-array-key to hidden.

nicolas s.’s picture

Issue summary: View changes
nicolas s.’s picture

Issue summary: View changes
smustgrave’s picture

Version: 10.2.x-dev » 11.x-dev
Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Thanks 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.

jacobupal’s picture

Issue summary: View changes
jacobupal’s picture

I 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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.