diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 96e442f..c9e50ed 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -80,6 +80,20 @@ class ImageStyle extends ConfigEntityBase implements ImageStyleInterface, Entity protected $label; /** + * Indicates whether runtime variables should be reset. + * + * When TRUE, the runtime variables will be reset at next call of + * ::createDerivative, ::transformDimensions, ::getDerivativeExtension. + * + * @see ::createDerivative + * @see ::transformDimensions + * @see ::getDerivativeExtension + * + * @var bool + */ + protected $resetRuntimeEnvironment = TRUE; + + /** * The array of image effects for this image style. * * @var array @@ -292,10 +306,13 @@ public function createDerivative($original_uri, $derivative_uri) { return FALSE; } - $this->getEffects()->resetRuntimeVariables(); + if ($this->resetRuntimeEnvironment) { + $this->getEffects()->resetRuntimeVariables(); + } foreach ($this->getEffects() as $effect) { $effect->applyEffect($image); } + $this->resetRuntimeEnvironment = TRUE; if (!$image->save($derivative_uri)) { if (file_exists($derivative_uri)) { @@ -311,26 +328,40 @@ public function createDerivative($original_uri, $derivative_uri) { * {@inheritdoc} */ public function transformDimensions(array &$dimensions, $uri) { - $this->getEffects()->resetRuntimeVariables(); + if ($this->resetRuntimeEnvironment) { + $this->getEffects()->resetRuntimeVariables(); + } foreach ($this->getEffects() as $effect) { $effect->transformDimensions($dimensions, $uri); } + $this->resetRuntimeEnvironment = TRUE; } /** * {@inheritdoc} */ public function getDerivativeExtension($extension) { - $this->getEffects()->resetRuntimeVariables(); + if ($this->resetRuntimeEnvironment) { + $this->getEffects()->resetRuntimeVariables(); + } foreach ($this->getEffects() as $effect) { $extension = $effect->getDerivativeExtension($extension); } + $this->resetRuntimeEnvironment = TRUE; return $extension; } /** * {@inheritdoc} */ + public function skipRuntimeEnvironmentReset($skip = TRUE) { + $this->resetRuntimeEnvironment = !$skip; + return $this; + } + + /** + * {@inheritdoc} + */ public function getPathToken($uri) { // Return the first 8 characters. return substr(Crypt::hmacBase64($this->id() . ':' . $this->addExtension($uri), $this->getPrivateKey() . $this->getHashSalt()), 0, 8); diff --git a/core/modules/image/src/ImageStyleInterface.php b/core/modules/image/src/ImageStyleInterface.php index 9507291..43d3ae5 100644 --- a/core/modules/image/src/ImageStyleInterface.php +++ b/core/modules/image/src/ImageStyleInterface.php @@ -153,6 +153,29 @@ public function transformDimensions(array &$dimensions, $uri); public function getDerivativeExtension($extension); /** + * Indicates to skip resetting runtime variables. + * + * Allows to skip resetting runtime variables at next call of any of + * ::createDerivative, ::transformDimensions, ::getDerivativeExtension + * methods. The setting is only relevant for next call, further calls of + * these methods will be resetting the variables, unless this method is + * called again. + * + * @param bool $skip + * (optional) When TRUE, the runtime variables will not be reset at next + * call of ::createDerivative, ::transformDimensions, + * ::getDerivativeExtension. When FALSE, runtime variables will be reset. + * Defaults to TRUE. + * + * @return $this + * + * @see ::createDerivative + * @see ::transformDimensions + * @see ::getDerivativeExtension + */ + public function skipRuntimeEnvironmentReset($skip = TRUE); + + /** * Returns a specific image effect. * * @param string $effect diff --git a/core/modules/image/src/Tests/ImageAdminStylesTest.php b/core/modules/image/src/Tests/ImageAdminStylesTest.php index f2abd78..62e9aa5 100644 --- a/core/modules/image/src/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/src/Tests/ImageAdminStylesTest.php @@ -503,6 +503,16 @@ public function testImageStyleRuntimeVariables() { $this->assertEqual(20, $dimensions['width']); $this->assertEqual($style->id(), $style->getEffects()->getRuntimeVariables()->get('image_style_id')); $this->assertEqual(200, $style->getEffects()->getRuntimeVariables()->get('test_variable')); + + // Set skipping runtime environment reset and re-run transform dimensions. + // 'test_variable' should be at 400. + $style->skipRuntimeEnvironmentReset()->transformDimensions($dimensions, NULL); + $this->assertEqual(400, $style->getEffects()->getRuntimeVariables()->get('test_variable')); + + // Run transform dimensions again, the runtime variables should have been + // reset. 'test_variable' should be at 200. + $style->transformDimensions($dimensions, NULL); + $this->assertEqual(200, $style->getEffects()->getRuntimeVariables()->get('test_variable')); } }