Commit 0263085f authored by webchick's avatar webchick

Issue #2233157 by blueminds, Jalandhar: Make the comment entity_id be a reference field.

parent e6ac9ec5
......@@ -216,7 +216,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->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);
......@@ -302,6 +302,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
return $fields;
}
/**
* {@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}
*/
......@@ -321,16 +331,14 @@ public function getParentComment() {
* {@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;
}
/**
......
......@@ -39,6 +39,7 @@ public static function getInfo() {
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'));
}
/**
......
......@@ -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 @@ function setUp() {
\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 @@ public function testTerm() {
}
}
/**
* 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());
}
}
}
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