diff --git a/core/modules/image/src/Entity/ImageStyle.php b/core/modules/image/src/Entity/ImageStyle.php index 32e5d3b..5ef0370 100644 --- a/core/modules/image/src/Entity/ImageStyle.php +++ b/core/modules/image/src/Entity/ImageStyle.php @@ -347,7 +347,7 @@ public function getEffect($effect) { */ public function getEffects() { if (!$this->effectsCollection) { - $this->effectsCollection = new ImageEffectPluginCollection($this->getImageEffectPluginManager(), $this->effects); + $this->effectsCollection = new ImageEffectPluginCollection($this->getImageEffectPluginManager(), $this->effects, $this); $this->effectsCollection->sort(); } return $this->effectsCollection; diff --git a/core/modules/image/src/ImageEffectPluginCollection.php b/core/modules/image/src/ImageEffectPluginCollection.php index ef17191..56688ec 100644 --- a/core/modules/image/src/ImageEffectPluginCollection.php +++ b/core/modules/image/src/ImageEffectPluginCollection.php @@ -15,6 +15,29 @@ class ImageEffectPluginCollection extends DefaultLazyPluginCollection { /** + * The image style using these effects. + * + * @var \Drupal\image\ImageStyleInterface + */ + protected $imageStyle; + + /** + * Constructs a new ImageEffectPluginCollection object. + * + * @param \Drupal\image\ImageEffectManager $manager + * The image effect manager. + * @param array $configurations + * An associative array containing the initial configuration for each plugin + * in the collection, keyed by plugin instance ID. + * @param \Drupal\image\ImageStyleInterface $image_style + * The image style using these effects. + */ + public function __construct(ImageEffectManager $manager, array $configurations, ImageStyleInterface $image_style) { + parent::__construct($manager, $configurations); + $this->imageStyle = $image_style; + } + + /** * {@inheritdoc} * * @return \Drupal\image\ImageEffectInterface @@ -26,6 +49,18 @@ public function &get($instance_id) { /** * {@inheritdoc} */ + protected function initializePlugin($instance_id) { + parent::initializePlugin($instance_id); + + $plugin_instance = $this->pluginInstances[$instance_id]; + if ($plugin_instance instanceof ImageStyleAwareInterface) { + $plugin_instance->setImageStyle($this->imageStyle); + } + } + + /** + * {@inheritdoc} + */ public function sortHelper($aID, $bID) { $a_weight = $this->get($aID)->getWeight(); $b_weight = $this->get($bID)->getWeight(); diff --git a/core/modules/image/src/ImageStyleAwareInterface.php b/core/modules/image/src/ImageStyleAwareInterface.php new file mode 100644 index 0000000..d31299f --- /dev/null +++ b/core/modules/image/src/ImageStyleAwareInterface.php @@ -0,0 +1,25 @@ +imageStyle = $image_style; + return $this; + } + + /** + * Gets the image style object this effect belongs to. + * + * @return \Drupal\image\ImageStyleInterface + * The image style this effect belongs to. + */ + protected function getImageStyle() { + return $this->imageStyle; + } + +} diff --git a/core/modules/image/src/Tests/ImageAdminStylesTest.php b/core/modules/image/src/Tests/ImageAdminStylesTest.php index 3ecf7c7..d15b1ed 100644 --- a/core/modules/image/src/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/src/Tests/ImageAdminStylesTest.php @@ -8,6 +8,7 @@ namespace Drupal\image\Tests; use Drupal\Component\Utility\String; +use Drupal\image\Entity\ImageStyle; use Drupal\image\ImageStyleInterface; use Drupal\node\Entity\Node; @@ -96,6 +97,9 @@ function testStyle() { 'random' => 1, 'bgcolor' => '#FFFF00', ), + 'image_module_test_image_style_aware' => [ + // No options for the test effect. + ], ); // Add style form. @@ -129,7 +133,7 @@ function testStyle() { } // Load the saved image style. - $style = entity_load('image_style', $style_name); + $style = ImageStyle::load($style_name); // Ensure that third party settings were added to the config entity. // These are added by a hook_image_style_presave() implemented in @@ -150,6 +154,11 @@ function testStyle() { foreach ($effect_edits[$effect->getPluginId()] as $field => $value) { $this->assertEqual($value, $effect_configuration['data'][$field], String::format('The %field field in the %effect effect has the correct value of %value.', array('%field' => $field, '%effect' => $effect->getPluginId(), '%value' => $value))); } + // 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.'); + } } // Assert that every effect was saved. @@ -258,7 +267,7 @@ function testStyle() { $this->drupalPostForm($style_path, array('new' => 'image_rotate'), t('Add')); $this->drupalPostForm(NULL, $edit, t('Add effect')); $style = entity_load_unchanged('image_style', $style_name); - $this->assertEqual(count($style->getEffects()), 6, 'Rotate effect with transparent background was added.'); + $this->assertEqual(count($style->getEffects()), 7, 'Rotate effect with transparent background was added.'); // Style deletion form. 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 a36719d..5619b3f 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 @@ -5,3 +5,7 @@ image_style.third_party.image_module_test: foo: type: string label: 'Label for foo' + +image.effect.image_module_test_image_style_aware: + type: image_size + label: 'Image module test image-style-aware' 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 new file mode 100644 index 0000000..1f38963 --- /dev/null +++ b/core/modules/image/tests/modules/image_module_test/src/Plugin/ImageEffect/ImageStyleAwareTestImageEffect.php @@ -0,0 +1,42 @@ +getImageStyle()->getThirdPartySetting('image_module_test', 'foo'); + } + +}