Commit 27ab4c6e authored by catch's avatar catch

Issue #2855068 by jian he, larowlan, andypost, mrinalini9, Jody Lynn,...

Issue #2855068 by jian he, larowlan, andypost, mrinalini9, Jody Lynn, himanshu-dixit, LaravZ, jurgenhaas: Can't create comments when comment is a base field

(cherry picked from commit 65e52585)
parent 96e0a5e8
......@@ -502,8 +502,8 @@ public function setThread($thread) {
*/
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');
$fields = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($values['entity_type']);
$values['comment_type'] = $fields[$values['field_name']]->getSetting('comment_type');
}
}
......
name: 'Comment base field test'
type: module
description: 'Test comment as a base field'
package: Testing
version: VERSION
dependencies:
- drupal:comment
- drupal:entity_test
langcode: en
status: true
id: test_comment_type
label: Test comment type
target_entity_type_id: comment_test_base_field
description: 'Test comment type.'
<?php
namespace Drupal\comment_base_field_test\Entity;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\entity_test\Entity\EntityTest;
/**
* Defines a test entity class for comment as a base field.
*
* @ContentEntityType(
* id = "comment_test_base_field",
* label = @Translation("Test comment - base field"),
* base_table = "comment_test_base_field",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type"
* },
* )
*/
class CommentTestBaseField extends EntityTest {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['test_comment'] = BaseFieldDefinition::create('comment')
->setLabel(t('A comment field'))
->setSetting('comment_type', 'test_comment_type')
->setDefaultValue([
'status' => CommentItemInterface::OPEN,
]);
return $fields;
}
}
<?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment_base_field_test\Entity\CommentTestBaseField;
use Drupal\Core\Language\LanguageInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests that comment as a base field.
*
* @group comment
*/
class CommentBaseFieldTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'system',
'user',
'comment',
'comment_base_field_test',
];
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('comment_test_base_field');
$this->installEntitySchema('comment');
$this->installSchema('system', ['sequences']);
$this->installEntitySchema('user');
}
/**
* Tests comment as a base field.
*/
public function testCommentBaseField() {
// Verify entity creation.
$entity = CommentTestBaseField::create([
'name' => $this->randomMachineName(),
'test_comment' => CommentItemInterface::OPEN,
]);
$entity->save();
$comment = Comment::create([
'entity_id' => $entity->id(),
'entity_type' => 'comment_test_base_field',
'field_name' => 'test_comment',
'pid' => 0,
'uid' => 0,
'status' => CommentInterface::PUBLISHED,
'subject' => $this->randomMachineName(),
'hostname' => '127.0.0.1',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [['value' => $this->randomMachineName()]],
]);
$comment->save();
$this->assertEquals('test_comment_type', $comment->bundle());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment