diff --git a/core/modules/media/src/FileGuesser.php b/core/modules/media/src/FileGuesser.php index e90d4a852c..9a23fe5b35 100644 --- a/core/modules/media/src/FileGuesser.php +++ b/core/modules/media/src/FileGuesser.php @@ -8,11 +8,12 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\file\FileInterface; use Drupal\file\Plugin\Field\FieldType\FileItem; +use Drupal\media\FileGuesserInterface; /** * Discovers media types which support file uploads. */ -class FileGuesser { +class FileGuesser implements FileGuesserInterface { use UseCacheBackendTrait; @@ -45,16 +46,7 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Cac } /** - * Returns the first media type ID that matches a file. - * - * @param \Drupal\file\FileInterface|int $file - * A file entity or ID. - * @param string[] $limit - * (optional) The media type IDs to choose from. If omitted, all media types - * will be considered. - * - * @return string|bool - * A media type ID, or FALSE if no media types matched. + * {@inheritdoc} */ public function guessForFile($file, array $limit = []) { $media_types = $this->getTypesForFile($file, $limit); @@ -62,16 +54,7 @@ public function guessForFile($file, array $limit = []) { } /** - * Returns the first media type ID that matches a file extension. - * - * @param string $extension - * A file extension, without the leading period. - * @param string[] $limit - * (optional) The media type IDs to choose from. If omitted, all media types - * will be considered. - * - * @return string|bool - * A media type ID, or FALSE if no media types matched. + * {@inheritdoc} */ public function guessForExtension($extension, array $limit = []) { $media_types = $this->getTypesByExtension($extension, $limit); @@ -79,19 +62,7 @@ public function guessForExtension($extension, array $limit = []) { } /** - * Returns all media types that match a file. - * - * @param \Drupal\file\FileInterface|int $file - * A file entity or ID. - * @param string[] $limit - * (optional) The media type IDs to choose from. If omitted, all media types - * will be considered. - * - * @throws \InvalidArgumentException - * If $file is not a file entity, or the ID of one. - * - * @return \Drupal\media\MediaTypeInterface[] - * All matching media types. + * {@inheritdoc} */ public function getTypesForFile($file, array $limit = []) { if (is_numeric($file)) { @@ -109,16 +80,7 @@ public function getTypesForFile($file, array $limit = []) { } /** - * Returns all media types that match a file extension. - * - * @param string $extension - * A file extension, without the leading period. - * @param string[] $limit - * (optional) The media type IDs to choose from. If omitted, all media types - * will be considered. - * - * @return \Drupal\media\MediaTypeInterface[] - * All matching media types. + * {@inheritdoc} */ public function getTypesByExtension($extension, array $limit = []) { $cache = $this->cacheGet($extension); diff --git a/core/modules/media/src/FileGuesserInterface.php b/core/modules/media/src/FileGuesserInterface.php new file mode 100644 index 0000000000..2cbddee69b --- /dev/null +++ b/core/modules/media/src/FileGuesserInterface.php @@ -0,0 +1,69 @@ +fileGuesser = $file_guesser; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.manager'), + $container->get('media.type_guesser.file'), + $container->get('entity_type.bundle.info'), + $container->get('datetime.time') + ); + } + /** * AJAX callback associated with the 'media type' input selector. * @@ -173,8 +215,7 @@ public function onSave(array &$form, FormStateInterface $form_state) { ]; $next_file_id = array_shift($query['files']); $parameters['file'] = $next_file_id; - // TODO: Inject the media.type_guesser.file service. - $parameters['media_type'] = \Drupal::service('media.type_guesser.file')->guessForFile($next_file_id); + $parameters['media_type'] = $this->fileGuesser->guessForFile($next_file_id); $url = Url::fromRoute($route_match->getRouteName(), $parameters, [ 'query' => $query,