From 68c0fd81dc4bccf6ba8529b205263bb3f376a464 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 20 Sep 2018 15:47:52 +0100 Subject: [PATCH] Issue #2922487 by Wim Leers, Jo Fitzgerald, Berdir, tedbow, borisson_, dawehner, alexpott: Follow-up for #2910211: fix all deprecation warnings --- .../src/Normalizer/ContentEntityNormalizer.php | 14 +++++++++++--- .../hal/src/Normalizer/FileEntityNormalizer.php | 17 +++++++++++++++++ .../EntityReferenceFieldItemNormalizer.php | 10 ++++++++-- .../EntityReferenceFieldItemNormalizerTest.php | 14 ++++++++++++-- .../Listeners/DeprecationListenerTrait.php | 1 - 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index 4a9f7c440d7..1ba859ed9c7 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -179,17 +179,25 @@ class ContentEntityNormalizer extends NormalizerBase { * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. + * @param array $context + * Normalization/serialization context. + * * @return string * The entity URI. */ - protected function getEntityUri(EntityInterface $entity) { + protected function getEntityUri(EntityInterface $entity, array $context = []) { // Some entity types don't provide a canonical link template, at least call // out to ->url(). if ($entity->isNew() || !$entity->hasLinkTemplate('canonical')) { return $entity->url('canonical', []); } - $url = $entity->urlInfo('canonical', ['absolute' => TRUE]); - return $url->setRouteParameter('_format', 'hal_json')->toString(); + $url = $entity->toUrl('canonical', ['absolute' => TRUE]); + if (!$url->isExternal()) { + $url->setRouteParameter('_format', 'hal_json'); + } + $generated_url = $url->toString(TRUE); + $this->addCacheableDependency($context, $generated_url); + return $generated_url->getGeneratedUrl(); } /** diff --git a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php index 5aaed3cdcbb..ec73f95f8a8 100644 --- a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php @@ -3,8 +3,10 @@ namespace Drupal\hal\Normalizer; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\file\FileInterface; use Drupal\hal\LinkManager\LinkManagerInterface; /** @@ -69,4 +71,19 @@ class FileEntityNormalizer extends ContentEntityNormalizer { return $data; } + /** + * {@inheritdoc} + */ + protected function getEntityUri(EntityInterface $entity, array $context = []) { + assert($entity instanceof FileInterface); + // https://www.drupal.org/project/drupal/issues/2277705 introduced a hack + // in \Drupal\file\Entity\File::url(), but EntityInterface::url() was + // deprecated in favor of ::toUrl(). The parent implementation now calls + // ::toUrl(), but this normalizer (for File entities) needs to override that + // back to the old behavior because it relies on said hack, not just to + // generate the value for the 'uri' field of a file (see ::normalize()), but + // also for the HAL normalization's '_links' value. + return file_create_url($entity->getFileUri()); + } + } diff --git a/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php b/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php index 4865c909795..c5b869cb860 100644 --- a/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php +++ b/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php @@ -55,8 +55,14 @@ class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer { // 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; + if ($entity->hasLinkTemplate('canonical') && $url = $entity->toUrl('canonical')->toString(TRUE)) { + $this->addCacheableDependency($context, $url); + $values['url'] = $url->getGeneratedUrl(); + } + // @todo Remove in https://www.drupal.org/project/drupal/issues/2925520 + // @see \Drupal\hal\Normalizer\FileEntityNormalizer + elseif ($entity->getEntityTypeId() === 'file') { + $values['url'] = file_create_url($entity->getFileUri()); } } diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php index 96274a675fd..a2cf6351bba 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\serialization\Unit\Normalizer; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\GeneratedUrl; use Drupal\Core\TypedData\Type\IntegerInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\Entity\EntityRepositoryInterface; @@ -11,6 +12,7 @@ use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; +use Drupal\Core\Url; use Drupal\locale\StringInterface; use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer; use Drupal\Tests\UnitTestCase; @@ -106,9 +108,17 @@ class EntityReferenceFieldItemNormalizerTest extends UnitTestCase { public function testNormalize() { $test_url = '/test/100'; + $generated_url = (new GeneratedUrl())->setGeneratedUrl($test_url); + + $url = $this->prophesize(Url::class); + $url->toString(TRUE) + ->willReturn($generated_url); + $entity = $this->prophesize(EntityInterface::class); - $entity->url('canonical') - ->willReturn($test_url) + $entity->hasLinkTemplate('canonical') + ->willReturn(TRUE); + $entity->toUrl('canonical') + ->willReturn($url) ->shouldBeCalled(); $entity->uuid() ->willReturn('080e3add-f9d5-41ac-9821-eea55b7b42fb') diff --git a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php index 988344a87cd..3352ae4628d 100644 --- a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php +++ b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php @@ -125,7 +125,6 @@ trait DeprecationListenerTrait { 'CommentType is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\node\Plugin\migrate\source\d7\NodeType instead.', 'CommentVariablePerCommentType is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\node\Plugin\migrate\source\d6\NodeType instead.', 'The Drupal\migrate_drupal\Plugin\migrate\source\d6\i18nVariable is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use Drupal\migrate_drupal\Plugin\migrate\source\d6\VariableTranslation', - 'Implicit cacheability metadata bubbling (onto the global render context) in normalizers is deprecated since Drupal 8.5.0 and will be removed in Drupal 9.0.0. Use the "cacheability" serialization context instead, for explicit cacheability metadata bubbling. See https://www.drupal.org/node/2918937', 'Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.', 'The "serializer.normalizer.file_entity.hal" normalizer service is deprecated: it is obsolete, it only remains available for backwards compatibility.', 'The Symfony\Component\ClassLoader\ApcClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use `composer install --apcu-autoloader` instead.',