diff -u b/core/modules/file/config/schema/file.schema.yml b/core/modules/file/config/schema/file.schema.yml --- b/core/modules/file/config/schema/file.schema.yml +++ b/core/modules/file/config/schema/file.schema.yml @@ -76,7 +76,7 @@ mapping: controls: type: boolean - label: 'Show audio controls' + label: 'Show controls' autoplay: type: boolean label: 'Autoplay' 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 @@ -20,5 +20,7 @@ * {@inheritdoc} */ - const APPLICABLE_MIME_TYPE = 'audio'; + public static function getMimeType() { + return 'audio'; + } } diff -u b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatter.php --- b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatter.php @@ -10,17 +10,11 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\Template\Attribute; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; /** * Base class for media file formatter. */ -abstract class FileMediaFormatter extends FileFormatterBase implements ContainerFactoryPluginInterface { - - /** - * The applicable mimetype for a formatter. - */ - const APPLICABLE_MIME_TYPE = FALSE; +abstract class FileMediaFormatter extends FileFormatterBase implements ContainerFactoryPluginInterface, FileMediaFormatterInterface { /** * The renderer service. @@ -75,41 +69,41 @@ */ public static function defaultSettings() { return [ - 'controls' => TRUE, - 'autoplay' => FALSE, - 'loop' => FALSE, - 'multiple_file_behavior' => 'tags', - ] + parent::defaultSettings(); + 'controls' => TRUE, + 'autoplay' => FALSE, + 'loop' => FALSE, + 'multiple_file_behavior' => 'tags', + ] + parent::defaultSettings(); } /** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $element['controls'] = array( + $element['controls'] = [ '#title' => $this->t('Show audio controls'), '#type' => 'checkbox', '#default_value' => $this->getSetting('controls'), - ); - $element['autoplay'] = array( + ]; + $element['autoplay'] = [ '#title' => $this->t('Autoplay'), '#type' => 'checkbox', '#default_value' => $this->getSetting('autoplay'), - ); - $element['loop'] = array( + ]; + $element['loop'] = [ '#title' => $this->t('Loop'), '#type' => 'checkbox', '#default_value' => $this->getSetting('loop'), - ); - $element['multiple_file_behavior'] = array( + ]; + $element['multiple_file_behavior'] = [ '#title' => $this->t('Display of multiple files'), '#type' => 'radios', - '#options' => array( - 'tags' => $this->t('Use multiple @tag tags, each with a single source.', array('@tag' => '<' . static::APPLICABLE_MIME_TYPE . '>')), - 'sources' => $this->t('Use multiple sources within a single @tag tag.', array('@tag' => '<' . static::APPLICABLE_MIME_TYPE . '>')), - ), + '#options' => [ + 'tags' => $this->t('Use multiple @tag tags, each with a single source.', ['@tag' => '<' . static::getMimeType() . '>']), + 'sources' => $this->t('Use multiple sources within a single @tag tag.', ['@tag' => '<' . static::getMimeType() . '>']), + ], '#default_value' => $this->getSetting('multiple_file_behavior'), - ); + ]; return $element; } @@ -118,20 +112,18 @@ * {@inheritdoc} */ public static function isApplicable(FieldDefinitionInterface $field_definition) { - if (!parent::isApplicable($field_definition)) { return FALSE; } - /** @var MimeTypeGuesserInterface $extension_mime_type_guesser */ + /** @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $extension_mime_type_guesser */ $extension_mime_type_guesser = \Drupal::service('file.mime_type.guesser.extension'); + $extension_list = array_filter(preg_split('/\s+/', $field_definition->getSetting('file_extensions'))); - $extensionList = array_filter(preg_split('/\s+/', $field_definition->getSetting('file_extensions'))); - - foreach ($extensionList as $extension) { - $mimeType = $extension_mime_type_guesser->guess('fakedFile.' . $extension); + foreach ($extension_list as $extension) { + $mime_type = $extension_mime_type_guesser->guess('fakedFile.' . $extension); - if (static::getMimeTypeType($mimeType) == static::APPLICABLE_MIME_TYPE) { + if (static::mimeTypeApplies($mime_type)) { return TRUE; } } @@ -144,10 +136,10 @@ */ public function settingsSummary() { $summary = []; - $summary[] = $this->t('Controls: %controls', array('%controls' => $this->getSetting('controls') ? $this->t('visible') : $this->t('hidden'))); - $summary[] = $this->t('Autoplay: %autoplay', array('%autoplay' => $this->getSetting('autoplay') ? $this->t('yes') : $this->t('no'))); - $summary[] = $this->t('Loop: %loop', array('%loop' => $this->getSetting('loop') ? $this->t('yes') : $this->t('no'))); - $summary[] = $this->t('Multiple files: %multiple', array('%multiple' => $this->getSetting('multiple_file_behavior'))); + $summary[] = $this->t('Controls: %controls', ['%controls' => $this->getSetting('controls') ? $this->t('visible') : $this->t('hidden')]); + $summary[] = $this->t('Autoplay: %autoplay', ['%autoplay' => $this->getSetting('autoplay') ? $this->t('yes') : $this->t('no')]); + $summary[] = $this->t('Loop: %loop', ['%loop' => $this->getSetting('loop') ? $this->t('yes') : $this->t('no')]); + $summary[] = $this->t('Multiple files: %multiple', ['%multiple' => $this->getSetting('multiple_file_behavior')]); return $summary; } @@ -155,7 +147,7 @@ * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { - $elements = array(); + $elements = []; $source_files = $this->getSourceFiles($items, $langcode); @@ -165,11 +157,11 @@ $attributes = $this->prepareAttributes(); foreach ($source_files as $delta => $files) { - $elements[$delta] = array( - '#theme' => 'file_' . static::APPLICABLE_MIME_TYPE, + $elements[$delta] = [ + '#theme' => 'file_' . static::getMimeType(), '#attributes' => $attributes, '#files' => $files, - ); + ]; foreach ($files as $file) { $this->renderer->addCacheableDependency($elements[$delta], $file['file']); } @@ -182,13 +174,14 @@ * Prepare the attributes according to the settings. * * @param array $additional_attributes - * Additional attributes for preparing. + * Additional attributes for preparing the html tag attributes. * - * @return Attribute + * @return \Drupal\Core\Template\Attribute + * Container with all the attributes for the html tag. */ protected function prepareAttributes(array $additional_attributes = []) { $attributes = new Attribute(); - foreach (array('controls', 'autoplay', 'loop') + $additional_attributes as $attribute) { + foreach (['controls', 'autoplay', 'loop'] + $additional_attributes as $attribute) { if ($this->getSetting($attribute)) { $attributes->setAttribute($attribute, $attribute); } @@ -197,17 +190,17 @@ } /** - * Returns the first part of the mimetype of the file. + * Check if given mimetype applies to formatter one. * * @param string $mimeType * The full mimeType. * - * @return string - * The last part of a mimetype. + * @return bool + * Mimetype applies or not. */ - protected static function getMimeTypeType($mimeType) { + protected static function mimeTypeApplies($mimeType) { list($type) = explode('/', $mimeType, 2); - return $type; + return ($type == static::getMimeType()); } /** @@ -228,23 +221,23 @@ // 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'. foreach ($this->getEntitiesToView($items, $langcode) as $file) { - if (static::getMimeTypeType($file->getMimeType()) == static::APPLICABLE_MIME_TYPE) { + 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_files[] = array( - array( + $source_files[] = [ + [ 'file' => $file, - 'source_attributes' => $source_attributes - ) - ); + 'source_attributes' => $source_attributes, + ], + ]; } else { - $source_files[0][] = array( + $source_files[0][] = [ 'file' => $file, - 'source_attributes' => $source_attributes - ); + 'source_attributes' => $source_attributes, + ]; } } } 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 @@ -2,9 +2,7 @@ namespace Drupal\file\Plugin\Field\FieldFormatter; -use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Template\Attribute; /** * Plugin implementation of the 'file_video' formatter. @@ -23,7 +21,9 @@ /** * {@inheritdoc} */ - const APPLICABLE_MIME_TYPE = 'video'; + public static function getMimeType() { + return 'video'; + } /** * {@inheritdoc} @@ -42,27 +42,27 @@ public function settingsForm(array $form, FormStateInterface $form_state) { $element = parent::settingsForm($form, $form_state); - $element['muted'] = array( + $element['muted'] = [ '#title' => $this->t('Muted'), '#type' => 'checkbox', '#default_value' => $this->getSetting('muted'), - ); - $element['width'] = array( + ]; + $element['width'] = [ '#type' => 'textfield', '#title' => $this->t('Width'), '#default_value' => $this->getSetting('width'), '#size' => 5, '#maxlength' => 5, '#field_suffix' => $this->t('pixels'), - ); - $element['height'] = array( + ]; + $element['height'] = [ '#type' => 'textfield', '#title' => $this->t('Height'), '#default_value' => $this->getSetting('height'), '#size' => 5, '#maxlength' => 5, '#field_suffix' => $this->t('pixels'), - ); + ]; return $element; } @@ -72,14 +72,14 @@ */ public function settingsSummary() { $summary = parent::settingsSummary(); - $summary[] = $this->t('Muted: %muted', array('%muted' => $this->getSetting('muted') ? $this->t('yes') : $this->t('no'))); + $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', array( + $summary[] = $this->t('Size: %width x %height pixels', [ '%width' => $this->getSetting('width'), '%height' => $this->getSetting('height'), - )); + ]); } return $summary; } @@ -88,7 +88,6 @@ * {@inheritdoc} */ protected function prepareAttributes(array $additional_attributes = []) { - $video_attributes = parent::prepareAttributes(['muted']); $width = $this->getSetting('width'); $height = $this->getSetting('height'); diff -u b/core/modules/file/tests/src/Kernel/Formatter/FileVideoFormatterTest.php b/core/modules/file/tests/src/Kernel/Formatter/FileVideoFormatterTest.php --- b/core/modules/file/tests/src/Kernel/Formatter/FileVideoFormatterTest.php +++ b/core/modules/file/tests/src/Kernel/Formatter/FileVideoFormatterTest.php @@ -54,7 +54,7 @@ $this->installSchema('file', array('file_usage')); $entityType = 'entity_test'; - $bundle = $this->entityType; + $bundle = $entityType; $this->fieldName = Unicode::strtolower($this->randomMachineName()); FieldStorageConfig::create(array( only in patch2: unchanged: --- /dev/null +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterInterface.php @@ -0,0 +1,17 @@ +