Skip to content

Commit

Permalink
refactor: replace class override with new hook to flush webp images
Browse files Browse the repository at this point in the history
Refs: RW-816
  • Loading branch information
orakili committed Jan 11, 2024
1 parent aac08a4 commit 3059fc2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 52 deletions.
31 changes: 18 additions & 13 deletions html/modules/custom/reliefweb_utility/reliefweb_utility.module
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,26 @@ function reliefweb_utility_file_presave(FileInterface $file) {
}

/**
* Implements hook_entity_type_alter().
* Implements hook_image_style_flush().
*
* Replace the image style class so we can properly delete webp versions of the
* derivative images because this is not done by image_optimize_webp, notably
* because there is no hook provided for that by the image module.
*
* Normally this runs after the imageapi_optimize_entity_type_alter() due to
* the alphabetical order.
* Delete the webp version of the derivative images.
*/
function reliefweb_utility_entity_type_alter(array &$entity_types) {
/** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
if (isset($entity_types['image_style'])) {
$image_style = $entity_types['image_style'];
if ($image_style->getClass() === 'Drupal\imageapi_optimize\Entity\ImageStyleWithPipeline') {
$image_style->setClass('Drupal\reliefweb_utility\Entity\ImageStyleWithPipeline');
function reliefweb_utility_image_style_flush($style, ?string $path = NULL) {
// Also delete the webp version of the image if it exists.
// @see Drupal\image\Entity\ImageStyle::flush()
if (isset($path)) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$derivative_uri = $style->buildUri($path);
$derivative_uri_webp = $derivative_uri . '.webp';

if (file_exists($derivative_uri_webp)) {
try {
$file_system->delete($derivative_uri_webp);
}
catch (FileException $exception) {
// Ignore failed deletion.
}
}
}
}

This file was deleted.

0 comments on commit 3059fc2

Please sign in to comment.