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 @@ -9,6 +9,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Template\Attribute; +use Drupal\file\Entity\File; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -173,11 +174,11 @@ /** * Prepare the attributes according to the settings. * - * @param array $additional_attributes - * Additional attributes for preparing the html tag attributes. + * @param string[] $additional_attributes + * Additional attributes to be applied to the HTML element. * * @return \Drupal\Core\Template\Attribute - * Container with all the attributes for the html tag. + * Container with all the attributes for the HTML tag. */ protected function prepareAttributes(array $additional_attributes = []) { $attributes = new Attribute(); @@ -190,17 +191,17 @@ } /** - * Check if given mimetype applies to formatter one. + * Check if given MIME type applies to formatter one. * - * @param string $mimeType + * @param string $mime_type * The full mimeType. * * @return bool - * Mimetype applies or not. + * TRUE if the MIME type applies, FALSE otherwise. */ - protected static function mimeTypeApplies($mimeType) { - list($type) = explode('/', $mimeType, 2); - return ($type == static::getMimeType()); + protected static function mimeTypeApplies($mime_type) { + list($type) = explode('/', $mime_type, 2); + return ($type === static::getMimeType()); } /** @@ -212,7 +213,9 @@ * The language code of the referenced entities to display. * * @return array - * List of files. + * Numeric bases array, which again contains an associative array with the following key/values: + * - file => \Drupal\file\Entity\File + * - source_attributes => \Drupal\Core\Template\Attribute */ protected function getSourceFiles(EntityReferenceFieldItemListInterface $items, $langcode) { $source_files = []; @@ -220,12 +223,14 @@ // Because we can have the files grouped in a single media tag, we do a // grouping in case the multiple file behavior is not 'tags'. + /** @var \Drupal\file\Entity\File $file */ foreach ($this->getEntitiesToView($items, $langcode) as $file) { if (static::mimeTypeApplies($file->getMimeType())) { $source_attributes = new Attribute(); - $source_attributes->setAttribute('src', file_create_url($file->getFileUri())); - $source_attributes->setAttribute('type', $file->getMimeType()); - if ($multiple_file_behavior == 'tags') { + $source_attributes + ->setAttribute('src', file_create_url($file->getFileUri())) + ->setAttribute('type', $file->getMimeType()); + if ($multiple_file_behavior === 'tags') { $source_files[] = [ [ 'file' => $file, diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterInterface.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterInterface.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterInterface.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterInterface.php @@ -3,14 +3,12 @@ namespace Drupal\file\Plugin\Field\FieldFormatter; /** - * Defines getter and setter methods for FileMediaFormatters. - * - * @package Drupal\file\Plugin\Field\FieldFormatter + * Defines getter methods for FileMediaFormatterBase. */ interface FileMediaFormatterInterface { /** - * The applicable mimetype for a formatter. + * The applicable MIME type for a formatter. */ public static function getMimeType(); 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 @@ -31,8 +31,8 @@ public static function defaultSettings() { return [ 'muted' => FALSE, - 'width' => NULL, - 'height' => NULL, + 'width' => 640, + 'height' => 480, ] + parent::defaultSettings(); } @@ -48,20 +48,24 @@ '#default_value' => $this->getSetting('muted'), ]; $element['width'] = [ - '#type' => 'textfield', + '#type' => 'number', '#title' => $this->t('Width'), '#default_value' => $this->getSetting('width'), '#size' => 5, '#maxlength' => 5, '#field_suffix' => $this->t('pixels'), + '#min' => 0, + '#required' => TRUE, ]; $element['height'] = [ - '#type' => 'textfield', + '#type' => 'number', '#title' => $this->t('Height'), '#default_value' => $this->getSetting('height'), '#size' => 5, '#maxlength' => 5, '#field_suffix' => $this->t('pixels'), + '#min' => 0, + '#required' => TRUE, ]; return $element; @@ -73,14 +77,10 @@ public function settingsSummary() { $summary = parent::settingsSummary(); $summary[] = $this->t('Muted: %muted', ['%muted' => $this->getSetting('muted') ? $this->t('yes') : $this->t('no')]); - $width = $this->getSetting('width'); - $height = $this->getSetting('height'); - if ($width && $height) { - $summary[] = $this->t('Size: %width x %height pixels', [ - '%width' => $this->getSetting('width'), - '%height' => $this->getSetting('height'), - ]); - } + $summary[] = $this->t('Size: %width x %height pixels', [ + '%width' => $this->getSetting('width'), + '%height' => $this->getSetting('height'), + ]); return $summary; } @@ -88,15 +88,9 @@ * {@inheritdoc} */ protected function prepareAttributes(array $additional_attributes = []) { - $video_attributes = parent::prepareAttributes(['muted']); - $width = $this->getSetting('width'); - $height = $this->getSetting('height'); - if ($width && $height) { - $video_attributes->setAttribute('width', $width); - $video_attributes->setAttribute('height', $height); - } - - return $video_attributes; + return parent::prepareAttributes(['muted']) + ->setAttribute('width', $this->getSetting('width')) + ->setAttribute('height', $this->getSetting('height')); } } 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 @@ -15,7 +15,7 @@ * @covers ::viewElements */ public function testRender() { - $this->createMediaField('file_audio', 'mp3'); + $field_config = $this->createMediaField('file_audio', 'mp3'); file_put_contents('public://file.mp3', str_repeat('t', 10)); $file = File::create([ @@ -25,7 +25,7 @@ $file->save(); $entity = EntityTest::create([ - $this->fieldName => [ + $field_config->getName() => [ [ 'target_id' => $file->id(), ], @@ -33,7 +33,7 @@ ]); $entity->save(); - $this->drupalGet($entity->url()); + $this->drupalGet($entity->toUrl()); $file_url = file_url_transform_relative(file_create_url($file->getFileUri())); $this->assertSession()->elementExists('css', 'audio[controls="controls"]'); 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 @@ -14,23 +14,9 @@ abstract class FileMediaFormatterTestBase extends BrowserTestBase { /** - * The field name. - * - * @var string - */ - protected $fieldName; - - /** - * The view display. - * - * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface - */ - protected $display; - - /** * {@inheritdoc} */ - public static $modules = [ + protected static $modules = [ 'entity_test', 'field', 'file', @@ -51,35 +37,39 @@ * * @param string $formatter * The formatter ID. - * @param string $file_extionsions + * @param string $file_extensions * The file extensions of the new field. + * + * @return \Drupal\field\Entity\FieldConfig + * Newly created file field. */ - protected function createMediaField($formatter, $file_extionsions) { - - $entityType = $bundle = 'entity_test'; - $this->fieldName = Unicode::strtolower($this->randomMachineName()); + protected function createMediaField($formatter, $file_extensions) { + $entity_type = $bundle = 'entity_test'; + $fieldName = Unicode::strtolower($this->randomMachineName()); FieldStorageConfig::create([ - 'entity_type' => $entityType, - 'field_name' => $this->fieldName, + 'entity_type' => $entity_type, + 'field_name' => $fieldName, 'type' => 'file', 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, ])->save(); - FieldConfig::create([ - 'entity_type' => $entityType, - 'field_name' => $this->fieldName, + $field_config = FieldConfig::create([ + 'entity_type' => $entity_type, + 'field_name' => $fieldName, 'bundle' => $bundle, 'settings' => [ - 'file_extensions' => $file_extionsions, + 'file_extensions' => trim($file_extensions), ], - ])->save(); + ]); + $field_config->save(); - $this->display = entity_get_display('entity_test', 'entity_test', 'full'); - $this->display->setComponent($this->fieldName, [ + $display = entity_get_display('entity_test', 'entity_test', 'full'); + $display->setComponent($fieldName, [ 'type' => $formatter, 'settings' => [], - ]); - $this->display->save(); + ])->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 @@ -15,7 +15,7 @@ * @covers ::viewElements */ public function testRender() { - $this->createMediaField('file_video', 'mp4'); + $field_config = $this->createMediaField('file_video', 'mp4'); file_put_contents('public://file.mp4', str_repeat('t', 10)); $file = File::create([ @@ -25,7 +25,7 @@ $file->save(); $entity = EntityTest::create([ - $this->fieldName => [ + $field_config->getName() => [ [ 'target_id' => $file->id(), ], @@ -33,7 +33,7 @@ ]); $entity->save(); - $this->drupalGet($entity->url()); + $this->drupalGet($entity->toUrl()); $file_url = file_url_transform_relative(file_create_url($file->getFileUri())); $this->assertSession()->elementExists('css', 'video[controls="controls"]');