diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileAudioFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileAudioFormatter.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileAudioFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileAudioFormatter.php @@ -25,2 +25,9 @@ + /** + * {@inheritdoc} + */ + protected function getHtmlTag() { + return static::getMediaType(); + } + } diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php @@ -15,6 +15,14 @@ abstract class FileMediaFormatterBase extends FileFormatterBase implements FileMediaFormatterInterface { /** + * Gets the HTML tag for a formatter. + * + * @return string + * The HTML tag of this formatter. + */ + abstract protected function getHtmlTag(); + + /** * {@inheritdoc} */ public static function defaultSettings() { @@ -50,8 +58,8 @@ '#title' => $this->t('Display of multiple files'), '#type' => 'radios', '#options' => [ - 'tags' => $this->t('Use multiple @tag tags, each with a single source.', ['@tag' => '<' . static::getMediaType() . '>']), - 'sources' => $this->t('Use multiple sources within a single @tag tag.', ['@tag' => '<' . static::getMediaType() . '>']), + 'tags' => $this->t('Use multiple @tag tags, each with a single source.', ['@tag' => '<' . $this->getHtmlTag() . '>']), + 'sources' => $this->t('Use multiple sources within a single @tag tag.', ['@tag' => '<' . $this->getHtmlTag() . '>']), ], '#default_value' => $this->getSetting('multiple_file_display_type'), ], @@ -113,7 +121,7 @@ $attributes = $this->prepareAttributes(); foreach ($source_files as $delta => $files) { $elements[$delta] = [ - '#theme' => 'file_' . static::getMediaType(), + '#theme' => 'file_' . $this->getHtmlTag(), '#attributes' => $attributes, '#files' => $files, '#cache' => ['tags' => []], diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php @@ -28,6 +28,13 @@ /** * {@inheritdoc} */ + protected function getHtmlTag() { + return static::getMediaType(); + } + + /** + * {@inheritdoc} + */ public static function defaultSettings() { return [ 'muted' => FALSE, diff -u b/core/modules/file/tests/src/Functional/Formatter/FileAudioFormatterTest.php b/core/modules/file/tests/src/Functional/Formatter/FileAudioFormatterTest.php --- b/core/modules/file/tests/src/Functional/Formatter/FileAudioFormatterTest.php +++ b/core/modules/file/tests/src/Functional/Formatter/FileAudioFormatterTest.php @@ -13,21 +13,33 @@ /** * @covers ::viewElements + * + * @dataProvider dataProvider */ - public function testRender() { - $field_config = $this->createMediaField('file_audio', 'mp3'); + public function testRender($tag_count, $formatter_settings) { + + $field_config = $this->createMediaField('file_audio', 'mp3', $formatter_settings); file_put_contents('public://file.mp3', str_repeat('t', 10)); - $file = File::create([ + $file1 = File::create([ + 'uri' => 'public://file.mp3', + 'filename' => 'file.mp3', + ]); + $file1->save(); + + $file2 = File::create([ 'uri' => 'public://file.mp3', 'filename' => 'file.mp3', ]); - $file->save(); + $file2->save(); $entity = EntityTest::create([ $field_config->getName() => [ [ - 'target_id' => $file->id(), + 'target_id' => $file1->id(), + ], + [ + 'target_id' => $file2->id(), ], ], ]); @@ -35,9 +47,25 @@ $this->drupalGet($entity->toUrl()); - $file_url = file_url_transform_relative(file_create_url($file->getFileUri())); - $this->assertSession()->elementExists('css', 'audio[controls="controls"]'); - $this->assertSession()->elementExists('css', "audio > source[src='$file_url'][type='audio/mpeg']"); + $file1_url = file_url_transform_relative(file_create_url($file1->getFileUri())); + $file2_url = file_url_transform_relative(file_create_url($file2->getFileUri())); + + $this->assertSession()->elementsCount('css', 'audio[controls="controls"]', $tag_count); + $this->assertSession()->elementExists('css', "audio > source[src='$file1_url'][type='audio/mpeg']"); + $this->assertSession()->elementExists('css', "audio > source[src='$file2_url'][type='audio/mpeg']"); + } + + /** + * Data provider for testRender. + * + * @return array + * Provided data. + */ + public function dataProvider() { + return [ + [2, []], + [1, ['multiple_file_display_type' => 'sources']], + ]; } } diff -u b/core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php b/core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php --- b/core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php +++ b/core/modules/file/tests/src/Functional/Formatter/FileMediaFormatterTestBase.php @@ -39,11 +39,13 @@ * The formatter ID. * @param string $file_extensions * The file extensions of the new field. + * @param string[] $formatter_settings + * Settings for the formatter. * * @return \Drupal\field\Entity\FieldConfig * Newly created file field. */ - protected function createMediaField($formatter, $file_extensions) { + protected function createMediaField($formatter, $file_extensions, array $formatter_settings = []) { $entity_type = $bundle = 'entity_test'; $field_name = Unicode::strtolower($this->randomMachineName()); @@ -66,7 +68,7 @@ $display = entity_get_display('entity_test', 'entity_test', 'full'); $display->setComponent($field_name, [ 'type' => $formatter, - 'settings' => [], + 'settings' => $formatter_settings, ])->save(); return $field_config; diff -u b/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php b/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php --- b/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php +++ b/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php @@ -13,21 +13,33 @@ /** * @covers ::viewElements + * + * @dataProvider dataProvider */ - public function testRender() { - $field_config = $this->createMediaField('file_video', 'mp4'); + public function testRender($tag_count, $formatter_settings) { + + $field_config = $this->createMediaField('file_video', 'mp4', $formatter_settings); file_put_contents('public://file.mp4', str_repeat('t', 10)); - $file = File::create([ + $file1 = File::create([ + 'uri' => 'public://file.mp4', + 'filename' => 'file.mp4', + ]); + $file1->save(); + + $file2 = File::create([ 'uri' => 'public://file.mp4', 'filename' => 'file.mp4', ]); - $file->save(); + $file2->save(); $entity = EntityTest::create([ $field_config->getName() => [ [ - 'target_id' => $file->id(), + 'target_id' => $file1->id(), + ], + [ + 'target_id' => $file2->id(), ], ], ]); @@ -35,9 +47,25 @@ $this->drupalGet($entity->toUrl()); - $file_url = file_url_transform_relative(file_create_url($file->getFileUri())); - $this->assertSession()->elementExists('css', 'video[controls="controls"]'); - $this->assertSession()->elementExists('css', "video > source[src='$file_url'][type='video/mp4']"); + $file1_url = file_url_transform_relative(file_create_url($file1->getFileUri())); + $file2_url = file_url_transform_relative(file_create_url($file2->getFileUri())); + + $this->assertSession()->elementsCount('css', 'video[controls="controls"]', $tag_count); + $this->assertSession()->elementExists('css', "video > source[src='$file1_url'][type='video/mp4']"); + $this->assertSession()->elementExists('css', "video > source[src='$file2_url'][type='video/mp4']"); + } + + /** + * Data provider for testRender. + * + * @return array + * Provided data. + */ + public function dataProvider() { + return [ + [2, []], + [1, ['multiple_file_display_type' => 'sources']], + ]; } }