Problem/Motivation

Steps to reproduce the problem:
1. Define an entity base field with comment field type.
2. Add new comment for the entity and click save will got error:
Call to a member function getSetting() on null in Drupal\comment\Entity\Comment::preCreate() (line 518 of core/modules/comment/src/Entity/Comment.php).

Research the code for Comment::preCreate():

  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    if (empty($values['comment_type']) && !empty($values['field_name']) && !empty($values['entity_type'])) {
      $field_storage = FieldStorageConfig::loadByName($values['entity_type'], $values['field_name']);
      $values['comment_type'] = $field_storage->getSetting('comment_type');
    }
  }

It using FieldStorageConfig::loadByName to fetch the field storage definition. This method could not fetch field storage definition for a base field.

Proposed resolution

Using \Drupal::service('entity_field.manager')->getFieldStorageDefinitions() to fetch field storage. The method can fetch field storage definition for both base field and bundle field.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

None

Original report by Jody Lynn

I added a Base Field of type 'comment' to a content entity type.

When adding a comment into the comment form, I got an error from Comment::preCreate() that FieldStorageConfig is not an object.
I'm guessing that base fields don't use FieldStorageConfig?

Comments

Jody Lynn created an issue. See original summary.

jody lynn’s picture

StatusFileSize
new902 bytes

Status: Needs review » Needs work

The last submitted patch, 2: 2855068.patch, failed testing.

larowlan’s picture

