diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index eb45247285..10a9746053 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -76,7 +76,10 @@ public function normalize($entity, $format = NULL, array $context = []) { if (isset($context['included_fields'])) { $field_items = []; foreach ($context['included_fields'] as $field_name) { - $field_items[] = $entity->get($field_name); + // Entity labels need special handling, see below. + if ($field_name !== 'label') { + $field_items[] = $entity->get($field_name); + } } } else { @@ -92,6 +95,11 @@ public function normalize($entity, $format = NULL, array $context = []) { $normalized = NestedArray::mergeDeep($normalized, $normalized_property); } + // Special handling for normalizing entity labels. + if (isset($context['included_fields']) && in_array('label', $context['included_fields'], TRUE) && $entity->access('view label')) { + $normalized['label'] = $entity->label(); + } + return $normalized; } diff --git a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php index fbcae8c758..64af7e1a5c 100644 --- a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php @@ -64,7 +64,7 @@ public function normalize($field_item, $format = NULL, array $context = []) { // will include the langcode. $langcode = isset($context['langcode']) ? $context['langcode'] : NULL; unset($context['langcode']); - $context['included_fields'] = ['uuid']; + $context['included_fields'] = ['uuid', 'label']; // Normalize the target entity. $embedded = $this->serializer->normalize($target_entity, $format, $context); diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php b/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php index fc9248aca7..dc3210ec8b 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Comment/CommentHalJsonTestBase.php @@ -89,6 +89,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/entity_test/bar', ], ], + 'label' => $commented_entity->label(), 'uuid' => [ ['value' => $commented_entity->uuid()] ], @@ -104,6 +105,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/user/user', ], ], + 'label' => $author->label(), 'uuid' => [ ['value' => $author->uuid()] ], diff --git a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php index ef3826bea6..e7a8888080 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/EntityTest/EntityTestHalJsonAnonTest.php @@ -65,6 +65,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/user/user', ], ], + 'label' => $author->label(), 'uuid' => [ ['value' => $author->uuid()] ], diff --git a/core/modules/hal/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelHalJsonAnonTest.php index f95ea5ce06..5faed46650 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelHalJsonAnonTest.php @@ -65,6 +65,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/user/user', ], ], + 'label' => $author->label(), 'uuid' => [ [ 'value' => $author->uuid(), diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php index 24157e5979..6a4ffb8ad2 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Item/ItemHalJsonTestBase.php @@ -50,6 +50,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/aggregator_feed/aggregator_feed', ], ], + 'label' => $feed->label(), 'uuid' => [ [ 'value' => $feed->uuid(), diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Media/MediaHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Media/MediaHalJsonAnonTest.php index afa9465f53..577b102d49 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Media/MediaHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Media/MediaHalJsonAnonTest.php @@ -96,6 +96,7 @@ protected function getExpectedNormalizedEntity() { 'value' => $file->uuid(), ], ], + 'label' => $file->label(), ], ], $this->baseUrl . '/rest/relation/media/camelids/revision_user' => [ @@ -113,6 +114,7 @@ protected function getExpectedNormalizedEntity() { 'value' => $author->uuid(), ], ], + 'label' => $author->label(), ], ], $this->baseUrl . '/rest/relation/media/camelids/thumbnail' => [ @@ -136,6 +138,7 @@ protected function getExpectedNormalizedEntity() { 'value' => $thumbnail->uuid(), ], ], + 'label' => $thumbnail->label(), ], ], $this->baseUrl . '/rest/relation/media/camelids/uid' => [ @@ -154,6 +157,7 @@ protected function getExpectedNormalizedEntity() { ], ], 'lang' => 'en', + 'label' => $author->label(), ], ], ], diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php index 7bbe21830c..af2833305f 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonAnonTest.php @@ -83,6 +83,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/user/user', ], ], + 'label' => $author->label(), 'uuid' => [ ['value' => $author->uuid()] ], @@ -98,6 +99,7 @@ protected function getExpectedNormalizedEntity() { 'href' => $this->baseUrl . '/rest/type/user/user', ], ], + 'label' => $author->label(), 'uuid' => [ ['value' => $author->uuid()] ], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php index 8b67cc62d2..9318bc52fb 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/BlockContent/BlockContentResourceTestBase.php @@ -96,6 +96,7 @@ protected function getExpectedNormalizedEntity() { 'target_id' => 'basic', 'target_type' => 'block_content_type', 'target_uuid' => BlockContentType::load('basic')->uuid(), + 'label' => BlockContentType::load('basic')->label(), ], ], 'info' => [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php index bade2a76c6..836b9f9ca1 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php @@ -165,6 +165,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'user', 'target_uuid' => $author->uuid(), 'url' => base_path() . 'user/' . $author->id(), + 'label' => $author->label(), ], ], 'pid' => [], @@ -179,6 +180,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'entity_test', 'target_uuid' => EntityTest::load(1)->uuid(), 'url' => base_path() . 'entity_test/1', + 'label' => EntityTest::load(1)->label(), ], ], 'field_name' => [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php index e2c0ccdf65..bbc72552c5 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTest/EntityTestResourceTestBase.php @@ -99,6 +99,7 @@ protected function getExpectedNormalizedEntity() { ], 'user_id' => [ [ + 'label' => $author->label(), 'target_id' => (int) $author->id(), 'target_type' => 'user', 'target_uuid' => $author->uuid(), diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php index 2257d6c275..10ce3569ee 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityTestLabel/EntityTestLabelResourceTestBase.php @@ -101,6 +101,7 @@ protected function getExpectedNormalizedEntity() { ], 'user_id' => [ [ + 'label' => $author->label(), 'target_id' => (int) $author->id(), 'target_type' => 'user', 'target_uuid' => $author->uuid(), diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php index f217b97fba..496de4f744 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Item/ItemResourceTestBase.php @@ -101,6 +101,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'aggregator_feed', 'target_uuid' => $feed->uuid(), 'url' => base_path() . 'aggregator/sources/1', + 'label' => $feed->label(), ], ], 'title' => [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php index f5bfa14291..9e898fee2a 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Media/MediaResourceTestBase.php @@ -155,6 +155,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'file', 'target_uuid' => $file->uuid(), 'url' => $file->url(), + 'label' => $file->label(), ], ], 'thumbnail' => [ @@ -167,6 +168,7 @@ protected function getExpectedNormalizedEntity() { 'target_uuid' => $thumbnail->uuid(), 'title' => 'Llama', 'url' => $thumbnail->url(), + 'label' => $thumbnail->label(), ], ], 'status' => [ @@ -194,6 +196,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'user', 'target_uuid' => $author->uuid(), 'url' => base_path() . 'user/' . $author->id(), + 'label' => $author->label(), ], ], 'revision_user' => [ @@ -202,6 +205,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'user', 'target_uuid' => $author->uuid(), 'url' => base_path() . 'user/' . $author->id(), + 'label' => $author->label(), ], ], 'revision_log_message' => [], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php index b4fc553f08..c12b8dfb91 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Node/NodeResourceTestBase.php @@ -162,6 +162,7 @@ protected function getExpectedNormalizedEntity() { 'target_type' => 'user', 'target_uuid' => $author->uuid(), 'url' => base_path() . 'user/' . $author->id(), + 'label' => $author->label(), ], ], 'revision_uid' => [ @@ -169,6 +170,7 @@ protected function getExpectedNormalizedEntity() { 'target_id' => (int) $author->id(), 'target_type' => 'user', 'target_uuid' => $author->uuid(), + 'label' => $author->label(), 'url' => base_path() . 'user/' . $author->id(), ], ], diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php index 36be5b58f0..13dd68529d 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Shortcut/ShortcutResourceTestBase.php @@ -90,6 +90,7 @@ protected function getExpectedNormalizedEntity() { 'target_id' => 'default', 'target_type' => 'shortcut_set', 'target_uuid' => ShortcutSet::load('default')->uuid(), + 'label' => ShortcutSet::load('default')->label(), ], ], 'link' => [ diff --git a/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php b/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php index ea2e020bf0..705fafe82d 100644 --- a/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php +++ b/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php @@ -54,6 +54,10 @@ public function normalize($field_item, $format = NULL, array $context = []) { if ($url = $entity->url('canonical')) { $values['url'] = $url; } + + if ($entity->access('view label')) { + $values['label'] = $entity->label(); + } } return $values; } diff --git a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php index 5b71b93b7a..548a15f7a0 100644 --- a/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php +++ b/core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @@ -118,6 +118,7 @@ public function testNormalize() { 'target_type' => $this->user->getEntityTypeId(), 'target_uuid' => $this->user->uuid(), 'url' => $this->user->url(), + 'label' => $this->user->label(), ], ], 'revision_id' => [ @@ -197,7 +198,7 @@ public function testSerialize() { 'name' => '' . $this->values['name'] . '', 'type' => 'entity_test_mulrev', 'created' => '' . $expected_created['value'] . '' . $expected_created['format'] . '', - 'user_id' => '' . $this->user->id() . '' . $this->user->getEntityTypeId() . '' . $this->user->uuid() . '' . $this->user->url() . '', + 'user_id' => '' . $this->user->id() . '' . $this->user->getEntityTypeId() . '' . $this->user->uuid() . '' . $this->user->url() . '', 'revision_id' => '' . $this->entity->getRevisionId() . '', 'default_langcode' => '1', 'revision_translation_affected' => '1', diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php index e0561a1003..809a8fb0a0 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php @@ -112,6 +112,9 @@ public function testNormalize() { $entity->getEntityTypeId() ->willReturn('test_type') ->shouldBeCalled(); + $entity->access('view label') + ->willReturn(FALSE) + ->shouldBeCalled(); $entity_reference = $this->prophesize(TypedDataInterface::class); $entity_reference->getValue()