diff --git a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php index e6553b9..a375001 100644 --- a/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php +++ b/core/lib/Drupal/Core/File/MimeType/MimeTypeMapper.php @@ -863,6 +863,15 @@ class MimeTypeMapper implements MimeTypeMapperInterface { ); /** + * Track if mapping has been altered already. + * + * @see hook_file_mimetype_mapping_alter() + * + * @var bool + */ + protected $isMappingAltered = FALSE; + + /** * The module handler. * * @var \Drupal\Core\Extension\ModuleHandlerInterface @@ -872,30 +881,39 @@ class MimeTypeMapper implements MimeTypeMapperInterface { /** * Constructs a new MimeTypeMapper. * - * Invokes hook_file_mimetype_mapping_alter() to allow modules to alter - * the mapping. - * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. - * - * @see hook_file_mimetype_mapping_alter() */ public function __construct(ModuleHandlerInterface $module_handler) { $this->moduleHandler = $module_handler; - $this->moduleHandler->alter('file_mimetype_mapping', $this); } /** * Returns the mapping from file extensions to appropriate MIME types. * + * Invokes hook_file_mimetype_mapping_alter() to allow modules to alter + * the mapping. + * * @return array * An array consisting of two arrays: * - mimetypes: MIME types, keyed by a unique number. * - extensions: an associative array with the MIME type key numbers as * values. The keys are file extensions, in lower case and without any * preceding dot. + * + * @see hook_file_mimetype_mapping_alter() */ protected function getMapping() { + static $altering = FALSE; + + // Allow modules to alter the default mapping. + if (!$this->isMappingAltered && !$altering) { + $altering = TRUE; + $this->moduleHandler->alter('file_mimetype_mapping', $this); + $this->isMappingAltered = TRUE; + $altering = FALSE; + } + return $this->mapping; } @@ -903,6 +921,10 @@ protected function getMapping() { * {@inheritdoc} */ public function addMapping($mimetype, $extension) { + // Calls ::getMapping() to make sure new mappings are added to the + // ::$mapping array that has been altered by modules already. + $this->getMapping(); + if (!in_array($mimetype, $this->mapping['mimetypes'])) { $this->mapping['mimetypes'][] = $mimetype; }