diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 0e239b7f76..013201119a 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -52,9 +52,8 @@ function file_help($route_name, RouteMatchInterface $route_match) { * Implements hook_field_widget_info_alter(). */ function file_field_widget_info_alter(array &$info) { - // This allows setting a valid default widget in the FileUriItem annotation. - // It's not strictly needed, but it's a required value on the annotation so - // should be a valid value. + // Allows using the 'uri' widget for the 'file_uri' field type, which uses it + // as the default widget. // @see \Drupal\file\Plugin\Field\FieldType\FileUriItem $info['uri']['field_types'][] = 'file_uri'; } diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileUriItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileUriItem.php index 84f09406f3..4f0402937c 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileUriItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileUriItem.php @@ -29,7 +29,6 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel $properties['url'] = DataDefinition::create('uri') ->setLabel(t('Root-relative file URL')) - ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive')) ->setComputed(TRUE) ->setInternal(FALSE) ->setClass(ComputedFileUrl::class); diff --git a/core/modules/file/tests/src/Kernel/FileUriItemTest.php b/core/modules/file/tests/src/Kernel/FileUriItemTest.php index d67eba0707..1e87fab64a 100644 --- a/core/modules/file/tests/src/Kernel/FileUriItemTest.php +++ b/core/modules/file/tests/src/Kernel/FileUriItemTest.php @@ -33,7 +33,7 @@ public function testCustomFileUriField() { $file->save(); $this->assertSame($uri, $file->uri->value); - $expected_url = base_path() . $this->siteDirectory . '/files/druplicon.txt'; + $expected_url = file_transform_relative(file_create_url($uri)); $this->assertSame($expected_url, $file->uri->url); } diff --git a/core/modules/file/tests/src/Unit/ComputedFileUrlTest.php b/core/modules/file/tests/src/Unit/ComputedFileUrlTest.php index 8e2a4a469e..459f154ba3 100644 --- a/core/modules/file/tests/src/Unit/ComputedFileUrlTest.php +++ b/core/modules/file/tests/src/Unit/ComputedFileUrlTest.php @@ -39,7 +39,7 @@ public function testGetValue() { $typed_data = new ComputedFileUrl($definition->reveal(), $this->randomMachineName(), $parent->reveal()); - $expected = base_path() . $this->siteDirectory . '/files/druplicon.txt'; + $expected = file_transform_relative(file_create_url($this->testUrl)); $this->assertSame($expected, $typed_data->getValue()); // Do this a second time to confirm the same value is returned but the value diff --git a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php index d3b1797a57..d44f634ee6 100644 --- a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php @@ -3,6 +3,7 @@ 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\hal\LinkManager\LinkManagerInterface; @@ -11,7 +12,7 @@ /** * Converts the Drupal entity object structure to a HAL array structure. * - * @deprecated in Drupal 8.5.x, to be removed before Drupal 9.0.x. + * @deprecated in Drupal 8.5.0, to be removed before Drupal 9.0.0. */ class FileEntityNormalizer extends ContentEntityNormalizer { @@ -32,7 +33,7 @@ class FileEntityNormalizer extends ContentEntityNormalizer { /** * The hal settings config. * - * @var \Drupal\Core\Config\Config + * @var \Drupal\Core\Config\ImmutableConfig */ protected $halSettings; @@ -85,4 +86,15 @@ public function denormalize($data, $class, $format = NULL, array $context = []) return $this->entityManager->getStorage('file')->create($data); } + /** + * {@inheritdoc} + */ + protected function getEntityUri(EntityInterface $entity) { + if ($this->halSettings->get('bc_file_uri_as_url_normalizer')) { + return $entity->url('canonical', []); + } + + return file_url_transform_relative(file_create_url($entity->getFileUri())); + } + } diff --git a/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php index 2e605a00bc..f96504f591 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/File/FileHalJsonAnonTest.php @@ -34,15 +34,17 @@ class FileHalJsonAnonTest extends FileResourceTestBase { * {@inheritdoc} */ protected function getExpectedNormalizedEntity() { + $use_url_bc = $this->config('hal.settings')->get('bc_file_uri_as_url_normalizer'); $default_normalization = parent::getExpectedNormalizedEntity(); $normalization = $this->applyHalFieldNormalization($default_normalization); $url = file_create_url($this->entity->getFileUri()); // @see \Drupal\Tests\hal\Functional\EntityResource\File\FileHalJsonAnonTest::testGetBcUriField() - if ($this->config('hal.settings')->get('bc_file_uri_as_url_normalizer') === TRUE) { + if ($use_url_bc) { $normalization['uri'][0]['value'] = $url; } + $uid = $this->author->id(); return $normalization + [ @@ -67,7 +69,7 @@ protected function getExpectedNormalizedEntity() { ], '_links' => [ 'self' => [ - 'href' => $url, + 'href' => $use_url_bc ? $url : file_url_transform_relative($url), ], 'type' => [ 'href' => $this->baseUrl . '/rest/type/file/file', @@ -133,7 +135,7 @@ public function testGetBcUriField() { 1 => [ 'value' => 'Two', ], - ] + ], ]; static::recursiveKSort($expected); $actual = $this->serializer->decode((string) $response->getBody(), static::$format); @@ -142,7 +144,7 @@ public function testGetBcUriField() { // Explicitly assert that $file->uri->value is an absolute file URL, unlike // the default normalization. - $this->assertSame($this->baseUrl . '/' . $this->siteDirectory . '/files/drupal.txt', $actual['uri'][0]['value']); + $this->assertSame(file_create_url('public://drupal.txt'), $actual['uri'][0]['value']); } /** diff --git a/core/modules/hal/tests/src/Functional/FileDenormalizeTest.php b/core/modules/hal/tests/src/Functional/FileDenormalizeTest.php index dbb708f8ca..a742c49a09 100644 --- a/core/modules/hal/tests/src/Functional/FileDenormalizeTest.php +++ b/core/modules/hal/tests/src/Functional/FileDenormalizeTest.php @@ -20,6 +20,9 @@ class FileDenormalizeTest extends BrowserTestBase { */ public static $modules = ['hal', 'file', 'node']; + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp();