summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxjm2018-01-07 17:28:18 (GMT)
committerxjm2018-01-07 17:28:18 (GMT)
commit3d0e79f229008e76a711148665d8f6f8ed6d9765 (patch)
tree446b2d70cf3f278c629d284e03a8fb142071ab16
parentdb680529531a341f5553cfba6daa7fa0c4c16ea4 (diff)
Issue #2928798 by balsama, axel.rutz, xjm, phenaproxima, chr.fritsch, jeetendrakumar: Add extractors: mimetype & filesize
-rw-r--r--core/modules/media/src/Plugin/media/Source/File.php26
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php23
2 files changed, 46 insertions, 3 deletions
diff --git a/core/modules/media/src/Plugin/media/Source/File.php b/core/modules/media/src/Plugin/media/Source/File.php
index b6ad979..0f7631e 100644
--- a/core/modules/media/src/Plugin/media/Source/File.php
+++ b/core/modules/media/src/Plugin/media/Source/File.php
@@ -23,10 +23,28 @@ use Drupal\media\MediaSourceBase;
class File extends MediaSourceBase {
/**
+ * Key for "MIME type" metadata attribute.
+ *
+ * @var string
+ */
+ const METADATA_ATTRIBUTE_MIME = 'mimetype';
+
+ /**
+ * Key for "File size" metadata attribute.
+ *
+ * @var string
+ */
+ const METADATA_ATTRIBUTE_SIZE = 'filesize';
+
+
+ /**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
- return [];
+ return [
+ static::METADATA_ATTRIBUTE_MIME => $this->t('MIME type'),
+ static::METADATA_ATTRIBUTE_SIZE => $this->t('File size'),
+ ];
}
/**
@@ -40,6 +58,12 @@ class File extends MediaSourceBase {
return parent::getMetadata($media, $attribute_name);
}
switch ($attribute_name) {
+ case 'mimetype':
+ return $file->getMimeType();
+
+ case 'filesize':
+ return $file->getSize();
+
case 'default_name':
return $file->getFilename();
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
index 06d5eb8..21954d8 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
@@ -3,6 +3,7 @@
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\media\Entity\Media;
+use Drupal\media\Plugin\media\Source\File;
/**
* Tests the file media source.
@@ -17,16 +18,32 @@ class MediaSourceFileTest extends MediaSourceTestBase {
public function testMediaFileSource() {
$media_type_id = 'test_media_file_type';
$source_field_id = 'field_media_file';
+ $provided_fields = [
+ File::METADATA_ATTRIBUTE_SIZE,
+ File::METADATA_ATTRIBUTE_MIME,
+ ];
$session = $this->getSession();
$page = $session->getPage();
$assert_session = $this->assertSession();
- $this->doTestCreateMediaType($media_type_id, 'file');
+ $this->doTestCreateMediaType($media_type_id, 'file', $provided_fields);
+
+ // Create custom fields for the media type to store metadata attributes.
+ $fields = [
+ 'field_string_file_size' => 'string',
+ 'field_string_mime_type' => 'string',
+ ];
+ $this->createMediaTypeFields($fields, $media_type_id);
// Hide the name field widget to test default name generation.
$this->hideMediaTypeFieldWidget('name', $media_type_id);
+ $this->drupalGet("admin/structure/media/manage/{$media_type_id}");
+ $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');
+
$test_filename = $this->randomMachineName() . '.txt';
$test_filepath = 'public://' . $test_filename;
file_put_contents($test_filepath, $this->randomMachineName());
@@ -50,9 +67,11 @@ class MediaSourceFileTest extends MediaSourceTestBase {
$page->checkField('revision');
$assert_session->elementAttributeNotContains('css', '.field--name-revision-log-message', 'style', 'display');
- // Load the media and check if the label was properly populated.
+ // Load the media and check that all the fields are properly populated.
$media = Media::load(1);
$this->assertEquals($test_filename, $media->getName());
+ $this->assertEquals('8', $media->get('field_string_file_size')->value);
+ $this->assertEquals('text/plain', $media->get('field_string_mime_type')->value);
// Test the MIME type icon.
$icon_base = \Drupal::config('media.settings')->get('icon_base_uri');