PHP 7.4 compatibility

Created on 14 August 2023, 11 months ago
Updated 20 August 2023, 10 months ago

Problem/Motivation

An error is being thrown when using the module on a php7.4 drupal instance

Steps to reproduce

install the module (should throw the error on install - not sure if linked to having media entries with pdf files)
edit a media entry that has a pdf and save it will cause the error
cache clear will also cause the error (probably if media entries with pdf files exist in system)

Proposed resolution

The cause of the error is a php 8 syntax element in MediaPdfThumbnailSettingsForm.php in the function getConfigUri
The current function is:

  public static function getConfigUri($value): string {
    return match ($value) {
      'public' => self::DESTINATION_URI_PUBLIC,
      'private' => self::DESTINATION_URI_PRIVATE,
    };
  }

Replacing it with this:

  public static function getConfigUri($value): string {
    return $value==='public'? self::DESTINATION_URI_PUBLIC:self::DESTINATION_URI_PRIVATE;
  }

will solve this issue in a way that does not provide the exact same functionality but that seems acceptable given the context.

I could not find in the documentation what value match would return if no matching entry is found in a match statement that does not have a default entry. I am assuming it is NULL (testing this is easy but can't do the required restarts and config changes now).
this second version only returns a non NULL value if the passed parameter is 'public' or 'private' any other value would return NULL

  public static function getConfigUri($value): string {
    return ($value==='public'? self::DESTINATION_URI_PUBLIC:  ($value==='private'?self::DESTINATION_URI_PRIVATE:NULL));
  }

Remaining tasks

Up to module maintainer. Either declare php 8 as a requirement for the module or make the code php 7 compatible. In this case there are no benefits to using this php 8 construct.

Apologies for using the same issue i know there should be two but i preferred using this one to avoid overwhelming the module maintainers with issues that are basically the same. In MediaPdfThumbnailImagickManager.php when using PHP 7.4 withe the latest spatie library (2.2.0) on windows. In the function generate which is declared to return :mixed an error happens because $pdf-saveImage($target); returns a boolean which is then returned and causes an exception to be raised. The fix to that is to remove :mixed from the the generate function declaration.

The error says it is a notice as all exception are properly caught but it prevents the module from working

[php7:notice] [pid 3124:tid 4368] [client 192.168.1.xxx:24429] TypeError: Return value of Drupal\\media_pdf_thumbnail\\Manager\\MediaPdfThumbnailImagickManager::generate() must be an instance of Drupal\\media_pdf_thumbnail\\Manager\\mixed, bool returned

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

6.0

Component

Code

Created by

πŸ‡±πŸ‡§Lebanon vitch

Live updates comments and jobs are added and updated live.
  • PHP 7.4

    The issue particularly affects sites running on PHP version 7.4.0 or later.

Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024