diff --git a/core/modules/image/image.api.php b/core/modules/image/image.api.php index cb56a76449..83fde6a69c 100644 --- a/core/modules/image/image.api.php +++ b/core/modules/image/image.api.php @@ -32,8 +32,11 @@ function hook_image_effect_info_alter(&$effects) { * * @param \Drupal\image\ImageStyleInterface $style * The image style object that is being flushed. + * @param $path + * (optional) The original image path or URI. If it's supplied, only this + * image derivative will be flushed. */ -function hook_image_style_flush($style) { +function hook_image_style_flush($style, $path = NULL) { // Empty cached data that contains information about the style. \Drupal::cache('mymodule')->deleteAll(); } diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 964823934e..873568d1e1 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -278,25 +278,25 @@ public function flush($path = NULL) { // Ignore failed deletes. } } - return $this; } - - // Delete the style directory in each registered wrapper. - $wrappers = $this->getStreamWrapperManager()->getWrappers(StreamWrapperInterface::WRITE_VISIBLE); - foreach ($wrappers as $wrapper => $wrapper_data) { - if (file_exists($directory = $wrapper . '://styles/' . $this->id())) { - try { - $file_system->deleteRecursive($directory); - } - catch (FileException $e) { - // Ignore failed deletes. + else { + // Delete the style directory in each registered wrapper. + $wrappers = $this->getStreamWrapperManager()->getWrappers(StreamWrapperInterface::WRITE_VISIBLE); + foreach ($wrappers as $wrapper => $wrapper_data) { + if (file_exists($directory = $wrapper . '://styles/' . $this->id())) { + try { + $file_system->deleteRecursive($directory); + } + catch (FileException $e) { + // Ignore failed deletes. + } } } } // Let other modules update as necessary on flush. $module_handler = \Drupal::moduleHandler(); - $module_handler->invokeAll('image_style_flush', [$this]); + $module_handler->invokeAll('image_style_flush', [$this, $path]); // Clear caches so that formatters may be added for this style. \Drupal::service('theme.registry')->reset(); diff --git a/core/modules/image/tests/modules/image_module_test/image_module_test.module b/core/modules/image/tests/modules/image_module_test/image_module_test.module index 3fd845cc58..7ff821971f 100644 --- a/core/modules/image/tests/modules/image_module_test/image_module_test.module +++ b/core/modules/image/tests/modules/image_module_test/image_module_test.module @@ -38,3 +38,11 @@ function image_module_test_image_effect_info_alter(&$effects) { function image_module_test_image_style_presave(ImageStyleInterface $style) { $style->setThirdPartySetting('image_module_test', 'foo', 'bar'); } + +/** + * Implements hook_image_style_flush(). + */ +function image_module_test_image_style_flush($style, $path = NULL) { + $state = \Drupal::state(); + $state->set('image_module_test_image_style_flush.called', TRUE); +} diff --git a/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php b/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php index 820bedffa2..2035f3269d 100644 --- a/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php +++ b/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php @@ -125,6 +125,14 @@ public function testFlush() { // Post flush, expected no image in the 'private' wrapper. $this->assertEquals(0, $this->getImageCount($style, 'private'), new FormattableMarkup('Image style %style flushed correctly for %wrapper wrapper.', ['%style' => $style->label(), '%wrapper' => 'private'])); + + $state = \Drupal::state(); + $state->set('image_module_test_image_style_flush.called', FALSE); + $style->flush(); + $this->assertTrue($state->get('image_module_test_image_style_flush.called')); + $state->set('image_module_test_image_style_flush.called', FALSE); + $style->flush('/made/up/path'); + $this->assertTrue($state->get('image_module_test_image_style_flush.called')); } }