function ImageStyle::flush

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

File

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

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.