public function MediaTypeGuesser::guess in Gutenberg 8
Same name and namespace in other branches
- 8.2 src/MediaTypeGuesser.php \Drupal\gutenberg\MediaTypeGuesser::guess()
Guesses all media types based on general mime type.
Parameters
\Drupal\gutenberg\string $general_mime_type: General mime type (image, video, audio, ...).
int $return_option: If self::RETURN_ALL - return all guessed media types. If self::RETURN_NEGOTIATED - returns only one negotiated media type.
Return value
string|array The guessed media types.
Overrides MediaTypeGuesserInterface::guess
File
- src/
MediaTypeGuesser.php, line 57
Class
- MediaTypeGuesser
- Guesses media type of the file.
Namespace
Drupal\gutenbergCode
public function guess(string $general_media_type, $return_option = self::RETURN_ALL) {
$file_extensions = $this->fileExtensionGuesser
->guessByGeneralMimeType($general_media_type);
$media_type_entities = $this->entityTypeManager
->getStorage('media_type')
->loadMultiple();
$media_types = [];
/** @var \Drupal\media\Entity\MediaType $media_type_entity */
foreach ($media_type_entities as $media_type_entity) {
$field_config = $media_type_entity
->getSource()
->getSourceFieldDefinition($media_type_entity);
$media_type_file_extensions = explode(' ', $field_config
->getSetting('file_extensions'));
if (array_intersect($media_type_file_extensions, $file_extensions)) {
$media_types[] = $media_type_entity
->id();
}
}
if ($return_option === self::RETURN_ALL) {
return $media_types;
}
if ($return_option === self::RETURN_NEGOTIATED) {
return $this
->negotiateMediaTypes($media_types);
}
throw new \InvalidArgumentException('Invalid guesser return type provided.');
}