Flush image styles for the media thumbnail on file replace

Created on 8 September 2021, almost 3 years ago
Updated 9 July 2023, 12 months ago

Problem/Motivation

For non-image media (e.g. PDF document), the media thumbnail might be generated from the media source file (e.g. Media PDF Thumbnail module - https://www.drupal.org/project/media_pdf_thumbnail β†’ ). If the file is replaced, the image styles for the media thumbnail should be flushed, so they can be updated following generation of the new media thumbnail.

Steps to reproduce

1. Enable this module and media_pdf_thumbnail module.
2. Add a media type with a file field allowing PDF files.
3. Add a new item and upload a PDF and save. Observe the generated thumbnail.
4. Use the replace widget to upload a new PDF.
5. The media thumbnail stays the same.

Proposed resolution

Attached patch flushes the image styles for the media thumbnail on file replace.

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada bgilhome Victoria

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States Chris Matthews

    @bkosborne, would it be possible to commit the patch β†’ in #5 πŸ› Flush image styles for the media thumbnail on file replace Needs work as is, and then create a follow-up issue for the remaining concern re: sites that don't have a dynamic thumbnail for non-image based media?

  • πŸ‡ΊπŸ‡ΈUnited States glynster

    We just ran into this issue as well. As much as the module works well for 'thumbnail' it skips all the image styles connected. It makes sense that if the original file is updated all image styles connected are flushed?

  • πŸ‡ΊπŸ‡ΈUnited States glynster

    Something like this referenced from 'Flush Single Image Style':

     /**
       * {@inheritdoc}
       */
      public function flush(string $path, int $action = self::ACTION_UNLINK): array {
        if (!in_array($action, [self::ACTION_UNLINK, self::ACTION_REGENERATE])) {
          throw new \InvalidArgumentException("The argument \$action must be either " . get_class($this) . "::ACTION_UNLINK or " . get_class($this) . "::ACTION_REGENERATE");
        }
    
        $style_paths = $this->getStylePaths($path);
        $flushed_style_paths = [];
        foreach ($style_paths as $style_id => $style_path) {
          if ($action == self::ACTION_UNLINK) {
            if ($this->fileSystem->unlink($style_path)) {
              $flushed_style_paths[$style_id] = $style_path;
            }
          }
          elseif ($action == self::ACTION_REGENERATE) {
            $imageStyle = ImageStyle::load($style_id);
            if ($imageStyle->createDerivative($path, $style_path)) {
              $flushed_style_paths[$style_id] = $style_path;
            }
          }
        }
    
        return $flushed_style_paths;
      }
Production build 0.69.0 2024