summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2014-04-18 20:41:57 (GMT)
committerwebchick2014-04-18 20:41:57 (GMT)
commit0263085f270c3060f148250d36334b1eaf98987c (patch)
tree9f6b8f90dfd1e319e320980cfe31c573bcb91a7c
parente6ac9ec5694c0ef0e8cba16302a1250f2e4c0113 (diff)
Issue #2233157 by blueminds, Jalandhar: Make the comment entity_id be a reference field.
-rw-r--r--core/modules/comment/lib/Drupal/comment/Entity/Comment.php18
-rw-r--r--core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php1
-rw-r--r--core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php58
3 files changed, 71 insertions, 6 deletions
diff --git a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php
index f757a96..eb09d5c 100644
--- a/core/modules/comment/lib/Drupal/comment/Entity/Comment.php
+++ b/core/modules/comment/lib/Drupal/comment/Entity/Comment.php
@@ -216,7 +216,7 @@ class Comment extends ContentEntityBase implements CommentInterface {
->setDescription(t('The parent comment ID if this is a reply to a comment.'))
->setSetting('target_type', 'comment');
- $fields['entity_id'] = FieldDefinition::create('integer')
+ $fields['entity_id'] = FieldDefinition::create('entity_reference')
->setLabel(t('Entity ID'))
->setDescription(t('The ID of the entity of which this comment is a reply.'))
->setRequired(TRUE);
@@ -305,6 +305,16 @@ class Comment extends ContentEntityBase implements CommentInterface {
/**
* {@inheritdoc}
*/
+ public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
+ list($target_type) = explode('__', $bundle, 2);
+ $fields['entity_id'] = clone $base_field_definitions['entity_id'];
+ $fields['entity_id']->setSetting('target_type', $target_type);
+ return $fields;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function hasParentComment() {
$parent = $this->get('pid')->entity;
return !empty($parent);
@@ -321,16 +331,14 @@ class Comment extends ContentEntityBase implements CommentInterface {
* {@inheritdoc}
*/
public function getCommentedEntity() {
- $entity_id = $this->getCommentedEntityId();
- $entity_type = $this->getCommentedEntityTypeId();
- return entity_load($entity_type, $entity_id);
+ return $this->get('entity_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getCommentedEntityId() {
- return $this->get('entity_id')->value;
+ return $this->get('entity_id')->target_id;
}
/**
diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php
index 881c965..03a0e82 100644
--- a/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php
+++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentValidationTest.php
@@ -39,6 +39,7 @@ class CommentValidationTest extends EntityUnitTestBase {
public function setUp() {
parent::setUp();
$this->installSchema('node', array('node', 'node_field_data', 'node_field_revision', 'node_revision'));
+ $this->installSchema('comment', array('comment_entity_statistics'));
}
/**
diff --git a/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php b/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php
index 42bfc4f..bfbe6f1 100644
--- a/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php
+++ b/core/modules/hal/lib/Drupal/hal/Tests/EntityTest.php
@@ -17,7 +17,7 @@ class EntityTest extends NormalizerTestBase {
*
* @var array
*/
- public static $modules = array('node', 'taxonomy');
+ public static $modules = array('node', 'taxonomy', 'comment');
/**
* {@inheritdoc}
@@ -39,6 +39,7 @@ class EntityTest extends NormalizerTestBase {
\Drupal::service('router.builder')->rebuild();
$this->installSchema('system', array('sequences'));
$this->installSchema('node', array('node', 'node_field_data', 'node_revision', 'node_field_revision'));
+ $this->installSchema('comment', array('comment', 'comment_entity_statistics'));
$this->installSchema('user', array('users_roles'));
$this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy'));
}
@@ -118,4 +119,59 @@ class EntityTest extends NormalizerTestBase {
}
}
+ /**
+ * Tests the normalization of comments.
+ */
+ public function testComment() {
+ $node_type = entity_create('node_type', array('type' => 'example_type'));
+ $node_type->save();
+
+ $user = entity_create('user', array('name' => $this->randomName()));
+ $user->save();
+
+ $node = entity_create('node', array(
+ 'title' => $this->randomName(),
+ 'uid' => $user->id(),
+ 'type' => $node_type->id(),
+ 'status' => NODE_PUBLISHED,
+ 'promote' => 1,
+ 'sticky' => 0,
+ 'body' => array(
+ 'value' => $this->randomName(),
+ 'format' => $this->randomName(),
+ )
+ ));
+ $node->save();
+
+ $this->container->get('comment.manager')->addDefaultField('node', 'example_type');
+
+ $comment = entity_create('comment', array(
+ 'uid' => $user->id(),
+ 'subject' => $this->randomName(),
+ 'comment_body' => $this->randomName(),
+ 'entity_id' => $node->id(),
+ 'entity_type' => 'node',
+ 'field_name' => 'comment'
+ ));
+ $comment->save();
+
+ $original_values = $comment->toArray();
+ unset($original_values['cid']);
+
+ $normalized = $this->serializer->normalize($comment, $this->format);
+ $denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format);
+
+ // Verify that the ID and revision ID were skipped by the normalizer.
+ $this->assertEqual(NULL, $denormalized_comment->id());
+
+ // Loop over the remaining fields and verify that they are identical.
+ foreach ($original_values as $field_name => $field_values) {
+ // The target field comes with revision id which is not set.
+ if (array_key_exists('revision_id', $field_values[0])) {
+ unset($field_values[0]['revision_id']);
+ }
+ $this->assertEqual($field_values, $denormalized_comment->get($field_name)->getValue());
+ }
+ }
+
}