Commit 150cbcf6 authored by catch's avatar catch

Issue #2374087 by alexpott, benjy: Fixed Create a persistent comment body field storage.

parent 8c3365d9
langcode: en
status: true
dependencies:
module:
- comment
- text
id: comment.comment_body
field_name: comment_body
entity_type: comment
type: text_long
settings: { }
module: text
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: true
......@@ -217,24 +217,13 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment',
* {@inheritdoc}
*/
public function addBodyField($comment_type_id) {
// Create the field if needed.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
if (!$field_storage) {
$field_storage = $this->entityManager->getStorage('field_storage_config')->create(array(
'field_name' => 'comment_body',
'type' => 'text_long',
'entity_type' => 'comment',
));
$field_storage->save();
}
if (!FieldConfig::loadByName('comment', $comment_type_id, 'comment_body')) {
// Attaches the body field by default.
$field = $this->entityManager->getStorage('field_config')->create(array(
'field_name' => 'comment_body',
'label' => 'Comment',
'entity_type' => 'comment',
'bundle' => $comment_type_id,
'required' => TRUE,
'field_storage' => FieldStorageConfig::loadByName('comment', 'comment_body'),
));
$field->save();
......
......@@ -34,13 +34,21 @@ class CommentTypeForm extends EntityForm {
*/
protected $logger;
/**
* The comment manager.
*
* @var \Drupal\comment\CommentManagerInterface
*/
protected $commentManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('logger.factory')->get('comment')
$container->get('logger.factory')->get('comment'),
$container->get('comment.manager')
);
}
......@@ -51,10 +59,13 @@ public static function create(ContainerInterface $container) {
* The entity manager service.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\comment\CommentManagerInterface $comment_manager
* The comment manager.
*/
public function __construct(EntityManagerInterface $entity_manager, LoggerInterface $logger) {
public function __construct(EntityManagerInterface $entity_manager, LoggerInterface $logger, CommentManagerInterface $comment_manager) {
$this->entityManager = $entity_manager;
$this->logger = $logger;
$this->commentManager = $comment_manager;
}
/**
......@@ -156,6 +167,7 @@ public function save(array $form, FormStateInterface $form_state) {
$this->logger->notice('Comment type %label has been updated.', array('%label' => $comment_type->label(), 'link' => $edit_link));
}
else {
$this->commentManager->addBodyField($comment_type->id());
drupal_set_message(t('Comment type %label has been added.', array('%label' => $comment_type->label())));
$this->logger->notice('Comment type %label has been added.', array('%label' => $comment_type->label(), 'link' => $edit_link));
}
......
......@@ -92,14 +92,4 @@ public function getTargetEntityTypeId() {
return $this->target_entity_type_id;
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update && !$this->isSyncing()) {
\Drupal::service('comment.manager')->addBodyField($this->id());
}
}
}
......@@ -40,7 +40,6 @@ protected function setUp() {
// Install tables and config needed to render comments.
$this->installSchema('comment', array('comment_entity_statistics'));
$this->installEntitySchema('comment');
$this->installConfig(array('system', 'filter'));
// Comment rendering generates links, so build the router.
......
......@@ -78,7 +78,6 @@ class CommentFieldAccessTest extends EntityUnitTestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(array('user'));
$this->installEntitySchema('comment');
$this->installSchema('comment', array('comment_entity_statistics'));
}
......
......@@ -40,9 +40,10 @@ function testCommentDefaultFields() {
$field->delete();
// Check that the 'comment_body' field is deleted.
// Check that the 'comment_body' field is not deleted since it is persisted
// even if it has no fields.
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$this->assertTrue(empty($field_storage), 'The comment_body field was deleted');
$this->assertTrue($field_storage, 'The comment_body field storage was not deleted');
// Create a new content type.
$type_name = 'test_node_type_2';
......
......@@ -36,6 +36,8 @@ protected function setUp() {
parent::setUp();
$this->installEntitySchema('comment');
$this->installSchema('comment', array('comment_entity_statistics'));
// Create the comment body field storage.
$this->installConfig(array('field'));
}
/**
......
......@@ -109,6 +109,7 @@ public function testCommentTypeEditing() {
$this->assertRaw('Bar', 'New name was displayed.');
$this->clickLink('Manage fields');
$this->assertUrl(\Drupal::url('field_ui.overview_comment', array('comment_type' => 'comment'), array('absolute' => TRUE)), [], 'Original machine name was used in URL.');
$this->assertTrue($this->cssSelect('tr#comment-body'), 'Body field exists.');
// Remove the body field.
$this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete', array(), t('Delete'));
......@@ -116,7 +117,7 @@ public function testCommentTypeEditing() {
$this->drupalPostForm('admin/structure/comment/manage/comment', array(), t('Save'));
// Check that the body field doesn't exist.
$this->drupalGet('admin/structure/comment/manage/comment/fields');
$this->assertNoRaw('comment_body', 'Body field was not found.');
$this->assertFalse($this->cssSelect('tr#comment-body'), 'Body field does not exist.');
}
/**
......
......@@ -29,7 +29,6 @@ class CommentValidationTest extends EntityUnitTestBase {
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('comment');
$this->installSchema('comment', array('comment_entity_statistics'));
}
......
langcode: en
status: true
dependencies: { }
id: comment_forum
label: Comment_forum
target_entity_type_id: node
description: 'Default comment field'
langcode: en
status: true
dependencies:
config:
- comment.type.comment_forum
- field.field.comment.comment_forum.comment_body
module:
- text
id: comment.comment_forum.default
targetEntityType: comment
bundle: comment_forum
mode: default
content:
author:
weight: -2
subject:
type: string_textfield
weight: 10
settings:
size: 60
placeholder: ''
third_party_settings: { }
comment_body:
type: text_textarea
weight: 11
settings:
rows: 5
placeholder: ''
third_party_settings: { }
hidden: { }
third_party_settings: { }
langcode: en
status: true
dependencies:
config:
- comment.type.comment_forum
- field.field.comment.comment_forum.comment_body
module:
- text
id: comment.comment_forum.default
label: null
targetEntityType: comment
bundle: comment_forum
mode: default
content:
comment_body:
label: hidden
type: text_default
weight: 0
settings: { }
third_party_settings: { }
links:
weight: 100
hidden: { }
third_party_settings: { }
langcode: en
status: true
dependencies:
config:
- comment.type.comment_forum
- field.storage.comment.comment_body
module:
- text
id: comment.comment_forum.comment_body
field_name: comment_body
entity_type: comment
bundle: comment_forum
label: Comment
description: ''
required: true
translatable: true
default_value: { }
default_value_callback: ''
settings: { }
third_party_settings: { }
field_type: text_long
langcode: en
status: true
dependencies:
config:
- field.storage.node.comment_forum
- node.type.forum
module:
- comment
id: node.forum.comment_forum
field_name: comment_forum
entity_type: node
bundle: forum
label: Comments
description: ''
required: true
translatable: true
default_value:
-
status: 2
cid: 0
last_comment_name: null
last_comment_timestamp: 0
last_comment_uid: 0
comment_count: 0
default_value_callback: ''
settings:
default_mode: 0
per_page: 50
form_location: true
anonymous: 0
preview: 1
third_party_settings: { }
field_type: comment
langcode: en
status: true
dependencies:
module:
- comment
- node
id: node.comment_forum
field_name: comment_forum
entity_type: node
type: comment
settings:
comment_type: comment_forum
module: comment
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
......@@ -5,10 +5,7 @@
* Install, update, and uninstall functions for the Forum module.
*/
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\comment\CommentManagerInterface;
use Drupal\taxonomy\Entity\Term;
/**
......@@ -32,29 +29,6 @@ function forum_install() {
'forum_container' => 0,
));
$term->save();
// Add the comment field to the forum node type.
$field_storages = entity_load_multiple_by_properties('field_storage_config', array(
'type' => 'comment',
'field_name' => 'comment_forum',
'include_deleted' => FALSE,
));
if (empty($field_storages)) {
Drupal::service('comment.manager')->addDefaultField('node', 'forum', 'comment_forum', CommentItemInterface::OPEN, 'comment_forum');
// Add here because we don't have param in addDefaultField function.
$field = FieldConfig::loadByName('node', 'forum', 'comment_forum');
$field->settings['default_mode'] = CommentManagerInterface::COMMENT_MODE_FLAT;
$field->save();
// Hide label for comment field.
entity_get_display('node', 'forum', 'default')
->setComponent('comment_forum', array(
'label' => 'hidden',
'type' => 'comment_default',
'weight' => 20,
))
->save();
}
}
}
......
......@@ -30,7 +30,6 @@ protected function setUp() {
\Drupal::service('router.builder')->rebuild();
$this->installSchema('system', array('sequences'));
$this->installSchema('comment', array('comment_entity_statistics'));
$this->installEntitySchema('comment');
$this->installEntitySchema('taxonomy_term');
}
......
......@@ -64,20 +64,19 @@ protected function setUp() {
$this->installSchema('system', array('url_alias', 'router'));
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test');
// If the concrete test sub-class installs node.module, ensure that the node
// entity schema is created before the field configurations are installed,
// because the node entity tables need to be created before the body field
// storage tables. This prevents trying to create the body field tables
// twice.
// If the concrete test sub-class installs the Node or Comment modules,
// ensure that the node and comment entity schema are created before the
// field configurations are installed. This is because the entity tables
// need to be created before the body field storage tables. This prevents
// trying to create the body field tables twice.
$class = get_class($this);
while ($class) {
if (property_exists($class, 'modules')) {
// Only check the modules, if the $modules property was not inherited.
$rp = new \ReflectionProperty($class, 'modules');
if ($rp->class == $class) {
if (in_array('node', $class::$modules, TRUE)) {
$this->installEntitySchema('node');
break;
foreach (array_intersect(array('node', 'comment'), $class::$modules) as $module) {
$this->installEntitySchema($module);
}
}
}
......
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\destination\EntityCommentType.
*/
namespace Drupal\migrate\Plugin\migrate\destination;
use Drupal\migrate\Row;
/**
* @MigrateDestination(
* id = "entity:comment_type"
* )
*/
class EntityCommentType extends EntityConfigBase {
/**
* {@inheritdoc}
*/
public function import(Row $row, array $old_destination_id_values = array()) {
$entity_ids = parent::import($row, $old_destination_id_values);
\Drupal::service('comment.manager')->addBodyField(reset($entity_ids));
return $entity_ids;
}
}
......@@ -28,16 +28,13 @@ protected function setUp() {
parent::setUp();
entity_create('node_type', array('type' => 'page'))->save();
entity_create('node_type', array('type' => 'story'))->save();
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
))->save();
\Drupal::service('comment.manager')->addDefaultField('node', 'story');
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
'id' => 'comment_no_subject',
'label' => 'comment_no_subject',
'target_entity_type_id' => 'node',
))->save();
\Drupal::service('comment.manager')->addBodyField('comment_no_subject');
$node = entity_create('node', array(
'type' => 'story',
......@@ -55,7 +52,6 @@ protected function setUp() {
);
$this->prepareMigrations($id_mappings);
\Drupal::service('comment.manager')->addDefaultField('node', 'story');
/** @var \Drupal\migrate\entity\Migration $migration */
$migration = entity_load('migration', 'd6_comment');
......
......@@ -44,8 +44,6 @@ class EntityCrudHookTest extends EntityUnitTestBase {
protected function setUp() {
parent::setUp();
$this->installEntitySchema('comment');
$this->installSchema('user', array('users_data'));
$this->installSchema('file', array('file_usage'));
$this->installSchema('node', array('node_access'));
......
......@@ -47,20 +47,19 @@ protected function setUp() {
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test');
// If the concrete test sub-class installs node.module, ensure that the node
// entity schema is created before the field configurations are installed,
// because the node entity tables need to be created before the body field
// storage tables. This prevents trying to create the body field tables
// twice.
// If the concrete test sub-class installs the Node or Comment modules,
// ensure that the node and comment entity schema are created before the
// field configurations are installed. This is because the entity tables
// need to be created before the body field storage tables. This prevents
// trying to create the body field tables twice.
$class = get_class($this);
while ($class) {
if (property_exists($class, 'modules')) {
// Only check the modules, if the $modules property was not inherited.
$rp = new \ReflectionProperty($class, 'modules');
if ($rp->class == $class) {
if (in_array('node', $class::$modules, TRUE)) {
$this->installEntitySchema('node');
break;
foreach (array_intersect(array('node', 'comment'), $class::$modules) as $module) {
$this->installEntitySchema($module);
}
}
}
......
langcode: en
status: true
dependencies:
config:
- comment.type.comment
- field.field.comment.comment.comment_body
module:
- text
id: comment.comment.default
targetEntityType: comment
bundle: comment
mode: default
content:
author:
weight: -2
subject:
type: string_textfield
weight: 10
settings:
size: 60
placeholder: ''
third_party_settings: { }
comment_body:
type: text_textarea
weight: 11
settings:
rows: 5
placeholder: ''
third_party_settings: { }
hidden: { }
third_party_settings: { }
langcode: en
status: true
dependencies:
config:
- comment.type.comment
- field.field.comment.comment.comment_body
module:
- text
id: comment.comment.default
label: null
targetEntityType: comment
bundle: comment
mode: default
content:
comment_body:
label: hidden
type: text_default
weight: 0
settings: { }
third_party_settings: { }
links:
weight: 100
hidden: { }
third_party_settings: { }
langcode: en
status: true
dependencies:
config:
- comment.type.comment
- field.storage.comment.comment_body
module:
- text
id: comment.comment.comment_body
field_name: comment_body
entity_type: comment
bundle: comment
label: Comment
description: ''
required: true
translatable: true
default_value: { }
default_value_callback: ''
settings: { }
third_party_settings: { }
field_type: text_long
langcode: en
status: true
dependencies:
config:
- field.storage.node.comment
- node.type.article
module:
- comment
id: node.article.comment
field_name: comment
entity_type: node
bundle: article
label: Comments
description: ''
required: true
translatable: true
default_value:
-
status: 2
cid: 0
last_comment_name: null
last_comment_timestamp: 0
last_comment_uid: 0
comment_count: 0
default_value_callback: ''
settings:
default_mode: 1
per_page: 50
form_location: true
anonymous: 0
preview: 1
third_party_settings: { }
field_type: comment
langcode: en
status: true
dependencies:
module:
- comment
- node
id: node.comment
field_name: comment
entity_type: node
type: comment
settings:
comment_type: comment
module: comment
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
langcode: und
status: true
dependencies:
config:
- comment.type.comment
module:
- comment
id: comment.comment
targetEntityType: comment
bundle: comment
......@@ -23,9 +30,4 @@ fieldMappings:
uid:
properties:
- 'schema:author'
mapping_type: 'rel'
dependencies:
module:
- comment
config:
- comment.type.comment
mapping_type: rel
......@@ -23,14 +23,6 @@ function standard_install() {
// Set front page to "node".
\Drupal::config('system.site')->set('page.front', 'node')->save();
// Add comment field to article node type.