Skip to content
Snippets Groups Projects
Commit dba007dc authored by Martin Anderson-Clutz's avatar Martin Anderson-Clutz
Browse files

Issue #3153137 by nginex, tdroden, GaëlG, MintLucky, StryKaizer, stBorchert,...

Issue #3153137 by nginex, tdroden, GaëlG, MintLucky, StryKaizer, stBorchert, kevinn, zipymonkey: Using Image Widget Crop in responsive images does not refresh webp image
parent 52f7d07d
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,12 @@ ...@@ -5,7 +5,12 @@
* Contains webp.module. * Contains webp.module.
*/ */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Plugin\Field\FieldType\FileFieldItemList;
use Drupal\image\Entity\ImageStyle;
/** /**
* Implements hook_help(). * Implements hook_help().
...@@ -23,6 +28,20 @@ function webp_help($route_name, RouteMatchInterface $route_match) { ...@@ -23,6 +28,20 @@ function webp_help($route_name, RouteMatchInterface $route_match) {
} }
} }
/**
* Implements hook_entity_insert().
*/
function webp_entity_insert(EntityInterface $entity) {
webp_flush_webp_derivatives($entity);
}
/**
* Implements hook_entity_update().
*/
function webp_entity_update(EntityInterface $entity) {
webp_flush_webp_derivatives($entity);
}
/** /**
* Implements template_preprocess_responsive_image(). * Implements template_preprocess_responsive_image().
*/ */
...@@ -64,3 +83,45 @@ function webp_preprocess_responsive_image(&$variables) { ...@@ -64,3 +83,45 @@ function webp_preprocess_responsive_image(&$variables) {
$variables['output_image_tag'] = FALSE; $variables['output_image_tag'] = FALSE;
} }
} }
/**
* Remove any outdated WebP derivatives.
*/
function webp_flush_webp_derivatives(EntityInterface $entity) {
if (isset($entity) && $entity instanceof FieldableEntityInterface) {
$styles = NULL;
$file_system = NULL;
// Loop all fields of the saved entity.
foreach ($entity->getFields() as $entity_fields) {
// If current field is FileField and use imageWidgetCrop.
if ($entity_fields instanceof FileFieldItemList) {
try {
$file_uri = $entity_fields->entity->getFileUri();
}
catch (\Throwable $th) {
// Continue if for some reason the file uri failed to get.
continue;
}
// Loop through each image style and check for webp derivatives.
$styles = ($styles) ?: ImageStyle::loadMultiple(); // Only load once.
$file_system = ($file_system) ?: \Drupal::service('file_system'); // Only load once.
foreach ($styles as $style) {
$derivative_uri = $style->buildUri($file_uri);
$derivative_webp_uri = preg_replace('/\.(png|jpg|jpeg)$/i', '.webp', $derivative_uri);
if (file_exists($derivative_webp_uri)) {
try {
// Remove the webp image style variation.
$file_system->delete($derivative_webp_uri);
}
catch (FileException $e) {
// Ignore failed deletes.
}
}
}
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment