Commit ada4e570 authored by effulgentsia's avatar effulgentsia

Issue #2060677 by damiankloip, dawehner, Wim Leers, larowlan: Add target_type,...

Issue #2060677 by damiankloip, dawehner, Wim Leers, larowlan: Add target_type, target_uuid to serialized output of entity reference fields in non-HAL formats
parent 090ae73c
......@@ -27,12 +27,18 @@ class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
public function normalize($field_item, $format = NULL, array $context = []) {
$values = parent::normalize($field_item, $format, $context);
// Add a 'url' value if there is a reference and a canonical URL. Hard code
// 'canonical' here as config entities override the default $rel parameter
// value to 'edit-form.
/** @var \Drupal\Core\Entity\EntityInterface $entity */
if (($entity = $field_item->get('entity')->getValue()) && ($url = $entity->url('canonical'))) {
$values['url'] = $url;
if ($entity = $field_item->get('entity')->getValue()) {
$values['target_type'] = $entity->getEntityTypeId();
// Add the target entity UUID to the normalized output values.
$values['target_uuid'] = $entity->uuid();
// Add a 'url' value if there is a reference and a canonical URL. Hard
// code 'canonical' here as config entities override the default $rel
// parameter value to 'edit-form.
if ($url = $entity->url('canonical')) {
$values['url'] = $url;
}
}
return $values;
......
......@@ -116,6 +116,8 @@ public function testNormalize() {
'user_id' => array(
array(
'target_id' => $this->user->id(),
'target_type' => $this->user->getEntityTypeId(),
'target_uuid' => $this->user->uuid(),
'url' => $this->user->url(),
),
),
......@@ -190,7 +192,7 @@ public function testSerialize() {
'name' => '<name><value>' . $this->values['name'] . '</value></name>',
'type' => '<type><value>entity_test_mulrev</value></type>',
'created' => '<created><value>' . $this->entity->created->value . '</value></created>',
'user_id' => '<user_id><target_id>' . $this->user->id() . '</target_id><url>' . $this->user->url() . '</url></user_id>',
'user_id' => '<user_id><target_id>' . $this->user->id() . '</target_id><target_type>' . $this->user->getEntityTypeId() . '</target_type><target_uuid>' . $this->user->uuid() . '</target_uuid><url>' . $this->user->url() . '</url></user_id>',
'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>',
......
......@@ -78,6 +78,12 @@ public function testNormalize() {
$entity->url('canonical')
->willReturn($test_url)
->shouldBeCalled();
$entity->uuid()
->willReturn('080e3add-f9d5-41ac-9821-eea55b7b42fb')
->shouldBeCalled();
$entity->getEntityTypeId()
->willReturn('test_type')
->shouldBeCalled();
$entity_reference = $this->prophesize(TypedDataInterface::class);
$entity_reference->getValue()
......@@ -92,6 +98,8 @@ public function testNormalize() {
$expected = [
'target_id' => ['value' => 'test'],
'target_type' => 'test_type',
'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
'url' => $test_url,
];
$this->assertSame($expected, $normalized);
......
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