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?
| Comment | File | Size | Author |
|---|---|---|---|
| #39 | interdiff-2855068-35-39.txt | 530 bytes | mrinalini9 |
| #39 | 2855068-39-9.x.patch | 5.44 KB | mrinalini9 |
Comments
Comment #2
jody lynnComment #4
larowlanCan 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.
Comment #5
cilefen commentedTest this against 8.3.x.
Comment #6
cilefen commentedThis different issue popped in my head when I saw this one.
Comment #7
himanshu-dixit commentedI would like to work on this issue .
Comment #8
himanshu-dixit commentedHere is the rerolled patch which also follows the suggestion in #4 .
Comment #12
laravz commentedThe 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.
Comment #13
jian he commentedAdd the testing script.
Comment #16
jian he commentedI am not understand why the test is fail. Can someone give some suggest?
Comment #17
laravz commentedHi,
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:
Perhaps you could use "Drupal\KernelTests\KernelTestBase" instead?
Comment #18
jian he commented@LaravZ Thank you very much. I have miss selected the PhpStorm hint before.
Comment #20
jian he commentedComment #21
jian he commentedComment #23
jian he commentedComment #24
jian he commentedComment #25
jian he commentedComment #26
jian he commentedComment #27
laravz commentedPatch 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:
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.
Comment #28
larowlanWe 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.
Comment #29
larowlanComment #34
jurgenhaasI 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
Comment #35
andypostJust a re-roll for 9.x and 8.x core
Comment #37
mrinalini9 commentedComment #38
andypostAs 9.0 test fails - patches should go 9.x first
Comment #39
mrinalini9 commentedUpdated patch and fixed test case failure issue by adding
protected function setUp(): void {, please review.Comment #40
jurgenhaasReviewed the revised patch, looking good.
Comment #42
andypostLooks unrelated fail
Drupal\Tests\media_library\FunctionalJavascript\WidgetUploadTestComment #43
larowlanReviewed this as part of bug-smash, RTBC+1
Comment #45
catchCommitted 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.
Comment #48
catchBackported to 9.0.x and 8.9.x.
Comment #51
tr commentedThe commits in #44 and #46 introduced a regression - they use
public static $modulesin the test instead ofprotected static $modules.I posted the fixes in #3164721: More uses of public static $modules - please review!