Unverified Commit 147357e3 authored by alexpott's avatar alexpott

Issue #2933012 by seanB, axel.rutz, marcoscano, alexpott, Berdir: Expose the...

Issue #2933012 by seanB, axel.rutz, marcoscano, alexpott, Berdir: Expose the filename as metadata for file based media sources

(cherry picked from commit a061dd7e)
parent f28f5620
......@@ -22,6 +22,13 @@
*/
class File extends MediaSourceBase {
/**
* Key for "Name" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_NAME = 'name';
/**
* Key for "MIME type" metadata attribute.
*
......@@ -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 @@ public function getMetadata(MediaInterface $media, $attribute_name) {
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);
......
......@@ -19,6 +19,7 @@ public function testMediaFileSource() {
$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 @@ public function testMediaFileSource() {
$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 @@ public function testMediaFileSource() {
$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.");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment