diff -u b/core/modules/media/src/MediaTypeResolverBase.php b/core/modules/media/src/MediaTypeResolverBase.php --- b/core/modules/media/src/MediaTypeResolverBase.php +++ b/core/modules/media/src/MediaTypeResolverBase.php @@ -1,10 +1,13 @@ entityTypeManager = $entity_type_manager; + $this->storage = $entity_type_manager->getStorage('media_type'); + $this->accessHandler = $entity_type_manager->getAccessControlHandler('media'); } /** * {@inheritdoc} */ public function getAll(array $allowed_types = NULL) { - $media_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple($allowed_types); + $media_types = $this->storage->loadMultiple($allowed_types); // Filter out any media types which the current user does not have // permission to create. - $access_handler = $this->entityTypeManager->getAccessControlHandler('media'); + $allowed_types = array_keys($media_types); + $allowed_types = array_filter($allowed_types, [$this->accessHandler, 'createAccess']); + + return array_intersect_key($media_types, array_flip($allowed_types)); + } - return array_filter($media_types, [$access_handler, 'createAccess']); + /** + * {@inheritdoc} + */ + public function alterFormElement(array &$element, array &$complete_form, FormStateInterface $form_state) { + // Don't do anything by default. } } diff -u b/core/modules/media/src/MediaTypeResolverInterface.php b/core/modules/media/src/MediaTypeResolverInterface.php --- b/core/modules/media/src/MediaTypeResolverInterface.php +++ b/core/modules/media/src/MediaTypeResolverInterface.php @@ -4,6 +4,21 @@ use Drupal\Core\Form\FormStateInterface; +/** + * Defines an interface for a media type resolver. + * + * Media type resolvers take arbitrary an input value and determine which media + * types, if any, can use that value in their source field. They can scan all + * available media types which match their internal critieria (for example, all + * media types using a specific source plugin), or a subset thereof, when + * making this determination. + * + * Media type resolvers can also alter form elements which will receive a value + * which may be used as the source value for a media item of indeterminate type. + * A good example of this is to attach generic validation criteria to a file + * upload element whose value (i.e., the uploaded file) will be used to create a + * new media item. + */ interface MediaTypeResolverInterface { /** diff -u b/core/modules/media_library/src/UploadMediaTypeResolver.php b/core/modules/media_library/src/UploadMediaTypeResolver.php --- b/core/modules/media_library/src/UploadMediaTypeResolver.php +++ b/core/modules/media_library/src/UploadMediaTypeResolver.php @@ -8,22 +8,16 @@ use Drupal\file\Plugin\Field\FieldType\FileFieldItemList; use Drupal\file\Plugin\Field\FieldType\FileItem; use Drupal\media\MediaTypeInterface; +use Drupal\media\MediaTypeResolverBase; +/** + * Media type resolver for uploaded files. + */ class UploadMediaTypeResolver extends MediaTypeResolverBase { /** * {@inheritdoc} */ - public function getAll(array $allowed_types = NULL) { - $media_types = parent::getAll($allowed_types); - - // Filter out media types which do not use a file field as a source. - return array_filter($media_types, [$this, 'isFileSource']); - } - - /** - * {@inheritdoc} - */ public function getAllByValue($value, array $allowed_types = NULL) { if ($value instanceof FileInterface) { $filter = function (MediaTypeInterface $media_type) use ($value) { @@ -38,6 +32,16 @@ /** * {@inheritdoc} */ + public function getAll(array $allowed_types = NULL) { + $media_types = parent::getAll($allowed_types); + + // Filter out media types which do not use a file field as a source. + return array_filter($media_types, [$this, 'isFileSource']); + } + + /** + * {@inheritdoc} + */ public function alterFormElement(array &$element, array &$complete_form, FormStateInterface $form_state) { if ($element['#type'] === 'managed_file') { $allowed_types = isset($element['#allowed_types']) ? $element['#allowed_types'] : NULL; @@ -46,6 +50,15 @@ } } + /** + * Derives a set of upload validators from a set of media types. + * + * @param \Drupal\media\MediaTypeInterface[] $media_types + * The media types, each of which uses a file or image field as its source. + * + * @return array + * The upload validators, suitable for passing to file_validate(). + */ protected function mergeUploadValidators(array $media_types) { $max_size = 0; $extensions = []; @@ -72,14 +85,36 @@ ]; } + /** + * Creates an empty file field item for a media type. + * + * @param \Drupal\media\MediaTypeInterface $media_type + * A media type which uses a file or image field as its source. + * + * @return \Drupal\file\Plugin\Field\FieldType\FileItem + * The empty field item. + */ protected function getItem(MediaTypeInterface $media_type) { $field_definition = $media_type->getSource()->getSourceFieldDefinition($media_type); $data_definition = FieldItemDataDefinition::create($field_definition); return new FileItem($data_definition); } + /** + * Indicates if a media type uses a file or image field as its source. + * + * @param \Drupal\media\MediaTypeInterface $media_type + * The media type. + * + * @return bool + * TRUE if the media type's source field is a file or image field, otherwise + * FALSE. + */ protected function isFileSource(MediaTypeInterface $media_type) { - $class = $media_type->getSource()->getSourceFieldDefinition($media_type); + $class = $media_type->getSource() + ->getSourceFieldDefinition($media_type) + ->getClass(); + return is_a($class, FileFieldItemList::class, TRUE); }