From f083251e5c9b1b37d57a883a6870bbee832aab19 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sun, 11 Aug 2013 14:43:15 +0300 Subject: [PATCH] Issue #2062573 by claudiu.cristea: Add method setConfigurationDefaults in ConfigurableImageEffectInterface. --- .../image/ConfigurableImageEffectInterface.php | 8 ++++++++ .../image/lib/Drupal/image/ImageEffectBase.php | 9 +++++++- .../image/Plugin/ImageEffect/CropImageEffect.php | 17 ++++++++------- .../image/Plugin/ImageEffect/ResizeImageEffect.php | 14 +++++++++++-- .../image/Plugin/ImageEffect/RotateImageEffect.php | 24 +++++++++++++--------- .../image/Plugin/ImageEffect/ScaleImageEffect.php | 18 ++++++++-------- 6 files changed, 60 insertions(+), 30 deletions(-) diff --git a/core/modules/image/lib/Drupal/image/ConfigurableImageEffectInterface.php b/core/modules/image/lib/Drupal/image/ConfigurableImageEffectInterface.php index 292aa4c..f920c92 100644 --- a/core/modules/image/lib/Drupal/image/ConfigurableImageEffectInterface.php +++ b/core/modules/image/lib/Drupal/image/ConfigurableImageEffectInterface.php @@ -13,6 +13,14 @@ interface ConfigurableImageEffectInterface extends ImageEffectInterface { /** + * Define the effect configuration defaults. + * + * @return array + * An associative array with defaults keyed by configuration names. + */ + public function getConfigurationDefaults(); + + /** * Builds the part of the image effect form specific to this image effect. * * This method is only responsible for the form elements specific to this diff --git a/core/modules/image/lib/Drupal/image/ImageEffectBase.php b/core/modules/image/lib/Drupal/image/ImageEffectBase.php index 2babe0d..e898045 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectBase.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectBase.php @@ -103,10 +103,17 @@ public function setConfiguration(array $configuration) { 'uuid' => '', 'weight' => '', ); - $this->configuration = $configuration['data']; + $this->configuration = $configuration['data'] + $this->getConfigurationDefaults(); $this->uuid = $configuration['uuid']; $this->weight = $configuration['weight']; return $this; } + /** + * Fallback for non-configurable effects. + */ + public function getConfigurationDefaults() { + return array(); + } + } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php index e46d2fa..751851e 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php @@ -26,11 +26,6 @@ class CropImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - // Set sane default values. - $this->configuration += array( - 'anchor' => 'center-center', - ); - list($x, $y) = explode('-', $this->configuration['anchor']); $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']); $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']); @@ -54,12 +49,16 @@ public function getSummary() { /** * {@inheritdoc} */ - public function getForm() { - $this->configuration += array( - 'width' => '', - 'height' => '', + public function getConfigurationDefaults() { + return parent::getConfigurationDefaults() + array( 'anchor' => 'center-center', ); + } + + /** + * {@inheritdoc} + */ + public function getForm() { $form = parent::getForm(); $form['anchor'] = array( '#type' => 'radios', diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php index 766e5a7..60f055d 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php @@ -57,11 +57,21 @@ public function getSummary() { /** * {@inheritdoc} */ + public function getConfigurationDefaults() { + return array( + 'width' => NULL, + 'height' => NULL, + ); + } + + /** + * {@inheritdoc} + */ public function getForm() { $form['width'] = array( '#type' => 'number', '#title' => t('Width'), - '#default_value' => isset($this->configuration['width']) ? $this->configuration['width'] : '', + '#default_value' => $this->configuration['width'], '#field_suffix' => ' ' . t('pixels'), '#required' => TRUE, '#min' => 1, @@ -69,7 +79,7 @@ public function getForm() { $form['height'] = array( '#type' => 'number', '#title' => t('Height'), - '#default_value' => isset($this->configuration['height']) ? $this->configuration['height'] : '', + '#default_value' => $this->configuration['height'], '#field_suffix' => ' ' . t('pixels'), '#required' => TRUE, '#min' => 1, diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php index fe563ae..2920c7b 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php @@ -28,13 +28,6 @@ class RotateImageEffect extends ImageEffectBase implements ConfigurableImageEffe * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - // Set sane default values. - $this->configuration += array( - 'degrees' => 0, - 'bgcolor' => NULL, - 'random' => FALSE, - ); - // Convert short #FFF syntax to full #FFFFFF syntax. if (strlen($this->configuration['bgcolor']) == 4) { $c = $this->configuration['bgcolor']; @@ -92,10 +85,21 @@ public function getSummary() { /** * {@inheritdoc} */ + public function getConfigurationDefaults() { + return array( + 'degrees' => 0, + 'bgcolor' => NULL, + 'random' => FALSE, + ); + } + + /** + * {@inheritdoc} + */ public function getForm() { $form['degrees'] = array( '#type' => 'number', - '#default_value' => (isset($this->configuration['degrees'])) ? $this->configuration['degrees'] : 0, + '#default_value' => $this->configuration['degrees'], '#title' => t('Rotation angle'), '#description' => t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'), '#field_suffix' => '°', @@ -103,7 +107,7 @@ public function getForm() { ); $form['bgcolor'] = array( '#type' => 'textfield', - '#default_value' => (isset($this->configuration['bgcolor'])) ? $this->configuration['bgcolor'] : '#FFFFFF', + '#default_value' => $this->configuration['bgcolor'], '#title' => t('Background color'), '#description' => t('The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave blank for transparency on image types that support it.'), '#size' => 7, @@ -112,7 +116,7 @@ public function getForm() { ); $form['random'] = array( '#type' => 'checkbox', - '#default_value' => (isset($this->configuration['random'])) ? $this->configuration['random'] : 0, + '#default_value' => $this->configuration['random'], '#title' => t('Randomize'), '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'), ); diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php index c7b2f53..8534688 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php @@ -27,13 +27,6 @@ class ScaleImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - // Set sane default values. - $this->configuration += array( - 'width' => NULL, - 'height' => NULL, - 'upscale' => FALSE, - ); - if (!$image->scale($this->configuration['width'], $this->configuration['height'], $this->configuration['upscale'])) { watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()), WATCHDOG_ERROR); return FALSE; @@ -63,6 +56,15 @@ public function getSummary() { /** * {@inheritdoc} */ + public function getConfigurationDefaults() { + return parent::getConfigurationDefaults() + array( + 'upscale' => FALSE, + ); + } + + /** + * {@inheritdoc} + */ public function getForm() { $form = parent::getForm(); $form['#element_validate'] = array(array($this, 'validateScaleEffect')); @@ -70,7 +72,7 @@ public function getForm() { $form['height']['#required'] = FALSE; $form['upscale'] = array( '#type' => 'checkbox', - '#default_value' => (isset($this->configuration['upscale'])) ? $this->configuration['upscale'] : 0, + '#default_value' => $this->configuration['upscale'], '#title' => t('Allow Upscaling'), '#description' => t('Let scale make images larger than their original size'), ); -- 1.8.3.1