summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-07-20 18:52:11 (GMT)
committerAlex Pott2018-07-20 18:52:24 (GMT)
commit147357e302d09b405bf8f3eb75bc0e3e381b69ac (patch)
tree2df1994031a249f8edddb0db55b6987e81aa0f68
parentf28f56207da4ea7db575cdf766039f872fe56f84 (diff)
Issue #2933012 by seanB, axel.rutz, marcoscano, alexpott, Berdir: Expose the filename as metadata for file based media sources
(cherry picked from commit a061dd7edd0beeed9dc52d7c31eba9b8d61b0dea)
-rw-r--r--core/modules/media/src/Plugin/media/Source/File.php19
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php19
2 files changed, 33 insertions, 5 deletions
diff --git a/core/modules/media/src/Plugin/media/Source/File.php b/core/modules/media/src/Plugin/media/Source/File.php
index 4806677..1c963ef 100644
--- a/core/modules/media/src/Plugin/media/Source/File.php
+++ b/core/modules/media/src/Plugin/media/Source/File.php
@@ -23,6 +23,13 @@ use Drupal\media\MediaSourceBase;
class File extends MediaSourceBase {
/**
+ * Key for "Name" metadata attribute.
+ *
+ * @var string
+ */
+ const METADATA_ATTRIBUTE_NAME = 'name';
+
+ /**
* Key for "MIME type" metadata attribute.
*
* @var string
@@ -41,6 +48,7 @@ class File extends MediaSourceBase {
*/
public function getMetadataAttributes() {
return [
+ static::METADATA_ATTRIBUTE_NAME => $this->t('Name'),
static::METADATA_ATTRIBUTE_MIME => $this->t('MIME type'),
static::METADATA_ATTRIBUTE_SIZE => $this->t('File size'),
];
@@ -57,15 +65,16 @@ class File extends MediaSourceBase {
return parent::getMetadata($media, $attribute_name);
}
switch ($attribute_name) {
- case 'mimetype':
+ case static::METADATA_ATTRIBUTE_NAME:
+ case 'default_name':
+ return $file->getFilename();
+
+ case static::METADATA_ATTRIBUTE_MIME:
return $file->getMimeType();
- case 'filesize':
+ case static::METADATA_ATTRIBUTE_SIZE:
return $file->getSize();
- case 'default_name':
- return $file->getFilename();
-
case 'thumbnail_uri':
return $this->getThumbnail($file) ?: parent::getMetadata($media, $attribute_name);
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
index 55ef4dd..708a941 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
@@ -19,6 +19,7 @@ class MediaSourceFileTest extends MediaSourceTestBase {
$media_type_id = 'test_media_file_type';
$source_field_id = 'field_media_file';
$provided_fields = [
+ File::METADATA_ATTRIBUTE_NAME,
File::METADATA_ATTRIBUTE_SIZE,
File::METADATA_ATTRIBUTE_MIME,
];
@@ -40,6 +41,7 @@ class MediaSourceFileTest extends MediaSourceTestBase {
$this->hideMediaTypeFieldWidget('name', $media_type_id);
$this->drupalGet("admin/structure/media/manage/{$media_type_id}");
+ $page->selectFieldOption("field_map[" . File::METADATA_ATTRIBUTE_NAME . "]", 'name');
$page->selectFieldOption("field_map[" . File::METADATA_ATTRIBUTE_SIZE . "]", 'field_string_file_size');
$page->selectFieldOption("field_map[" . File::METADATA_ATTRIBUTE_MIME . "]", 'field_string_mime_type');
$page->pressButton('Save');
@@ -82,6 +84,23 @@ class MediaSourceFileTest extends MediaSourceTestBase {
$this->assertNotEmpty($result);
$page->pressButton('Save');
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'text--plain.png');
+
+ // Check if the mapped name is automatically updated.
+ $new_filename = $this->randomMachineName() . '.txt';
+ $new_filepath = 'public://' . $new_filename;
+ file_put_contents($new_filepath, $this->randomMachineName());
+ $this->drupalGet("media/1/edit");
+ $page->pressButton('Remove');
+ $result = $assert_session->waitForField("files[{$source_field_id}_0]");
+ $this->assertNotEmpty($result);
+ $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($new_filepath));
+ $result = $assert_session->waitForButton('Remove');
+ $this->assertNotEmpty($result);
+ $page->pressButton('Save');
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = \Drupal::entityTypeManager()->getStorage('media')->loadUnchanged(1);
+ $this->assertEquals($new_filename, $media->getName());
+ $assert_session->pageTextContains("$new_filename has been updated.");
}
}