function ImageStyle::flush

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

Overrides ImageStyleInterface::flush

1 call to ImageStyle::flush()
ImageStyle::postSave in core/modules/image/src/Entity/ImageStyle.php

File

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

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) {
                // 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) {
                    // Ignore failed deletes.
                }
            }
        }
    }
    // Let other modules update as necessary on flush.
    $module_handler = \Drupal::moduleHandler();
    $module_handler->invokeAll('image_style_flush', [
        $this,
        $path,
    ]);
    // Clear caches when the complete image style is flushed,
    // so that field formatters may be added for this style.
    if (!isset($path)) {
        \Drupal::service('theme.registry')->reset();
        Cache::invalidateTags($this->getCacheTagsToInvalidate());
    }
    return $this;
}

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