function ImageStyle::replaceImageStyle

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

Update field settings if the image style name is changed.

Parameters

\Drupal\image\ImageStyleInterface $style: The image style.

1 call to ImageStyle::replaceImageStyle()
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 148

Class

ImageStyle
Defines an image style configuration entity.

Namespace

Drupal\image\Entity

Code

protected static function replaceImageStyle(ImageStyleInterface $style) {
    if ($style->id() != $style->getOriginalId()) {
        // Loop through all entity displays looking for formatters / widgets using
        // the image style.
        foreach (EntityViewDisplay::loadMultiple() as $display) {
            foreach ($display->getComponents() as $name => $options) {
                if (isset($options['type']) && $options['type'] == 'image' && $options['settings']['image_style'] == $style->getOriginalId()) {
                    $options['settings']['image_style'] = $style->id();
                    $display->setComponent($name, $options)
                        ->save();
                }
            }
        }
        foreach (EntityFormDisplay::loadMultiple() as $display) {
            foreach ($display->getComponents() as $name => $options) {
                if (isset($options['type']) && $options['type'] == 'image_image' && $options['settings']['preview_image_style'] == $style->getOriginalId()) {
                    $options['settings']['preview_image_style'] = $style->id();
                    $display->setComponent($name, $options)
                        ->save();
                }
            }
        }
    }
}

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