Commit 6bf9738a authored by alexpott's avatar alexpott

Issue #2251223 by tim.plunkett: ConfigurableImageEffectInterface should use...

Issue #2251223 by tim.plunkett: ConfigurableImageEffectInterface should use ConfigurablePluginInterface and PluginFormInterface.
parent 4d347113
<?php
/**
* @file
* Contains \Drupal\image\ConfigurableImageEffectBase.
*/
namespace Drupal\image;
/**
* Provides a base class for configurable image effects.
*/
abstract class ConfigurableImageEffectBase extends ImageEffectBase implements ConfigurableImageEffectInterface {
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
}
}
......@@ -7,20 +7,10 @@
namespace Drupal\image;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Defines the interface for configurable image effects.
*/
interface ConfigurableImageEffectInterface extends ImageEffectInterface {
/**
* 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
* image effect. All other aspects of the form are handled by calling code.
*
* @return array
* A render array.
*/
public function getForm();
interface ConfigurableImageEffectInterface extends ImageEffectInterface, PluginFormInterface {
}
......@@ -348,10 +348,10 @@ public function getPluginBags() {
/**
* {@inheritdoc}
*/
public function saveImageEffect(array $configuration) {
$effect_id = $this->getEffects()->updateConfiguration($configuration);
$this->save();
return $effect_id;
public function addImageEffect(array $configuration) {
$configuration['uuid'] = $this->uuidGenerator()->generate();
$this->getEffects()->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
/**
......
......@@ -77,7 +77,7 @@ public function buildForm(array $form, array &$form_state, ImageStyleInterface $
'#value' => $this->imageEffect->getPluginId(),
);
$form['data'] = $this->imageEffect->getForm();
$form['data'] = $this->imageEffect->buildConfigurationForm(array(), $form_state);
$form['data']['#tree'] = TRUE;
// Check the URL for a weight, then the image effect, otherwise use default.
......@@ -98,12 +98,35 @@ public function buildForm(array $form, array &$form_state, ImageStyleInterface $
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
// The image effect configuration is stored in the 'data' key in the form,
// pass that through for validation.
$effect_data = array(
'values' => &$form_state['values']['data']
);
$this->imageEffect->validateConfigurationForm($form, $effect_data);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
form_state_values_clean($form_state);
$this->imageStyle->saveImageEffect($form_state['values']);
// The image effect configuration is stored in the 'data' key in the form,
// pass that through for submission.
$effect_data = array(
'values' => &$form_state['values']['data']
);
$this->imageEffect->submitConfigurationForm($form, $effect_data);
$this->imageEffect->setWeight($form_state['values']['weight']);
if (!$this->imageEffect->getUuid()) {
$this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
}
$this->imageStyle->save();
drupal_set_message($this->t('The image effect was successfully applied.'));
$form_state['redirect_route'] = $this->imageStyle->urlInfo('edit-form');
......
......@@ -203,7 +203,8 @@ public function effectSave($form, &$form_state) {
'data' => array(),
'weight' => $form_state['values']['weight'],
);
$effect_id = $this->entity->saveImageEffect($effect);
$effect_id = $this->entity->addImageEffect($effect);
$this->entity->save();
if (!empty($effect_id)) {
drupal_set_message($this->t('The image effect was successfully applied.'));
}
......
......@@ -16,6 +16,13 @@
*/
abstract class ImageStyleFormBase extends EntityForm {
/**
* The entity being used by this form.
*
* @var \Drupal\image\ImageStyleInterface
*/
protected $entity;
/**
* The image style entity storage.
*
......
......@@ -23,29 +23,6 @@ public function &get($instance_id) {
return parent::get($instance_id);
}
/**
* Updates the configuration for an image effect instance.
*
* If there is no plugin instance yet, a new will be instantiated. Otherwise,
* the existing instance is updated with the new configuration.
*
* @param array $configuration
* The image effect configuration to set.
*
* @return string
* The image effect UUID.
*/
public function updateConfiguration(array $configuration) {
// Derive the instance ID from the configuration.
if (empty($configuration['uuid'])) {
$uuid_generator = \Drupal::service('uuid');
$configuration['uuid'] = $uuid_generator->generate();
}
$instance_id = $configuration['uuid'];
$this->addInstanceId($instance_id, $configuration);
return $instance_id;
}
/**
* {@inheritdoc}
*/
......
......@@ -7,8 +7,8 @@
namespace Drupal\image;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Image\ImageInterface;
/**
......
......@@ -159,7 +159,7 @@ public function getEffects();
* @return string
* The image effect ID.
*/
public function saveImageEffect(array $configuration);
public function addImageEffect(array $configuration);
/**
* Deletes an image effect from this style.
......
......@@ -56,8 +56,8 @@ public function defaultConfiguration() {
/**
* {@inheritdoc}
*/
public function getForm() {
$form = parent::getForm();
public function buildConfigurationForm(array $form, array &$form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['anchor'] = array(
'#type' => 'radios',
'#title' => t('Anchor'),
......
......@@ -8,8 +8,7 @@
namespace Drupal\image\Plugin\ImageEffect;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ConfigurableImageEffectInterface;
use Drupal\image\ImageEffectBase;
use Drupal\image\ConfigurableImageEffectBase;
/**
* Resizes an image resource.
......@@ -20,7 +19,7 @@
* description = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.")
* )
*/
class ResizeImageEffect extends ImageEffectBase implements ConfigurableImageEffectInterface {
class ResizeImageEffect extends ConfigurableImageEffectBase {
/**
* {@inheritdoc}
......@@ -65,7 +64,7 @@ public function defaultConfiguration() {
/**
* {@inheritdoc}
*/
public function getForm() {
public function buildConfigurationForm(array $form, array &$form_state) {
$form['width'] = array(
'#type' => 'number',
'#title' => t('Width'),
......@@ -85,4 +84,14 @@ public function getForm() {
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['height'] = $form_state['values']['height'];
$this->configuration['width'] = $form_state['values']['width'];
}
}
......@@ -8,8 +8,8 @@
namespace Drupal\image\Plugin\ImageEffect;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ConfigurableImageEffectInterface;
use Drupal\image\ImageEffectBase;
use Drupal\Core\Utility\Color;
use Drupal\image\ConfigurableImageEffectBase;
/**
* Rotates an image resource.
......@@ -20,7 +20,7 @@
* description = @Translation("Rotating an image may cause the dimensions of an image to increase to fit the diagonal.")
* )
*/
class RotateImageEffect extends ImageEffectBase implements ConfigurableImageEffectInterface {
class RotateImageEffect extends ConfigurableImageEffectBase {
/**
* {@inheritdoc}
......@@ -94,7 +94,7 @@ public function defaultConfiguration() {
/**
* {@inheritdoc}
*/
public function getForm() {
public function buildConfigurationForm(array $form, array &$form_state) {
$form['degrees'] = array(
'#type' => 'number',
'#default_value' => $this->configuration['degrees'],
......@@ -110,7 +110,6 @@ public function getForm() {
'#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,
'#maxlength' => 7,
'#element_validate' => array(array($this, 'validateColorEffect')),
);
$form['random'] = array(
'#type' => 'checkbox',
......@@ -122,14 +121,23 @@ public function getForm() {
}
/**
* Validates to ensure a hexadecimal color value.
* {@inheritdoc}
*/
public function validateColorEffect(array $element, array &$form_state) {
if ($element['#value'] != '') {
if (!preg_match('/^#[0-9A-F]{3}([0-9A-F]{3})?$/', $element['#value'])) {
form_error($element, $form_state, t('!name must be a hexadecimal color value.', array('!name' => $element['#title'])));
}
public function validateConfigurationForm(array &$form, array &$form_state) {
if (!Color::validateHex($form_state['values']['bgcolor'])) {
form_set_error('bgcolor', $form_state, $this->t('Background color must be a hexadecimal color value.'));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['degrees'] = $form_state['values']['degrees'];
$this->configuration['bgcolor'] = $form_state['values']['bgcolor'];
$this->configuration['random'] = $form_state['values']['random'];
}
}
......@@ -63,9 +63,8 @@ public function defaultConfiguration() {
/**
* {@inheritdoc}
*/
public function getForm() {
$form = parent::getForm();
$form['#element_validate'] = array(array($this, 'validateScaleEffect'));
public function buildConfigurationForm(array $form, array &$form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['width']['#required'] = FALSE;
$form['height']['#required'] = FALSE;
$form['upscale'] = array(
......@@ -78,12 +77,22 @@ public function getForm() {
}
/**
* Validates to ensure that either a height or a width is specified.
* {@inheritdoc}
*/
public function validateScaleEffect(array $element, array &$form_state) {
if (empty($element['width']['#value']) && empty($element['height']['#value'])) {
form_error($element, $form_state, t('Width and height can not both be blank.'));
public function validateConfigurationForm(array &$form, array &$form_state) {
parent::validateConfigurationForm($form, $form_state);
if (empty($form_state['values']['width']) && empty($form_state['values']['height'])) {
form_set_error('data', $form_state, $this->t('Width and height can not both be blank.'));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, array &$form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['upscale'] = $form_state['values']['upscale'];
}
}
......@@ -42,6 +42,7 @@ function testImageDimensions() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
/** @var $style \Drupal\image\ImageStyleInterface */
$style = entity_create('image_style', array('name' => 'test', 'label' => 'Test'));
$style->save();
$generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
......@@ -69,7 +70,8 @@ function testImageDimensions() {
'weight' => 0,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="120" height="60" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -90,7 +92,8 @@ function testImageDimensions() {
'weight' => 1,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="60" height="120" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -112,7 +115,8 @@ function testImageDimensions() {
'weight' => 2,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -134,7 +138,8 @@ function testImageDimensions() {
'weight' => 3,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -152,7 +157,8 @@ function testImageDimensions() {
'weight' => 4,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -173,7 +179,8 @@ function testImageDimensions() {
'weight' => 5,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -193,7 +200,8 @@ function testImageDimensions() {
'weight' => 6,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="30" height="30" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -214,7 +222,8 @@ function testImageDimensions() {
'weight' => 7,
);
$effect_id = $style->saveImageEffect($effect);
$effect_id = $style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -233,7 +242,8 @@ function testImageDimensions() {
'weight' => 8,
);
$style->saveImageEffect($effect);
$style->addImageEffect($effect);
$style->save();
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />' . "\n");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment