Problem/Motivation

ImageStyle::flush allows to delete a particular image in an image style, However the hook invoked from the flush() method doesn't pass this path value.

Proposed resolution

Pass $path along to the hook and make sure the hook is invoked when a path is passed.

Remaining tasks

None

User interface changes

None

API changes

$path is passed to hook_image_style_flush
hook_image_style_flush is invoked when a $path is passed

Release note

hook_image_style_flush is now invoked if ImageStyle::flush is called with the $path parameter set. Previously the hook was only called if no path was passed to this method. Also, the path parameter is now passed along to the hook implementation.

Comments

vijaycs85 created an issue. See original summary.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

firewaller’s picture

Status: Active » Needs review
StatusFileSize
new1.17 KB

This happens partially because the hooks are not invoked when $path is set in Drupal\image\Entity\ImageStyle::flush():

/**
 * {@inheritdoc}
 */
public function flush($path = NULL) {
  // A specific image path has been provided. Flush only that derivative.
  if (isset($path)) {
    $derivative_uri = $this->buildUri($path);
    if (file_exists($derivative_uri)) {
      file_unmanaged_delete($derivative_uri);
    }
    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())) {
      file_unmanaged_delete_recursive($directory);
    }
  }

  // Let other modules update as necessary on flush.
  $module_handler = \Drupal::moduleHandler();
  $module_handler->invokeAll('image_style_flush', [$this]);

  // Clear caches so that formatters may be added for this style.
  drupal_theme_rebuild();

  Cache::invalidateTags($this->getCacheTagsToInvalidate());

  return $this;
}

The attached patch triggers things appropriately. The next step is to add $path as an additional argument for the hook.

firewaller’s picture

StatusFileSize
new2.15 KB

Attached is an updated patch that passes $path as an additional argument for the hook.

borisson_’s picture

+++ b/core/modules/image/src/Entity/ImageStyle.php
@@ -256,20 +256,20 @@ public function flush($path = NULL) {
-    $module_handler->invokeAll('image_style_flush', [$this]);
+    $module_handler->invokeAll('image_style_flush', [$this, $path]);

This looks like it's an API break? There isn't anything on the BC policy page about alter hook arguments, so I'm not sure.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

psebborn’s picture

Re-rolled the patch from #6 as it didn't apply cleanly for me (8.7.3)

mr.baileys’s picture

Issue summary: View changes
berdir’s picture

Version: 8.6.x-dev » 8.9.x-dev
Status: Needs review » Needs work
+++ 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
+ *   A string containing a file path or (streamwrapper) URI.

there must be no empty line betwen the params and the param needs to document the type.

I think BC is OK, the only theoretical scenario is that someone would call the hook from a different place and not pass the second argument. We can fix that by documenting it as optional and defaulting to NULL.

mr.baileys’s picture

Issue tags: +Novice
ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new2.32 KB

I have re-rolled the patch against Drupal 8.9.x-dev and made changes that mentioned in #11.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

idebr’s picture

StatusFileSize
new1.28 KB
new2.38 KB

Attached patch implements the following changes:

  1. Update the @param description for $path so it matches the description of \Drupal\image\ImageStyleInterface::flush($path = NULL)
  2. Fixed coding standards
idebr’s picture

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

weseze’s picture

Just confirming that the patch from #17 still works perfectly fine against D9.4.7

WagnerMelo’s picture

Hi, i'll review it.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vistree’s picture

Just to confirm: the patch from #17 still applies to Drupal core 9.4.9. I also needed this patch to get https://www.drupal.org/project/imageapi_optimize_webp/issues/3213379 working (WebP image styles not being updated using focal_point module)

martijn de wit’s picture

Does this solution needs a test, and so a status Needs work? Or can we RTBC this?

I see many +1 in the #3213379: WebP images are not regenerated when using image_widget_crop ticket.

elber’s picture

Hi can you add steps to reproduce this issue ? Please.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs change record

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Think this could use a test case to show the issue.

Also since a new parameter is being added to the hook it will need a change record

thanks

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lendude’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new2.55 KB
new4.23 KB

Added a test. Adding a test-only for $path being passed is non-sensical since we wouldn't expect that to pass currently, but we can test the change where if you pass a $path to flush(), currently the hook is never called at all.
So added a test for that.

Test only is the interdiff.

The last submitted patch, 29: 2833129-29-TEST_ONLY.patch, failed testing. View results

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Test coverage looks solid.

idebr’s picture

lendude’s picture

Issue tags: -Novice, -Needs change record

Added an attempt at a CR, https://www.drupal.org/node/3373248

lauriii’s picture

Would be great to get confirmation from the release managers that the behavior change here is acceptable for a minor release.

catch’s picture

I think this is sensible for a minor release - more likely it fixes a bug for someone than breaks it, and easy mitigation if there's a problem.

lendude’s picture

Issue summary: View changes
Issue tags: -Needs release note

Updated the IS, added a release note

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 29: 2833129-29.patch, failed testing. View results

mr.baileys’s picture

Status: Needs work » Reviewed & tested by the community

Test failure was unrelated to this change, and tests are back to green, so reverting to RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +10.2.0 release notes

These seems a surprising change for me. Currently the hook is only invoked when a whole style is being flushed and not a single path. Therefore all existing implementations are going to assume that the everything is being flushed and not just a single path and therefore act accordingly. That said I can only find 2 instances of this hook in contrib and one already requires this patch so I think I agree with @catch here - okay for a minor release.

Committed 996fb53 and pushed to 11.x. Thanks!

Did a couple of improvements on commit...

Ensured that $path is the expected value in the test

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 7ff821971f1..3955e44b69e 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
@@ -44,5 +44,5 @@ function image_module_test_image_style_presave(ImageStyleInterface $style) {
  */
 function image_module_test_image_style_flush($style, $path = NULL) {
   $state = \Drupal::state();
-  $state->set('image_module_test_image_style_flush.called', TRUE);
+  $state->set('image_module_test_image_style_flush.called', $path);
 }
diff --git a/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php b/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php
index 337f3827cc6..13334291953 100644
--- a/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php
+++ b/core/modules/image/tests/src/Functional/ImageStyleFlushTest.php
@@ -129,10 +129,9 @@ public function testFlush() {
     $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);
+    $this->assertNull($state->get('image_module_test_image_style_flush.called'));
     $style->flush('/made/up/path');
-    $this->assertTrue($state->get('image_module_test_image_style_flush.called'));
+    $this->assertSame('/made/up/path', $state->get('image_module_test_image_style_flush.called'));
   }
 
 }

Added correct typehinit on commit to hook docs.

diff --git a/core/modules/image/image.api.php b/core/modules/image/image.api.php
index 83fde6a69c0..2f786fc318c 100644
--- a/core/modules/image/image.api.php
+++ b/core/modules/image/image.api.php
@@ -32,7 +32,7 @@ function hook_image_effect_info_alter(&$effects) {
  *
  * @param \Drupal\image\ImageStyleInterface $style
  *   The image style object that is being flushed.
- * @param $path
+ * @param string|null $path
  *   (optional) The original image path or URI. If it's supplied, only this
  *   image derivative will be flushed.
  */

Ran the tests locally prior to commit.

  • alexpott committed 996fb537 on 11.x
    Issue #2833129 by Lendude, firewaller, idebr, psebborn, ravi.shankar,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

fago’s picture