Fix null parameter in explode() function in FileMediaFormatterBase

Created on 22 August 2024, 3 months ago
Updated 25 August 2024, 3 months ago

Problem/Motivation

Drupal core's `FileMediaFormatterBase::mimeTypeApplies()` function is using the `explode()` function without properly handling the case where a `null` value might be passed to it. This leads to a deprecated function warning in PHP 8.1 and above, where passing `null` to the second parameter of `explode()` is no longer allowed.

The deprecation warning is triggered when accessing the media display settings for a file, particularly when the MIME type cannot be determined, and `null` is returned.

Steps to reproduce

1. Install a Drupal site on PHP 8.1 or above.
2. Navigate to **/admin/structure/media/manage/file/display**.
3. Observe the deprecation warning related to the `explode()` function being logged or displayed.

Proposed resolution

Add a null check in the `FileMediaFormatterBase::isApplicable()` method before passing the value to the `mimeTypeApplies()` method. This will prevent the `explode()` function from receiving a `null` value and resolve the deprecation warning.

**Code Change:**

```php
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
if (!parent::isApplicable($field_definition)) {
return FALSE;
}
/** @var \Symfony\Component\Mime\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')));

foreach ($extension_list as $extension) {
$mime_type = $extension_mime_type_guesser->guessMimeType('fakedFile.' . $extension);

// Add null check to avoid passing null to mimeTypeApplies().
if ($mime_type !== null && static::mimeTypeApplies($mime_type)) {
return TRUE;
}
}
return FALSE;
}
```

Remaining tasks

- [ ] Write and apply the code patch.
- [ ] Review the patch.
- [ ] Test the patch to ensure it resolves the deprecation warning.
- [ ] Submit the patch or merge request for inclusion in Drupal core.

User interface changes

There are no user interface changes introduced by this fix. The change is solely within the backend PHP code.

Introduced terminology

No new terminology is introduced.

API changes

No API changes are introduced. This change is purely a bug fix and maintains backward compatibility.

Data model changes

There are no data model changes involved in this fix.

Release notes snippet

Fixed a deprecation warning in `FileMediaFormatterBase::isApplicable()` where `null` was being passed to the `explode()` function, which is not allowed in PHP 8.1 and later versions.

πŸ› Bug report
Status

Closed: duplicate

Version

11.0 πŸ”₯

Component
File moduleΒ  β†’

Last updated about 8 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States donrichards Knoxville, TN

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024