You are here

public function ImageStyle::flush in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/image/src/Entity/ImageStyle.php \Drupal\image\Entity\ImageStyle::flush()
  2. 10 core/modules/image/src/Entity/ImageStyle.php \Drupal\image\Entity\ImageStyle::flush()

Flushes cached media for this style.

Parameters

string $path: (optional) The original image path or URI. If it's supplied, only this image derivative will be flushed.

Return value

$this

Overrides ImageStyleInterface::flush

1 call to ImageStyle::flush()
ImageStyle::postSave in core/modules/image/src/Entity/ImageStyle.php
Acts on a saved entity before the insert or update hook is invoked.

File

core/modules/image/src/Entity/ImageStyle.php, line 262

Class

ImageStyle
Defines an image style configuration entity.

Namespace

Drupal\image\Entity

Code

public function flush($path = NULL) {

  // A specific image path has been provided. Flush only that derivative.

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  if (isset($path)) {
    $derivative_uri = $this
      ->buildUri($path);
    if (file_exists($derivative_uri)) {
      try {
        $file_system
          ->delete($derivative_uri);
      } catch (FileException $e) {

        // 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.
      }
    }
  }

  // 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;
}