diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 788afb2..96e442f 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -292,6 +292,7 @@ public function createDerivative($original_uri, $derivative_uri) { return FALSE; } + $this->getEffects()->resetRuntimeVariables(); foreach ($this->getEffects() as $effect) { $effect->applyEffect($image); } @@ -310,6 +311,7 @@ public function createDerivative($original_uri, $derivative_uri) { * {@inheritdoc} */ public function transformDimensions(array &$dimensions, $uri) { + $this->getEffects()->resetRuntimeVariables(); foreach ($this->getEffects() as $effect) { $effect->transformDimensions($dimensions, $uri); } @@ -319,6 +321,7 @@ public function transformDimensions(array &$dimensions, $uri) { * {@inheritdoc} */ public function getDerivativeExtension($extension) { + $this->getEffects()->resetRuntimeVariables(); foreach ($this->getEffects() as $effect) { $extension = $effect->getDerivativeExtension($extension); } diff --git a/core/modules/image/src/ImageEffectPluginCollection.php b/core/modules/image/src/ImageEffectPluginCollection.php index db51f2d..ba4af43 100644 --- a/core/modules/image/src/ImageEffectPluginCollection.php +++ b/core/modules/image/src/ImageEffectPluginCollection.php @@ -7,12 +7,13 @@ namespace Drupal\image; +use Drupal\Core\KeyValueStore\MemoryStorage; use Drupal\Core\Plugin\DefaultLazyPluginCollection; /** * A collection of image effects. */ -class ImageEffectPluginCollection extends DefaultLazyPluginCollection { +class ImageEffectPluginCollection extends DefaultLazyPluginCollection implements ImageEffectPluginCollectionInterface { /** * The unique ID of the image style using these effects. @@ -22,6 +23,13 @@ class ImageEffectPluginCollection extends DefaultLazyPluginCollection { protected $imageStyleId; /** + * A pool of runtime variables for the image effects. + * + * @var \Drupal\Core\KeyValueStore\MemoryStorage + */ + protected $runtimeVariables; + + /** * Constructs a new ImageEffectPluginCollection object. * * @param \Drupal\image\ImageEffectManager $manager @@ -35,6 +43,8 @@ class ImageEffectPluginCollection extends DefaultLazyPluginCollection { public function __construct(ImageEffectManager $manager, array $configurations, $image_style_id) { parent::__construct($manager, $configurations); $this->imageStyleId = $image_style_id; + $this->runtimeVariables = new MemoryStorage('runtime_variables'); + $this->resetRuntimeVariables(); } /** @@ -54,7 +64,7 @@ protected function initializePlugin($instance_id) { $plugin_instance = $this->pluginInstances[$instance_id]; if ($plugin_instance instanceof ImageStyleAwareInterface) { - $plugin_instance->setImageStyleId($this->imageStyleId); + $plugin_instance->setRuntimeVariables($this->runtimeVariables); } } @@ -71,4 +81,20 @@ public function sortHelper($aID, $bID) { return ($a_weight < $b_weight) ? -1 : 1; } + /** + * {@inheritdoc} + */ + public function getRuntimeVariables() { + return $this->runtimeVariables; + } + + /** + * {@inheritdoc} + */ + public function resetRuntimeVariables() { + $this->runtimeVariables->deleteAll(); + $this->runtimeVariables->set('image_style_id', $this->imageStyleId); + return $this; + } + } diff --git a/core/modules/image/src/ImageEffectPluginCollectionInterface.php b/core/modules/image/src/ImageEffectPluginCollectionInterface.php new file mode 100644 index 0000000..546ec7e --- /dev/null +++ b/core/modules/image/src/ImageEffectPluginCollectionInterface.php @@ -0,0 +1,37 @@ +runtimeVariables); + } + + /** + * Returns the pool of runtime variables. + * + * @return \Drupal\Core\KeyValueStore\MemoryStorage|null + * The runtime variables, or NULL if not set. + */ + protected function getRuntimeVariables() { + return $this->runtimeVariables; + } /** - * Implements \Drupal\image\ImageStyleAwareInterface::setImageStyleId(). + * Implements \Drupal\image\ImageStyleAwareInterface::setRuntimeVariables(). */ - public function setImageStyleId($image_style_id) { - $this->imageStyleId = $image_style_id; + public function setRuntimeVariables($runtime_variables) { + $this->runtimeVariables = $runtime_variables; return $this; } /** * Gets the image style object this effect belongs to. * - * @return \Drupal\image\ImageStyleInterface - * The image style this effect belongs to. + * @return \Drupal\image\ImageStyleInterface|null + * The image style this effect belongs to, or NULL if runtime variables are + * not available. */ protected function getImageStyle() { - if (!$this->imageStyle) { - $this->imageStyle = ImageStyle::load($this->imageStyleId); + if ($this->getRuntimeVariables() && $this->getRuntimeVariables()->has('image_style_id')) { + return ImageStyle::load($this->getRuntimeVariables()->get('image_style_id')); } - return $this->imageStyle; + return NULL; } } diff --git a/core/modules/image/src/Tests/ImageAdminStylesTest.php b/core/modules/image/src/Tests/ImageAdminStylesTest.php index 6c4f2cf..f2abd78 100644 --- a/core/modules/image/src/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/src/Tests/ImageAdminStylesTest.php @@ -474,4 +474,35 @@ public function testImageStyleAccess() { $this->assertRaw(t('Select a new effect')); } + /** + * Tests image style runtime variables. + */ + 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', + 'data' => [], + 'weight' => 2, + ]; + $style->addImageEffect($effect); + $effect['weight'] = 4; + $style->addImageEffect($effect); + $style->save(); + + // Style ID should be in the runtime variables. + $this->assertEqual($style->id(), $style->getEffects()->getRuntimeVariables()->get('image_style_id')); + + // Run through style transform dimensions and check runtime variable is + // set at the end. + $dimensions = [ + 'width' => 10, + 'height' => 10, + ]; + $style->transformDimensions($dimensions, NULL); + $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')); + } + } 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/ImageStyleAwareTestImageEffect.php index 1f38963..3638eed 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/ImageStyleAwareTestImageEffect.php @@ -28,7 +28,17 @@ class ImageStyleAwareTestImageEffect extends ImageEffectBase implements ImageSty /** * {@inheritdoc} */ + public function transformDimensions(array &$dimensions, $uri) { + $dimensions['width'] = 20; + $dimensions['height'] = 20; + $this->setTestVariable(); + } + + /** + * {@inheritdoc} + */ public function applyEffect(ImageInterface $image) { + $this->setTestVariable(); return TRUE; } @@ -39,4 +49,15 @@ public function label() { return $this->getImageStyle()->getThirdPartySetting('image_module_test', 'foo'); } + /** + * Sets the runtime test variable. + */ + protected function setTestVariable() { + if ($this->hasRuntimeVariables()) { + $test_variable = $this->getRuntimeVariables()->get('test_variable', 0); + $test_variable += 100; + $this->getRuntimeVariables()->set('test_variable', $test_variable); + } + } + }