diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index c9e50ed..8aa96eb 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -80,20 +80,6 @@ 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 @@ -306,13 +292,11 @@ public function createDerivative($original_uri, $derivative_uri) { return FALSE; } - if ($this->resetRuntimeEnvironment) { - $this->getEffects()->resetRuntimeVariables(); - } + $this->getEffects()->resetRuntimeVariables(); foreach ($this->getEffects() as $effect) { $effect->applyEffect($image); } - $this->resetRuntimeEnvironment = TRUE; + $this->getEffects()->skipRuntimeEnvironmentReset(FALSE); if (!$image->save($derivative_uri)) { if (file_exists($derivative_uri)) { @@ -328,40 +312,28 @@ public function createDerivative($original_uri, $derivative_uri) { * {@inheritdoc} */ public function transformDimensions(array &$dimensions, $uri) { - if ($this->resetRuntimeEnvironment) { - $this->getEffects()->resetRuntimeVariables(); - } + $this->getEffects()->resetRuntimeVariables(); foreach ($this->getEffects() as $effect) { $effect->transformDimensions($dimensions, $uri); } - $this->resetRuntimeEnvironment = TRUE; + $this->getEffects()->skipRuntimeEnvironmentReset(FALSE); } /** * {@inheritdoc} */ public function getDerivativeExtension($extension) { - if ($this->resetRuntimeEnvironment) { - $this->getEffects()->resetRuntimeVariables(); - } + $this->getEffects()->resetRuntimeVariables(); foreach ($this->getEffects() as $effect) { $extension = $effect->getDerivativeExtension($extension); } - $this->resetRuntimeEnvironment = TRUE; + $this->getEffects()->skipRuntimeEnvironmentReset(FALSE); 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/ImageEffectPluginCollection.php b/core/modules/image/src/ImageEffectPluginCollection.php index ba4af43..1858f09 100644 --- a/core/modules/image/src/ImageEffectPluginCollection.php +++ b/core/modules/image/src/ImageEffectPluginCollection.php @@ -30,6 +30,16 @@ class ImageEffectPluginCollection extends DefaultLazyPluginCollection implements protected $runtimeVariables; /** + * Indicates whether runtime variables should be reset. + * + * @var bool + * + * @see ::resetRuntimeVariables + * @see ::skipRuntimeEnvironmentReset + */ + protected $resetRuntimeEnvironment = TRUE; + + /** * Constructs a new ImageEffectPluginCollection object. * * @param \Drupal\image\ImageEffectManager $manager @@ -63,7 +73,7 @@ protected function initializePlugin($instance_id) { parent::initializePlugin($instance_id); $plugin_instance = $this->pluginInstances[$instance_id]; - if ($plugin_instance instanceof ImageStyleAwareInterface) { + if ($plugin_instance instanceof ImageEffectWithRuntimeVariablesInterface) { $plugin_instance->setRuntimeVariables($this->runtimeVariables); } } @@ -92,8 +102,18 @@ public function getRuntimeVariables() { * {@inheritdoc} */ public function resetRuntimeVariables() { - $this->runtimeVariables->deleteAll(); - $this->runtimeVariables->set('image_style_id', $this->imageStyleId); + if ($this->resetRuntimeEnvironment) { + $this->runtimeVariables->deleteAll(); + $this->runtimeVariables->set('image_style_id', $this->imageStyleId); + } + return $this; + } + + /** + * {@inheritdoc} + */ + public function skipRuntimeEnvironmentReset($skip = TRUE) { + $this->resetRuntimeEnvironment = !$skip; return $this; } diff --git a/core/modules/image/src/ImageEffectPluginCollectionInterface.php b/core/modules/image/src/ImageEffectPluginCollectionInterface.php index 546ec7e..5a2368d 100644 --- a/core/modules/image/src/ImageEffectPluginCollectionInterface.php +++ b/core/modules/image/src/ImageEffectPluginCollectionInterface.php @@ -30,8 +30,28 @@ public function getRuntimeVariables(); * * @return $this * + * @see ::skipRuntimeEnvironmentReset * @see \Drupal\image\ImageStyleAwareTrait::getImageStyle */ public function resetRuntimeVariables(); + /** + * Indicates to skip resetting runtime variables. + * + * Allows to skip resetting runtime variables at next call of + * ::resetRuntimeVariables. + * + * @param bool $skip + * (optional) When TRUE, the runtime variables will not be reset at next + * call of ::resetRuntimeVariables. Defaults to TRUE. + * + * @return $this + * + * @see ::resetRuntimeVariables + * @see \Drupal\image\Entity\ImageStyle::createDerivative + * @see \Drupal\image\Entity\ImageStyle::transformDimensions + * @see \Drupal\image\Entity\ImageStyle::getDerivativeExtension + */ + public function skipRuntimeEnvironmentReset($skip = TRUE); + } diff --git a/core/modules/image/src/ImageStyleAwareInterface.php b/core/modules/image/src/ImageEffectWithRuntimeVariablesInterface.php similarity index 49% rename from core/modules/image/src/ImageStyleAwareInterface.php rename to core/modules/image/src/ImageEffectWithRuntimeVariablesInterface.php index d420bf0..ae2e5f5 100644 --- a/core/modules/image/src/ImageStyleAwareInterface.php +++ b/core/modules/image/src/ImageEffectWithRuntimeVariablesInterface.php @@ -2,15 +2,17 @@ /** * @file - * Contains \Drupal\image\ImageStyleAwareInterface. + * Contains \Drupal\image\ImageEffectWithRuntimeVariablesInterface. */ namespace Drupal\image; +use Drupal\Core\KeyValueStore\MemoryStorage; + /** - * Allows an image effect to be aware of the image style that contains it. + * Allows an image effect to be aware of a pool of runtime variables. */ -interface ImageStyleAwareInterface { +interface ImageEffectWithRuntimeVariablesInterface { /** * Sets the the pool of runtime variables for the image effects. @@ -20,6 +22,6 @@ * * @return $this */ - public function setRuntimeVariables($runtime_variables); + public function setRuntimeVariables(MemoryStorage $runtime_variables); } diff --git a/core/modules/image/src/ImageStyleAwareTrait.php b/core/modules/image/src/ImageEffectWithRuntimeVariablesTrait.php similarity index 65% rename from core/modules/image/src/ImageStyleAwareTrait.php rename to core/modules/image/src/ImageEffectWithRuntimeVariablesTrait.php index 8e22485..d715dd2 100644 --- a/core/modules/image/src/ImageStyleAwareTrait.php +++ b/core/modules/image/src/ImageEffectWithRuntimeVariablesTrait.php @@ -2,19 +2,20 @@ /** * @file - * Contains \Drupal\image\ImageStyleAwareTrait. + * Contains \Drupal\image\ImageEffectWithRuntimeVariablesTrait. */ namespace Drupal\image; +use Drupal\Core\KeyValueStore\MemoryStorage; use Drupal\image\Entity\ImageStyle; /** - * Provides an implementation for a image-style-aware object. + * Provides an implementation for an image effect with runtime variables. * - * @see \Drupal\image\ImageStyleAwareInterface + * @see \Drupal\image\ImageEffectWithRuntimeVariablesInterface */ -trait ImageStyleAwareTrait { +trait ImageEffectWithRuntimeVariablesTrait { /** * The shared pool of runtime variables for the image effects. @@ -44,9 +45,14 @@ protected function getRuntimeVariables() { } /** - * Implements \Drupal\image\ImageStyleAwareInterface::setRuntimeVariables(). + * Sets the the pool of runtime variables for the image effects. + * + * @param \Drupal\Core\KeyValueStore\MemoryStorage $runtime_variables; + * The pool of runtime variables for the image effects. + * + * @return $this */ - public function setRuntimeVariables($runtime_variables) { + public function setRuntimeVariables(MemoryStorage $runtime_variables) { $this->runtimeVariables = $runtime_variables; return $this; } @@ -59,7 +65,7 @@ public function setRuntimeVariables($runtime_variables) { * not available. */ protected function getImageStyle() { - if ($this->getRuntimeVariables() && $this->getRuntimeVariables()->has('image_style_id')) { + if ($this->hasRuntimeVariables() && $this->getRuntimeVariables()->has('image_style_id')) { return ImageStyle::load($this->getRuntimeVariables()->get('image_style_id')); } return NULL; diff --git a/core/modules/image/src/ImageStyleInterface.php b/core/modules/image/src/ImageStyleInterface.php index 43d3ae5..9507291 100644 --- a/core/modules/image/src/ImageStyleInterface.php +++ b/core/modules/image/src/ImageStyleInterface.php @@ -153,29 +153,6 @@ 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 62e9aa5..eea99f4 100644 --- a/core/modules/image/src/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/src/Tests/ImageAdminStylesTest.php @@ -98,7 +98,7 @@ function testStyle() { 'random' => 1, 'bgcolor' => '#FFFF00', ), - 'image_module_test_image_style_aware' => [ + 'image_module_test_effect_with_runtime_variables' => [ // No options for the test effect. ], ); @@ -157,8 +157,8 @@ function testStyle() { } // Check that ImageStyle third party settings are accessible from within // the effect. - if ($effect->getPluginId() === 'image_module_test_image_style_aware') { - $this->assertEqual('bar', $effect->label(), 'Image style third party settings are accessible from the effect.'); + if ($effect->getPluginId() === 'image_module_test_effect_with_runtime_variables') { + $this->assertEqual('bar', $effect->label()); } } @@ -481,7 +481,7 @@ public function testImageStyleRuntimeVariables() { // Create a test image style, with runtime variables aware image effect. $style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]); $effect = [ - 'id' => 'image_module_test_image_style_aware', + 'id' => 'image_module_test_effect_with_runtime_variables', 'data' => [], 'weight' => 2, ]; @@ -506,7 +506,8 @@ public function testImageStyleRuntimeVariables() { // Set skipping runtime environment reset and re-run transform dimensions. // 'test_variable' should be at 400. - $style->skipRuntimeEnvironmentReset()->transformDimensions($dimensions, NULL); + $style->getEffects()->skipRuntimeEnvironmentReset(); + $style->transformDimensions($dimensions, NULL); $this->assertEqual(400, $style->getEffects()->getRuntimeVariables()->get('test_variable')); // Run transform dimensions again, the runtime variables should have been diff --git a/core/modules/image/tests/modules/image_module_test/config/schema/image_module_test.schema.yml b/core/modules/image/tests/modules/image_module_test/config/schema/image_module_test.schema.yml index 9a8240d..8d62a7f 100644 --- a/core/modules/image/tests/modules/image_module_test/config/schema/image_module_test.schema.yml +++ b/core/modules/image/tests/modules/image_module_test/config/schema/image_module_test.schema.yml @@ -6,6 +6,6 @@ image.style.*.third_party.image_module_test: type: string label: 'Label for foo' -image.effect.image_module_test_image_style_aware: +image.effect.image_module_test_effect_with_runtime_variables: type: image_size - label: 'Image module test image-style-aware' + label: 'Image module test effect with runtime variables' diff --git a/core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/ImageStyleAwareTestImageEffect.php b/core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/EffectWithRuntimeVariables.php similarity index 55% rename from core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/ImageStyleAwareTestImageEffect.php rename to core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/EffectWithRuntimeVariables.php index 3638eed..ae6a4fb 100644 --- a/core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/ImageStyleAwareTestImageEffect.php +++ b/core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/EffectWithRuntimeVariables.php @@ -2,28 +2,27 @@ /** * @file - * Contains - * \Drupal\image_module_test\Plugin\ImageEffect\ImageStyleAwareTestImageEffect. + * Contains \Drupal\image_module_test\Plugin\ImageEffect\EffectWithRuntimeVariables. */ namespace Drupal\image_module_test\Plugin\ImageEffect; use Drupal\Core\Image\ImageInterface; -use Drupal\image\ImageStyleAwareTrait; use Drupal\image\ImageEffectBase; -use Drupal\image\ImageStyleAwareInterface; +use Drupal\image\ImageEffectWithRuntimeVariablesInterface; +use Drupal\image\ImageEffectWithRuntimeVariablesTrait; /** - * Provides an image-style-aware image effect. + * Provides a test image effect with runtime variables. * * @ImageEffect( - * id = "image_module_test_image_style_aware", - * label = @Translation("Image module test image-style-aware") + * id = "image_module_test_effect_with_runtime_variables", + * label = @Translation("Image module test effect with runtime variables") * ) */ -class ImageStyleAwareTestImageEffect extends ImageEffectBase implements ImageStyleAwareInterface { +class EffectWithRuntimeVariables extends ImageEffectBase implements ImageEffectWithRuntimeVariablesInterface { - use ImageStyleAwareTrait; + use ImageEffectWithRuntimeVariablesTrait; /** * {@inheritdoc} @@ -46,7 +45,12 @@ public function applyEffect(ImageInterface $image) { * {@inheritdoc} */ public function label() { - return $this->getImageStyle()->getThirdPartySetting('image_module_test', 'foo'); + if ($this->hasRuntimeVariables()) { + return $this->getImageStyle()->getThirdPartySetting('image_module_test', 'foo'); + } + else { + return 'qux'; + } } /**