+++ b/core/modules/comment/src/Entity/Comment.php
@@ -512,8 +512,8 @@ public function setThread($thread) {
+      $fields = \Drupal::getContainer()->get('entity_field.manager')->getFieldStorageDefinitions($values['entity_type']);

Can you use \Drupal::service here instead of \Drupal::getContainer()

Nice catch, never considered case where someone would do this, we'd need to add a test for this, which would basically be a new test-module that modified the entity_test field definitions to add a comment field and then tried to add a comment.

cilefen’s picture

Version: 8.2.x-dev » 8.3.x-dev

Test this against 8.3.x.

cilefen’s picture

This different issue popped in my head when I saw this one.

himanshu-dixit’s picture

Assigned: Unassigned » himanshu-dixit

I would like to work on this issue .

himanshu-dixit’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests
StatusFileSize
new892 bytes

Here is the rerolled patch which also follows the suggestion in #4 .

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

laravz’s picture

The patch still applies and solves the issue for my projects. The container call has also been replaced by the service and I have no further issues with the solution.

I am rerunning the test for PHP 7, but am wondering whether we should add the suggested test (#4) in this issue or create a new one. In any case, we also still need a change record.

jian he’s picture

Version: 8.6.x-dev » 8.7.x-dev
Issue tags: -Needs tests
StatusFileSize
new4.5 KB
new3.63 KB

Add the testing script.

Status: Needs review » Needs work

The last submitted patch, 13: 2855068-basefield-13.patch, failed testing. View results

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jian he’s picture

I am not understand why the test is fail. Can someone give some suggest?

laravz’s picture

Hi,

+++ b/core/modules/comment/tests/src/Kernel/CommentBaseFieldTest.php
@@ -0,0 +1,35 @@
+use Drupal\Tests\token\Kernel\KernelTestBase;

Your patch is using the KernelTestBase from token. This module is a contrib module and can therefore not be found in core. DrupalCI also gave this error on rerun:

Fatal error: Class 'Drupal\Tests\token\Kernel\KernelTestBase' not found in /var/www/html/core/modules/comment/tests/src/Kernel/CommentBaseFieldTest.php on line 13

Perhaps you could use "Drupal\KernelTests\KernelTestBase" instead?

jian he’s picture

Status: Needs work » Needs review
StatusFileSize
new4.5 KB
new572 bytes

@LaravZ Thank you very much. I have miss selected the PhpStorm hint before.

Status: Needs review » Needs work

The last submitted patch, 18: 2855068-basefield-18.patch, failed testing. View results

jian he’s picture

Status: Needs work » Needs review
StatusFileSize
new5.12 KB
new641 bytes
jian he’s picture

StatusFileSize
new4.5 KB
new621 bytes

Status: Needs review » Needs work

The last submitted patch, 21: 2855068-basefield-21.patch, failed testing. View results

jian he’s picture

Status: Needs work » Needs review
StatusFileSize
new4.51 KB
new639 bytes
jian he’s picture

Issue summary: View changes
jian he’s picture

Issue summary: View changes
jian he’s picture

Assigned: himanshu-dixit » Unassigned
laravz’s picture

Patch still applies cleanly and the test has been added. I am wondering whether we should also test adding the field to an entity.

I also have the following question:

+++ b/core/modules/comment/tests/src/Kernel/CommentBaseFieldTest.php
@@ -0,0 +1,35 @@
+    $entity->save();

Would it be possible to assert whether the entity was saved correctly? The save() function should return a SAVED_NEW int or EntityStorageException in any case.

larowlan’s picture

Status: Needs review » Needs work
+++ b/core/modules/comment/tests/src/Kernel/CommentBaseFieldTest.php
@@ -0,0 +1,35 @@
+  public function testCommentBaseField() {
+    // Verify entity creation.
+    $entity = CommentTestBaseField::create();
+    $entity->name->value = $this->randomMachineName();

We need to create a comment on the given entity, to trigger the ::preCreate method, and then we need to assert that the comment_type value is added.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new4.57 KB
new5.45 KB
new2.17 KB

The last submitted patch, 29: 2855068-base-test-only.patch, failed testing. View results

The last submitted patch, 29: 2855068-base-test-only.patch, failed testing. View results

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

I have tested and reviewed this extensively in the context of a helpdesk module which uses comments as a base field. Every possible use case has been tested and it works as expected. RTBC

andypost’s picture

StatusFileSize
new611 bytes
new5.44 KB

Just a re-roll for 9.x and 8.x core

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 35: 2855068-35-9.x.patch, failed testing. View results

mrinalini9’s picture

Assigned: Unassigned » mrinalini9
andypost’s picture

As 9.0 test fails - patches should go 9.x first

Declaring ::setUp without a void return typehint in Drupal\Tests\comment\Kernel\CommentBaseFieldTest is deprecated in drupal:9.0.0. Typehinting will be required before drupal:10.0.0. See https://www.drupal.org/node/3114724

mrinalini9’s picture

Assigned: mrinalini9 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new5.44 KB
new530 bytes

Updated patch and fixed test case failure issue by adding protected function setUp(): void {, please review.

jurgenhaas’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed the revised patch, looking good.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 39: 2855068-39-9.x.patch, failed testing. View results

andypost’s picture

Status: Needs work » Reviewed & tested by the community

Looks unrelated fail Drupal\Tests\media_library\FunctionalJavascript\WidgetUploadTest

larowlan’s picture

Issue tags: +Bug Smash Initiative

Reviewed this as part of bug-smash, RTBC+1

  • catch committed 65e5258 on 9.1.x
    Issue #2855068 by jian he, larowlan, andypost, mrinalini9, Jody Lynn,...
catch’s picture

Title: Can't create comments when comment is a base field » [backport] Can't create comments when comment is a base field
Version: 9.1.x-dev » 8.9.x-dev

Committed 65e5258 and pushed to 9.1.x. Thanks!

This can be backported to 9.0.x and 8.9.x once the branches are unfrozen from this week's release.

  • catch committed 27ab4c6 on 9.0.x
    Issue #2855068 by jian he, larowlan, andypost, mrinalini9, Jody Lynn,...

  • catch committed 9892b8d on 8.9.x
    Issue #2855068 by jian he, larowlan, andypost, mrinalini9, Jody Lynn,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Backported to 9.0.x and 8.9.x.

  • alexpott committed 217ca47 on 8.9.x
    Issue #2855068 hotfix: Can't create comments when comment is a base...

Status: Fixed » Closed (fixed)

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

tr’s picture

The commits in #44 and #46 introduced a regression - they use public static $modules in the test instead of protected static $modules.

I posted the fixes in #3164721: More uses of public static $modules - please review!