summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxjm2018-01-07 15:36:01 (GMT)
committerxjm2018-01-07 15:36:01 (GMT)
commitdb680529531a341f5553cfba6daa7fa0c4c16ea4 (patch)
tree1333c40d36623ee05904a298972f0f37ed920add
parent28830054bb760b12d3377c8fcd8c2c6850a815bc (diff)
Issue #2924631 by marcoscano, starshaped, chr.fritsch, xjm, phenaproxima, Gábor Hojtsy, seanB, yoroy, evankay: Media sources for local video and audio support
-rw-r--r--core/modules/media/config/schema/media.schema.yml8
-rw-r--r--core/modules/media/images/icons/audio.pngbin0 -> 5611 bytes
-rw-r--r--core/modules/media/images/icons/video.pngbin0 -> 4436 bytes
-rw-r--r--core/modules/media/src/Plugin/media/Source/AudioFile.php39
-rw-r--r--core/modules/media/src/Plugin/media/Source/VideoFile.php39
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php105
6 files changed, 191 insertions, 0 deletions
diff --git a/core/modules/media/config/schema/media.schema.yml b/core/modules/media/config/schema/media.schema.yml
index dad5189..b9156b2 100644
--- a/core/modules/media/config/schema/media.schema.yml
+++ b/core/modules/media/config/schema/media.schema.yml
@@ -52,6 +52,14 @@ media.source.image:
type: media.source.field_aware
label: '"Image" media source configuration'
+media.source.audio_file:
+ type: media.source.field_aware
+ label: '"Audio" media source configuration'
+
+media.source.video_file:
+ type: media.source.field_aware
+ label: '"Video" media source configuration'
+
media.source.field_aware:
type: mapping
mapping:
diff --git a/core/modules/media/images/icons/audio.png b/core/modules/media/images/icons/audio.png
new file mode 100644
index 0000000..23efc4a
--- /dev/null
+++ b/core/modules/media/images/icons/audio.png
Binary files differ
diff --git a/core/modules/media/images/icons/video.png b/core/modules/media/images/icons/video.png
new file mode 100644
index 0000000..4364cf3
--- /dev/null
+++ b/core/modules/media/images/icons/video.png
Binary files differ
diff --git a/core/modules/media/src/Plugin/media/Source/AudioFile.php b/core/modules/media/src/Plugin/media/Source/AudioFile.php
new file mode 100644
index 0000000..2229143
--- /dev/null
+++ b/core/modules/media/src/Plugin/media/Source/AudioFile.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Drupal\media\Plugin\media\Source;
+
+use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
+use Drupal\media\MediaTypeInterface;
+
+/**
+ * Media source wrapping around an audio file.
+ *
+ * @see \Drupal\file\FileInterface
+ *
+ * @MediaSource(
+ * id = "audio_file",
+ * label = @Translation("Audio file"),
+ * description = @Translation("Use audio files for reusable media."),
+ * allowed_field_types = {"file"},
+ * default_thumbnail_filename = "audio.png"
+ * )
+ */
+class AudioFile extends File {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createSourceField(MediaTypeInterface $type) {
+ return parent::createSourceField($type)->set('settings', ['file_extensions' => 'mp3 wav aac']);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
+ $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
+ 'type' => 'file_audio',
+ ]);
+ }
+
+}
diff --git a/core/modules/media/src/Plugin/media/Source/VideoFile.php b/core/modules/media/src/Plugin/media/Source/VideoFile.php
new file mode 100644
index 0000000..214d36c
--- /dev/null
+++ b/core/modules/media/src/Plugin/media/Source/VideoFile.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Drupal\media\Plugin\media\Source;
+
+use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
+use Drupal\media\MediaTypeInterface;
+
+/**
+ * Media source wrapping around a video file.
+ *
+ * @see \Drupal\file\FileInterface
+ *
+ * @MediaSource(
+ * id = "video_file",
+ * label = @Translation("Video file"),
+ * description = @Translation("Use video files for reusable media."),
+ * allowed_field_types = {"file"},
+ * default_thumbnail_filename = "video.png"
+ * )
+ */
+class VideoFile extends File {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createSourceField(MediaTypeInterface $type) {
+ return parent::createSourceField($type)->set('settings', ['file_extensions' => 'mp4']);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
+ $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
+ 'type' => 'file_video',
+ ]);
+ }
+
+}
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
new file mode 100644
index 0000000..4ad3c5b
--- /dev/null
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Drupal\Tests\media\FunctionalJavascript;
+
+use Drupal\Core\Entity\Entity\EntityViewDisplay;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\file\Entity\File;
+
+/**
+ * Tests the Audio and Video media sources.
+ *
+ * @group media
+ */
+class MediaSourceAudioVideoTest extends MediaSourceTestBase {
+
+ /**
+ * Check the Audio source functionality.
+ */
+ public function testAudioTypeCreation() {
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+
+ $source_id = 'audio_file';
+ $type_name = 'audio_type';
+ $field_name = 'field_media_' . $source_id;
+ $this->doTestCreateMediaType($type_name, $source_id);
+
+ // Check that the source field was created with the correct settings.
+ $storage = FieldStorageConfig::load("media.$field_name");
+ $this->assertInstanceOf(FieldStorageConfig::class, $storage);
+ $field = FieldConfig::load("media.$type_name.$field_name");
+ $this->assertInstanceOf(FieldConfig::class, $field);
+ $this->assertSame('mp3 wav aac', FieldConfig::load("media.$type_name.$field_name")->get('settings')['file_extensions']);
+
+ // Check that the display holds the correct formatter configuration.
+ $display = EntityViewDisplay::load("media.$type_name.default");
+ $this->assertInstanceOf(EntityViewDisplay::class, $display);
+ $formatter = $display->getComponent($field_name)['type'];
+ $this->assertSame('file_audio', $formatter);
+
+ // Create a media asset and verify that the <audio> tag is present.
+ file_put_contents('public://file.mp3', str_repeat('t', 10));
+ $file = File::create([
+ 'uri' => 'public://file.mp3',
+ 'filename' => 'file.mp3',
+ ]);
+ $file->save();
+
+ $this->drupalGet("media/add/$type_name");
+ $page->fillField('Name', 'Audio media asset');
+ $page->attachFileToField("files[{$field_name}_0]", \Drupal::service('file_system')->realpath('public://file.mp3'));
+ $result = $assert_session->waitForButton('Remove');
+ $this->assertNotEmpty($result);
+ $page->pressButton('Save');
+
+ $assert_session->pageTextContains("$type_name Audio media asset has been created.");
+ $assert_session->elementExists('css', "audio > source[type='audio/mpeg']");
+ }
+
+ /**
+ * Check the Video source functionality.
+ */
+ public function testVideoTypeCreation() {
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+
+ $source_id = 'video_file';
+ $type_name = 'video_type';
+ $field_name = 'field_media_' . $source_id;
+ $this->doTestCreateMediaType($type_name, $source_id);
+
+ // Check that the source field was created with the correct settings.
+ $storage = FieldStorageConfig::load("media.$field_name");
+ $this->assertInstanceOf(FieldStorageConfig::class, $storage);
+ $field = FieldConfig::load("media.$type_name.$field_name");
+ $this->assertInstanceOf(FieldConfig::class, $field);
+ $this->assertSame('mp4', FieldConfig::load("media.$type_name.$field_name")->getSetting('file_extensions'));
+
+ // Check that the display holds the correct formatter configuration.
+ $display = EntityViewDisplay::load("media.$type_name.default");
+ $this->assertInstanceOf(EntityViewDisplay::class, $display);
+ $formatter = $display->getComponent($field_name)['type'];
+ $this->assertSame('file_video', $formatter);
+
+ // Create a media asset and verify that the <video> tag is present.
+ file_put_contents('public://file.mp4', str_repeat('t', 10));
+ $file = File::create([
+ 'uri' => 'public://file.mp4',
+ 'filename' => 'file.mp4',
+ ]);
+ $file->save();
+
+ $this->drupalGet("media/add/$type_name");
+ $page->fillField('Name', 'Video media asset');
+ $page->attachFileToField("files[{$field_name}_0]", \Drupal::service('file_system')->realpath('public://file.mp4'));
+ $result = $assert_session->waitForButton('Remove');
+ $this->assertNotEmpty($result);
+ $page->pressButton('Save');
+
+ $assert_session->pageTextContains("$type_name Video media asset has been created.");
+ $assert_session->elementExists('css', "video > source[type='video/mp4']");
+ }
+
+}