diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php index dacc33e..666e42e 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php @@ -42,7 +42,7 @@ public function normalize($entity, $format = NULL, array $context = array()) { 'self' => array( 'href' => $entity_wrapper->getUri(), ), - 'site:type' => array( + 'type' => array( 'href' => $entity_wrapper->getTypeUri(), ), ), diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php index 954322f..9b35dc2 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityReferenceItemNormalizer.php @@ -26,26 +26,36 @@ class EntityReferenceItemNormalizer extends FieldItemNormalizer { */ public function normalize($field_item, $format = NULL, array $context = array()) { $target_entity = $field_item->get('entity')->getValue(); - $field_name = $field_item->getParent()->getName(); $entity_wrapper = new EntityWrapper($target_entity); - $values = array( - 'uuid' => $entity_wrapper->getUuid(), - ); + // Set up the values for _link and _embedded and add the language if one + // was passed in. + $target_entity_uri = $entity_wrapper->getUri(); $link = array( - 'href' => $entity_wrapper->getUri(), + 'href' => $target_entity_uri, + ); + $target_entity_uuid = $entity_wrapper->getUuid(); + $embedded = array( + '_links' => array( + 'self' => $target_entity_uri, + ), + 'uuid' => $target_entity_uuid, ); if (isset($context['langcode'])) { - $values['lang'] = $link['lang'] = $context['langcode']; + $embedded['lang'] = $link['lang'] = $context['langcode']; } // This structure will be recursively merged into the normalized entity so - // that the links are properly added to the _links object. + // that the items are properly added to the _links and _embedded objects. + $field_name = $field_item->getParent()->getName(); + $field_curie = "site:$field_name"; return array( '_links' => array( - "site:$field_name" => array($link), + $field_curie => array($link), + ), + '_embedded' => array( + $field_curie => array($embedded), ), - $field_name => array($values), ); } diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php index 61819f5..17cf2a4 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php @@ -24,9 +24,9 @@ public static function getInfo() { * Tests the normalize function. */ public function testNormalize() { - $target_entity_de = entity_create('entity_test', (array('field_test_entity_reference' => NULL))); + $target_entity_de = entity_create('entity_test', (array('langcode' => 'de', 'field_test_entity_reference' => NULL))); $target_entity_de->save(); - $target_entity_en = entity_create('entity_test', (array('field_test_entity_reference' => NULL))); + $target_entity_en = entity_create('entity_test', (array('langcode' => 'en', 'field_test_entity_reference' => NULL))); $target_entity_en->save(); // Create a German entity. @@ -69,7 +69,7 @@ public function testNormalize() { 'self' => array( 'href' => $this->getUri($entity), ), - 'site:type' => array( + 'type' => array( 'href' => '/types/entity_test/entity_test', ), 'site:user_id' => array( @@ -89,6 +89,30 @@ public function testNormalize() { ), ), ), + '_embedded' => array( + 'site:user_id' => array( + array( + 'href' => NULL, + 'lang' => 'de', + ), + ), + 'site:field_test_entity_reference' => array( + array( + '_links' => array( + 'self' => $this->getUri($target_entity_de), + ), + 'uuid' => $target_entity_de->uuid(), + 'lang' => 'de', + ), + array( + '_links' => array( + 'self' => $this->getUri($target_entity_en), + ), + 'uuid' => $target_entity_en->uuid(), + 'lang' => 'en', + ), + ), + ), 'uuid' => array( array( 'value' => $entity->uuid(), @@ -109,28 +133,12 @@ public function testNormalize() { 'lang' => 'en', ), ), - 'user_id' => array( - array( - 'uuid' => NULL, - 'lang' => 'de', - ), - ), 'field_test_text' => array( array( 'value' => $values['field_test_text']['value'], 'format' => $values['field_test_text']['format'], ), ), - 'field_test_entity_reference' => array( - array( - 'uuid' => $target_entity_de->uuid(), - 'lang' => 'de', - ), - array( - 'uuid' => $target_entity_en->uuid(), - 'lang' => 'en', - ), - ), ); $normalized = $this->container->get('serializer')->normalize($entity, $this->format); @@ -141,7 +149,7 @@ public function testNormalize() { $this->assertEqual($normalized['uuid'], $expected_array['uuid'], 'Non-translatable fields is normalized.'); $this->assertEqual($normalized['name'], $expected_array['name'], 'Translatable field with multiple language values is normalized.'); $this->assertEqual($normalized['field_test_text'], $expected_array['field_test_text'], 'Field with properties is normalized.'); - $this->assertEqual($normalized['field_test_entity_reference'], $expected_array['field_test_entity_reference'], 'Entity reference field is normalized.'); + $this->assertEqual($normalized['_embedded']['site:field_test_entity_reference'], $expected_array['_embedded']['site:field_test_entity_reference'], 'Entity reference field is normalized.'); $this->assertEqual($normalized['_links']['site:field_test_entity_reference'], $expected_array['_links']['site:field_test_entity_reference'], 'Links are added for entity reference field.'); }