diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTest.php index 922a32b..ce81657 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigSchemaTest.php @@ -105,7 +105,7 @@ function testSchemaMapping() { $expected['mapping']['effects']['type'] = 'sequence'; $expected['mapping']['effects']['sequence'][0]['type'] = 'mapping'; $expected['mapping']['effects']['sequence'][0]['mapping']['id']['type'] = 'string'; - $expected['mapping']['effects']['sequence'][0]['mapping']['data']['type'] = 'image.effect.[%parent.name]'; + $expected['mapping']['effects']['sequence'][0]['mapping']['data']['type'] = 'image.effect.[%parent.id]'; $expected['mapping']['effects']['sequence'][0]['mapping']['weight']['type'] = 'integer'; $expected['mapping']['effects']['sequence'][0]['mapping']['ieid']['type'] = 'string'; $expected['mapping']['langcode']['label'] = 'Default language'; diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index 6916f19..5c4692a 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -26,10 +26,10 @@ image.style.*: sequence: - type: mapping mapping: - name: + id: type: string data: - type: image.effect.[%parent.name] + type: image.effect.[%parent.id] weight: type: integer ieid: diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc index 25f71b3..e1cbbbf 100644 --- a/core/modules/image/image.admin.inc +++ b/core/modules/image/image.admin.inc @@ -38,6 +38,7 @@ function image_style_list() { function image_style_form($form, &$form_state, $style) { $title = t('Edit style %name', array('%name' => $style->label())); drupal_set_title($title, PASS_THROUGH); + $manager = Drupal::service('plugin.manager.image.effect'); $form_state['image_style'] = $style; $form['#tree'] = TRUE; @@ -71,13 +72,12 @@ function image_style_form($form, &$form_state, $style) { ); if (!empty($style->effects)) { foreach ($style->effects as $key => $effect) { + $effect_instance = $manager->getInstance($effect); $form['effects'][$key]['#weight'] = isset($form_state['input']['effects']) ? $form_state['input']['effects'][$key]['weight'] : NULL; $form['effects'][$key]['label'] = array( '#markup' => check_plain($effect['label']), ); - $form['effects'][$key]['summary'] = array( - '#markup' => isset($effect['summary_theme']) ? theme($effect['summary_theme'], array('data' => $effect['data'])) : '', - ); + $form['effects'][$key]['summary'] = $effect_instance->getSummary(); $form['effects'][$key]['weight'] = array( '#type' => 'weight', '#title' => t('Weight for @title', array('@title' => $effect['label'])), @@ -86,7 +86,7 @@ function image_style_form($form, &$form_state, $style) { ); $links = array(); - if (isset($effect['form_callback'])) { + if (!$effect['no_form']) { $links['edit'] = array( 'title' => t('edit'), 'href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key, @@ -104,7 +104,7 @@ function image_style_form($form, &$form_state, $style) { '#type' => 'link', '#title' => t('edit'), '#href' => 'admin/config/media/image-styles/manage/' . $style->id() . '/effects/' . $key, - '#access' => isset($effect['form_callback']), + '#access' => !$effect['no_form'], ); $form['effects'][$key]['remove'] = array( '#type' => 'link', @@ -116,7 +116,7 @@ function image_style_form($form, &$form_state, $style) { // Build the new image effect addition form and add it to the effect list. $new_effect_options = array(); - $effects = Drupal::service('plugin.manager.image.effect')->getDefinitions(); + $effects = $manager->getDefinitions(); uasort($effects, '_image_effect_definitions_sort'); foreach ($effects as $effect => $definition) { $new_effect_options[$effect] = $definition['label']; @@ -173,7 +173,7 @@ function image_style_form_add_submit($form, &$form_state) { $effect = Drupal::service('plugin.manager.image.effect')->getDefinition($form_state['values']['new']); // Load the configuration form for this option. - if (isset($effect['form_callback'])) { + if (!$effect['no_form']) { $path = 'admin/config/media/image-styles/manage/' . $style->id() . '/add/' . $form_state['values']['new']; $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight']))); } @@ -267,8 +267,8 @@ function image_style_add_form_submit($form, &$form_state) { * Form builder; Form for adding and editing image effects. * * This form is used universally for editing all image effects. Each effect adds - * its own custom section to the form by calling the 'form_callback' specified - * in \Drupal\image\Annotation\ImageEffect. + * its own custom section to the form by overriding + * \Drupal\image\ImageEffectInterface::getForm(). * * @param $form_state * An associative array containing the current state of the form. @@ -278,16 +278,13 @@ function image_style_add_form_submit($form, &$form_state) { * An image effect array. * * @ingroup forms - * @see image_resize_form() - * @see image_scale_form() - * @see image_rotate_form() - * @see image_crop_form() - mage.mo* @see image_effect_form_submit() + * + * @see image_effect_form_submit() */ function image_effect_form($form, &$form_state, $style, $effect) { // If there's no configuration for this image effect, return to // the image style page. - if (!isset($effect['form_callback'])) { + if ($effect['no_form']) { return new RedirectResponse(url('admin/config/media/image-styles/manage/' . $style->id(), array('absolute' => TRUE))); } $form_state['image_style'] = $style; @@ -312,7 +309,7 @@ function image_effect_form($form, &$form_state, $style, $effect) { '#value' => $effect['id'], ); - $form['data'] = call_user_func($effect['form_callback'], $effect['data']); + $form['data'] = Drupal::service('plugin.manager.image.effect')->getInstance($effect)->getForm(); $form['data']['#tree'] = TRUE; // Check the URL for a weight, then the image effect, otherwise use default. @@ -373,137 +370,6 @@ function image_effect_scale_validate($element, &$form_state) { } /** - * Form structure for the image resize form. - * - * Note that this is not a complete form, it only contains the portion of the - * form for configuring the resize options. Therefore it does not not need to - * include metadata about the effect, nor a submit button. - * - * @param $data - * The current configuration for this resize effect. - */ -function image_resize_form($data) { - $form['width'] = array( - '#type' => 'number', - '#title' => t('Width'), - '#default_value' => isset($data['width']) ? $data['width'] : '', - '#field_suffix' => ' ' . t('pixels'), - '#required' => TRUE, - '#min' => 1, - ); - $form['height'] = array( - '#type' => 'number', - '#title' => t('Height'), - '#default_value' => isset($data['height']) ? $data['height'] : '', - '#field_suffix' => ' ' . t('pixels'), - '#required' => TRUE, - '#min' => 1, - ); - return $form; -} - -/** - * Form structure for the image scale form. - * - * Note that this is not a complete form, it only contains the portion of the - * form for configuring the scale options. Therefore it does not not need to - * include metadata about the effect, nor a submit button. - * - * @param $data - * The current configuration for this scale effect. - */ -function image_scale_form($data) { - $form = image_resize_form($data); - $form['#element_validate'] = array('image_effect_scale_validate'); - $form['width']['#required'] = FALSE; - $form['height']['#required'] = FALSE; - $form['upscale'] = array( - '#type' => 'checkbox', - '#default_value' => (isset($data['upscale'])) ? $data['upscale'] : 0, - '#title' => t('Allow Upscaling'), - '#description' => t('Let scale make images larger than their original size'), - ); - return $form; -} - -/** - * Form structure for the image crop form. - * - * Note that this is not a complete form, it only contains the portion of the - * form for configuring the crop options. Therefore it does not not need to - * include metadata about the effect, nor a submit button. - * - * @param $data - * The current configuration for this crop effect. - */ -function image_crop_form($data) { - $data += array( - 'width' => '', - 'height' => '', - 'anchor' => 'center-center', - ); - - $form = image_resize_form($data); - $form['anchor'] = array( - '#type' => 'radios', - '#title' => t('Anchor'), - '#options' => array( - 'left-top' => t('Top') . ' ' . t('Left'), - 'center-top' => t('Top') . ' ' . t('Center'), - 'right-top' => t('Top') . ' ' . t('Right'), - 'left-center' => t('Center') . ' ' . t('Left'), - 'center-center' => t('Center'), - 'right-center' => t('Center') . ' ' . t('Right'), - 'left-bottom' => t('Bottom') . ' ' . t('Left'), - 'center-bottom' => t('Bottom') . ' ' . t('Center'), - 'right-bottom' => t('Bottom') . ' ' . t('Right'), - ), - '#theme' => 'image_anchor', - '#default_value' => $data['anchor'], - '#description' => t('The part of the image that will be retained during the crop.'), - ); - - return $form; -} - -/** - * Form structure for the image rotate form. - * - * Note that this is not a complete form, it only contains the portion of the - * form for configuring the rotate options. Therefore it does not not need to - * include metadata about the effect, nor a submit button. - * - * @param $data - * The current configuration for this rotate effect. - */ -function image_rotate_form($data) { - $form['degrees'] = array( - '#type' => 'number', - '#default_value' => (isset($data['degrees'])) ? $data['degrees'] : 0, - '#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' => '°', - '#required' => TRUE, - ); - $form['bgcolor'] = array( - '#type' => 'textfield', - '#default_value' => (isset($data['bgcolor'])) ? $data['bgcolor'] : '#FFFFFF', - '#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, - '#maxlength' => 7, - '#element_validate' => array('image_effect_color_validate'), - ); - $form['random'] = array( - '#type' => 'checkbox', - '#default_value' => (isset($data['random'])) ? $data['random'] : 0, - '#title' => t('Randomize'), - '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'), - ); - return $form; -} - -/** * Returns HTML for the page containing the list of image styles. * * @param $variables diff --git a/core/modules/image/image.effects.inc b/core/modules/image/image.effects.inc deleted file mode 100644 index 94845de..0000000 --- a/core/modules/image/image.effects.inc +++ /dev/null @@ -1,246 +0,0 @@ - $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); - return FALSE; - } - return TRUE; -} - -/** - * Image dimensions callback; Resize. - * - * @param array $dimensions - * Dimensions to be modified - an array with components width and height, in - * pixels. - * @param array $data - * An array of attributes to use when performing the resize effect with the - * following items: - * - "width": An integer representing the desired width in pixels. - * - "height": An integer representing the desired height in pixels. - */ -function image_resize_dimensions(array &$dimensions, array $data) { - // The new image will have the exact dimensions defined for the effect. - $dimensions['width'] = $data['width']; - $dimensions['height'] = $data['height']; -} - -/** - * Image effect callback; Scale an image resource. - * - * @param object $image - * An image object returned by image_load(). - * @param array $data - * An array of attributes to use when performing the scale effect with the - * following items: - * - "width": An integer representing the desired width in pixels. - * - "height": An integer representing the desired height in pixels. - * - "upscale": A boolean indicating that the image should be upscaled if the - * dimensions are larger than the original image. - * - * @return bool - * TRUE on success. FALSE on failure to scale image. - * - * @see image_scale() - */ -function image_scale_effect($image, array $data) { - // Set sane default values. - $data += array( - 'width' => NULL, - 'height' => NULL, - 'upscale' => FALSE, - ); - - if (!image_scale($image, $data['width'], $data['height'], $data['upscale'])) { - watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); - return FALSE; - } - return TRUE; -} - -/** - * Image effect callback; Crop an image resource. - * - * @param object $image - * An image object returned by image_load(). - * @param array $data - * An array of attributes to use when performing the crop effect with the - * following items: - * - "width": An integer representing the desired width in pixels. - * - "height": An integer representing the desired height in pixels. - * - "anchor": A string describing where the crop should originate in the form - * of "XOFFSET-YOFFSET". XOFFSET is either a number of pixels or - * "left", "center", "right" and YOFFSET is either a number of pixels or - * "top", "center", "bottom". - * - * @return bool - * TRUE on success. FALSE on failure to crop image. - * - * @see image_crop() - */ -function image_crop_effect($image, array $data) { - // Set sane default values. - $data += array( - 'anchor' => 'center-center', - ); - - list($x, $y) = explode('-', $data['anchor']); - $x = image_filter_keyword($x, $image->info['width'], $data['width']); - $y = image_filter_keyword($y, $image->info['height'], $data['height']); - if (!image_crop($image, $x, $y, $data['width'], $data['height'])) { - watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); - return FALSE; - } - return TRUE; -} - -/** - * Image effect callback; Scale and crop an image resource. - * - * @param object $image - * An image object returned by image_load(). - * @param array $data - * An array of attributes to use when performing the scale and crop effect - * with the following items: - * - "width": An integer representing the desired width in pixels. - * - "height": An integer representing the desired height in pixels. - * - * @return bool - * TRUE on success. FALSE on failure to scale and crop image. - * - * @see image_scale_and_crop() - */ -function image_scale_and_crop_effect($image, array $data) { - if (!image_scale_and_crop($image, $data['width'], $data['height'])) { - watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); - return FALSE; - } - return TRUE; -} - -/** - * Image effect callback; Desaturate (grayscale) an image resource. - * - * @param object $image - * An image object returned by image_load(). - * @param array $data - * An array of attributes to use when performing the desaturate effect. - * - * @return bool - * TRUE on success. FALSE on failure to desaturate image. - * - * @see image_desaturate() - */ -function image_desaturate_effect($image, $data) { - if (!image_desaturate($image)) { - watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); - return FALSE; - } - return TRUE; -} - -/** - * Image effect callback; Rotate an image resource. - * - * @param object $image - * An image object returned by image_load(). - * @param array $data - * An array of attributes to use when performing the rotate effect containing - * the following items: - * - "degrees": The number of (clockwise) degrees to rotate the image. - * - "random": A boolean indicating that a random rotation angle should be - * used for this image. The angle specified in "degrees" is used as a - * positive and negative maximum. - * - "bgcolor": 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. - * - * @return bool - * TRUE on success. FALSE on failure to rotate image. - * - * @see image_rotate(). - */ -function image_rotate_effect($image, $data) { - // Set sane default values. - $data += array( - 'degrees' => 0, - 'bgcolor' => NULL, - 'random' => FALSE, - ); - - // Convert short #FFF syntax to full #FFFFFF syntax. - if (strlen($data['bgcolor']) == 4) { - $c = $data['bgcolor']; - $data['bgcolor'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3]; - } - - // Convert #FFFFFF syntax to hexadecimal colors. - if ($data['bgcolor'] != '') { - $data['bgcolor'] = hexdec(str_replace('#', '0x', $data['bgcolor'])); - } - else { - $data['bgcolor'] = NULL; - } - - if (!empty($data['random'])) { - $degrees = abs((float) $data['degrees']); - $data['degrees'] = rand(-1 * $degrees, $degrees); - } - - if (!image_rotate($image, $data['degrees'], $data['bgcolor'])) { - watchdog('image', 'Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); - return FALSE; - } - return TRUE; -} - -/** - * Image dimensions callback; Rotate. - * - * @param array $dimensions - * Dimensions to be modified - an array with components width and height, in - * pixels. - * @param array $data - * An array of attributes to use when performing the rotate effect containing - * the following items: - * - "degrees": The number of (clockwise) degrees to rotate the image. - * - "random": A boolean indicating that a random rotation angle should be - * used for this image. The angle specified in "degrees" is used as a - * positive and negative maximum. - */ -function image_rotate_dimensions(array &$dimensions, array $data) { - // If the rotate is not random and the angle is a multiple of 90 degrees, - // then the new dimensions can be determined. - if (!$data['random'] && ((int) ($data['degrees']) == $data['degrees']) && ($data['degrees'] % 90 == 0)) { - if ($data['degrees'] % 180 != 0) { - $temp = $dimensions['width']; - $dimensions['width'] = $dimensions['height']; - $dimensions['height'] = $temp; - } - } - else { - $dimensions['width'] = $dimensions['height'] = NULL; - } -} diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 6c87e7d..d3bb6ab 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -532,7 +532,7 @@ function image_style_create_derivative($style, $source, $destination) { if (!empty($style->effects)) { $manager = Drupal::service('plugin.manager.image.effect'); foreach ($style->effects as $effect) { - $manager->getInstance($effect)->effect($image); + $manager->getInstance($effect)->processEffect($image); } } @@ -558,15 +558,13 @@ function image_style_create_derivative($style, $source, $destination) { * pixels. */ function image_style_transform_dimensions($style_name, array &$dimensions) { - module_load_include('inc', 'image', 'image.effects'); $style = entity_load('image_style', $style_name); if (!empty($style->effects)) { $manager = Drupal::service('plugin.manager.image.effect'); foreach ($style->effects as $effect) { - $effect_instance = $manager->getInstance($effect); - if (!$effect_instance->ignoreDimensions()) { - $effect_instance->processDimensions($dimensions); + if (!$effect['dimensions_passthrough']) { + $manager->getInstance($effect)->processDimensions($dimensions); } } } @@ -705,6 +703,32 @@ function image_style_path($style_name, $uri) { } /** + * Loads the definition for an image effect. + * + * The effect definition is a set of core properties for an image effect, not + * containing any user-settings. The definition defines various functions to + * call when configuring or executing an image effect. This loader is mostly for + * internal use within image.module. Use image_effect_load() or + * entity_load() to get image effects that contain configuration. + * + * @param $effect + * The name of the effect definition to load. + * @return + * An array containing the image effect definition with the following keys: + * - "effect": The unique name for the effect being performed. Usually prefixed + * with the name of the module providing the effect. + * - "module": The module providing the effect. + * - "help": A description of the effect. + * - "function": The name of the function that will execute the effect. + * - "form": (optional) The name of a function to configure the effect. + * - "summary": (optional) The name of a theme function that will display a + * one-line summary of the effect. Does not include the "theme_" prefix. + */ +function image_effect_definition_load($effect) { + return Drupal::service('plugin.manager.image.effect')->getDefinition($effect); +} + +/** * Loads a single image effect. * * @param $ieid diff --git a/core/modules/image/lib/Drupal/image/Annotation/ImageEffect.php b/core/modules/image/lib/Drupal/image/Annotation/ImageEffect.php index 15a9db8..7d2038c 100644 --- a/core/modules/image/lib/Drupal/image/Annotation/ImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Annotation/ImageEffect.php @@ -65,4 +65,10 @@ class ImageEffect extends Plugin { public $data = array(); + public $dimensions_passthrough = FALSE; + + public $summary_theme = ''; + + public $no_form = FALSE; + } diff --git a/core/modules/image/lib/Drupal/image/ImageEffectBase.php b/core/modules/image/lib/Drupal/image/ImageEffectBase.php index 8403b88..b3864e2 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectBase.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectBase.php @@ -12,36 +12,29 @@ /** * @todo. */ -class ImageEffectBase extends ContainerFactoryPluginBase implements ImageEffectInterface { +abstract class ImageEffectBase extends ContainerFactoryPluginBase implements ImageEffectInterface { /** * {@inheritdoc} */ - public function processEffect($image) { - module_load_include('inc', 'image', 'image.effects'); - $definition = $this->getPluginDefinition(); - $definition['effect_callback']($image, $this->configuration); + public function processDimensions(array &$dimensions) { + $dimensions['width'] = $dimensions['height'] = NULL; } /** * {@inheritdoc} */ - public function processDimensions(array &$dimensions) { - module_load_include('inc', 'image', 'image.effects'); - $definition = $this->getPluginDefinition(); - if (isset($definition['dimensions_callback'])) { - $definition['dimensions_callback']($dimensions, $this->configuration); - } - else { - $dimensions['width'] = $dimensions['height'] = NULL; - } + public function getSummary() { + return array( + '#markup' => '', + ); } /** * {@inheritdoc} */ - public function ignoreDimensions() { - return FALSE; + public function getForm() { + return array(); } } diff --git a/core/modules/image/lib/Drupal/image/ImageEffectInterface.php b/core/modules/image/lib/Drupal/image/ImageEffectInterface.php index 2208091..825b197 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectInterface.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectInterface.php @@ -24,13 +24,6 @@ public function processEffect($image); /** - * If the effect doesn't change the dimensions of the image. - * - * @return bool - */ - public function ignoreDimensions(); - - /** * Determines the dimensions of the styled image. * * @param array $dimensions @@ -39,4 +32,8 @@ public function ignoreDimensions(); */ public function processDimensions(array &$dimensions); + public function getSummary(); + + public function getForm(); + } diff --git a/core/modules/image/lib/Drupal/image/ImageEffectManager.php b/core/modules/image/lib/Drupal/image/ImageEffectManager.php index 67c9465..7fff8b1 100644 --- a/core/modules/image/lib/Drupal/image/ImageEffectManager.php +++ b/core/modules/image/lib/Drupal/image/ImageEffectManager.php @@ -34,12 +34,19 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac $annotation_namespaces = array('Drupal\image\Annotation' => $namespaces['Drupal\image']); parent::__construct('ImageEffect', $namespaces, $annotation_namespaces, 'Drupal\image\Annotation\ImageEffect'); - $this->alterInfo($module_handler, 'image_effect'); + $this->alterInfo($module_handler, 'image_effect_info'); $this->setCacheBackend($cache_backend, $language_manager, 'image_effect'); } /** - * {@inheritdoc} + * Returns a preconfigured image effect plugin. + * + * @param array $options + * An array of options that can be used to determine a suitable plugin to + * instantiate and how to configure it. + * + * @return \Drupal\image\ImageEffectInterface + * An image effect plugin. */ public function getInstance(array $options) { $options += array('data' => 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 7f8a536..391564a 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/CropImageEffect.php @@ -4,25 +4,81 @@ * @file * Contains \Drupal\image\Plugin\ImageEffect\CropImageEffect. */ + namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; use Drupal\image\Annotation\ImageEffect; -use Drupal\image\ImageEffectBase; /** - * @todo. + * Crops an image resource. * * @ImageEffect( * id = "image_crop", * label = @Translation("Crop"), - * help = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately."), - * effect_callback = "image_crop_effect", - * dimensions_callback = "image_resize_dimensions", - * form_callback = "image_crop_form", - * summary_theme = "image_crop_summary" + * help = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.") * ) */ -class CropImageEffect extends ImageEffectBase { +class CropImageEffect extends ResizeImageEffect { + + /** + * {@inheritdoc} + */ + public function processEffect($image) { + // Set sane default values. + $this->configuration += array( + 'anchor' => 'center-center', + ); + + list($x, $y) = explode('-', $this->configuration['anchor']); + $x = image_filter_keyword($x, $image->info['width'], $this->configuration['width']); + $y = image_filter_keyword($y, $image->info['height'], $this->configuration['height']); + if (!image_crop($image, $x, $y, $this->configuration['width'], $this->configuration['height'])) { + watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function getSummary() { + return array( + '#theme' => 'image_crop_summary', + '#data' => $this->configuration, + ); + } + + /** + * {@inheritdoc} + */ + public function getForm() { + $this->configuration += array( + 'width' => '', + 'height' => '', + 'anchor' => 'center-center', + ); + $form = parent::getForm(); + $form['anchor'] = array( + '#type' => 'radios', + '#title' => t('Anchor'), + '#options' => array( + 'left-top' => t('Top') . ' ' . t('Left'), + 'center-top' => t('Top') . ' ' . t('Center'), + 'right-top' => t('Top') . ' ' . t('Right'), + 'left-center' => t('Center') . ' ' . t('Left'), + 'center-center' => t('Center'), + 'right-center' => t('Center') . ' ' . t('Right'), + 'left-bottom' => t('Bottom') . ' ' . t('Left'), + 'center-bottom' => t('Bottom') . ' ' . t('Center'), + 'right-bottom' => t('Bottom') . ' ' . t('Right'), + ), + '#theme' => 'image_anchor', + '#default_value' => $this->configuration['anchor'], + '#description' => t('The part of the image that will be retained during the crop.'), + ); + return $form; + } } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php index 14687f3..9150032 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/DesaturateImageEffect.php @@ -4,6 +4,7 @@ * @file * Contains \Drupal\image\Plugin\ImageEffect\DesaturateImageEffect. */ + namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; @@ -11,13 +12,14 @@ use Drupal\image\ImageEffectBase; /** - * @todo. + * Desaturates (grayscale) an image resource. * * @ImageEffect( * id = "image_desaturate", * label = @Translation("Desaturate"), * help = @Translation("Desaturate converts an image to grayscale."), - * effect_callback = "image_desaturate_effect" + * no_form = TRUE, + * dimensions_passthrough = TRUE * ) */ class DesaturateImageEffect extends ImageEffectBase { @@ -25,7 +27,11 @@ class DesaturateImageEffect extends ImageEffectBase { /** * {@inheritdoc} */ - public function ignoreDimensions() { + public function processEffect($image) { + if (!image_desaturate($image)) { + watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); + return FALSE; + } return TRUE; } 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 a89f225..a40f7d3 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ResizeImageEffect.php @@ -4,6 +4,7 @@ * @file * Contains \Drupal\image\Plugin\ImageEffect\Resize. */ + namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; @@ -11,18 +12,67 @@ use Drupal\image\ImageEffectBase; /** - * @todo. + * Resizes an image resource. * * @ImageEffect( * id = "image_resize", * label = @Translation("Resize"), - * help = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately."), - * effect_callback = "image_resize_effect", - * dimensions_callback = "image_resize_dimensions", - * form_callback = "image_resize_form", - * summary_theme = "image_resize_summary" + * help = @Translation("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately.") * ) */ class ResizeImageEffect extends ImageEffectBase { + /** + * {@inheritdoc} + */ + public function processEffect($image) { + if (!image_resize($image, $this->configuration['width'], $this->configuration['height'])) { + watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function processDimensions(array &$dimensions) { + // The new image will have the exact dimensions defined for the effect. + $dimensions['width'] = $this->configuration['width']; + $dimensions['height'] = $this->configuration['height']; + } + + /** + * {@inheritdoc} + */ + public function getSummary() { + return array( + '#theme' => 'image_resize_summary', + '#data' => $this->configuration, + ); + } + + /** + * {@inheritdoc} + */ + public function getForm() { + $form['width'] = array( + '#type' => 'number', + '#title' => t('Width'), + '#default_value' => isset($this->configuration['width']) ? $this->configuration['width'] : '', + '#field_suffix' => ' ' . t('pixels'), + '#required' => TRUE, + '#min' => 1, + ); + $form['height'] = array( + '#type' => 'number', + '#title' => t('Height'), + '#default_value' => isset($this->configuration['height']) ? $this->configuration['height'] : '', + '#field_suffix' => ' ' . t('pixels'), + '#required' => TRUE, + '#min' => 1, + ); + return $form; + } + } 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 311b49b..5bf0d28 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/RotateImageEffect.php @@ -4,6 +4,7 @@ * @file * Contains \Drupal\image\Plugin\ImageEffect\RotateImageEffect. */ + namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; @@ -16,13 +17,104 @@ * @ImageEffect( * id = "image_rotate", * label = @Translation("Rotate"), - * help = @Translation("Rotating an image may cause the dimensions of an image to increase to fit the diagonal."), - * effect_callback = "image_rotate_effect", - * dimensions_callback = "image_rotate_dimensions", - * form_callback = "image_rotate_form", - * summary_theme = "image_rotate_summary" + * help = @Translation("Rotating an image may cause the dimensions of an image to increase to fit the diagonal.") * ) */ class RotateImageEffect extends ImageEffectBase { + /** + * {@inheritdoc} + */ + public function processEffect($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']; + $this->configuration['bgcolor'] = $c[0] . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3]; + } + + // Convert #FFFFFF syntax to hexadecimal colors. + if ($this->configuration['bgcolor'] != '') { + $this->configuration['bgcolor'] = hexdec(str_replace('#', '0x', $this->configuration['bgcolor'])); + } + else { + $this->configuration['bgcolor'] = NULL; + } + + if (!empty($this->configuration['random'])) { + $degrees = abs((float) $this->configuration['degrees']); + $this->configuration['degrees'] = rand(-1 * $degrees, $degrees); + } + + if (!image_rotate($image, $this->configuration['degrees'], $this->configuration['bgcolor'])) { + watchdog('image', 'Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; + } + + /** + * {@inheritdoc} + */ + public function processDimensions(array &$dimensions) { + // If the rotate is not random and the angle is a multiple of 90 degrees, + // then the new dimensions can be determined. + if (!$this->configuration['random'] && ((int) ($this->configuration['degrees']) == $this->configuration['degrees']) && ($this->configuration['degrees'] % 90 == 0)) { + if ($this->configuration['degrees'] % 180 != 0) { + $temp = $dimensions['width']; + $dimensions['width'] = $dimensions['height']; + $dimensions['height'] = $temp; + } + } + else { + $dimensions['width'] = $dimensions['height'] = NULL; + } + } + + /** + * {@inheritdoc} + */ + public function getSummary() { + return array( + '#theme' => 'image_rotate_summary', + '#data' => $this->configuration, + ); + } + + /** + * {@inheritdoc} + */ + public function getForm() { + $form['degrees'] = array( + '#type' => 'number', + '#default_value' => (isset($this->configuration['degrees'])) ? $this->configuration['degrees'] : 0, + '#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' => '°', + '#required' => TRUE, + ); + $form['bgcolor'] = array( + '#type' => 'textfield', + '#default_value' => (isset($this->configuration['bgcolor'])) ? $this->configuration['bgcolor'] : '#FFFFFF', + '#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, + '#maxlength' => 7, + '#element_validate' => array('image_effect_color_validate'), + ); + $form['random'] = array( + '#type' => 'checkbox', + '#default_value' => (isset($this->configuration['random'])) ? $this->configuration['random'] : 0, + '#title' => t('Randomize'), + '#description' => t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'), + ); + return $form; + } + } diff --git a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php index 3c85201..9ee30f0 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleAndCropImageEffect.php @@ -4,25 +4,32 @@ * @file * Contains \Drupal\image\Plugin\ImageEffect\ScaleAndCropImageEffect. */ + namespace Drupal\image\Plugin\ImageEffect; use Drupal\Core\Annotation\Translation; use Drupal\image\Annotation\ImageEffect; -use Drupal\image\ImageEffectBase; /** - * @todo. + * Scales and crops an image resource. * * @ImageEffect( * id = "image_scale_and_crop", * label = @Translation("Scale and crop"), - * help = @Translation("Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image."), - * effect_callback = "image_scale_and_crop_effect", - * dimensions_callback = "image_resize_dimensions", - * form_callback = "image_resize_form", - * summary_theme = "image_resize_summary" + * help = @Translation("Scale and crop will maintain the aspect-ratio of the original image, then crop the larger dimension. This is most useful for creating perfectly square thumbnails without stretching the image.") * ) */ -class ScaleAndCropImageEffect extends ImageEffectBase { +class ScaleAndCropImageEffect extends ResizeImageEffect { + + /** + * {@inheritdoc} + */ + public function processEffect($image) { + if (!image_scale_and_crop($image, $this->configuration['width'], $this->configuration['height'])) { + watchdog('image', 'Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR); + return FALSE; + } + return TRUE; + } } 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 ddde389..aab5ae8 100644 --- a/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php +++ b/core/modules/image/lib/Drupal/image/Plugin/ImageEffect/ScaleImageEffect.php @@ -10,20 +10,17 @@ use Drupal\Component\Image\Image; use Drupal\Core\Annotation\Translation; use Drupal\image\Annotation\ImageEffect; -use Drupal\image\ImageEffectBase; /** - * @todo. + * Scales an image resource. * * @ImageEffect( * id = "image_scale", * label = @Translation("Scale"), - * help = @Translation("Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated."), - * form_callback = "image_scale_form", - * summary_theme = "image_scale_summary" + * help = @Translation("Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.") * ) */ -class ScaleImageEffect extends ImageEffectBase { +class ScaleImageEffect extends ResizeImageEffect { /** * {@inheritdoc} @@ -52,4 +49,31 @@ public function processDimensions(array &$dimensions) { } } + /** + * {@inheritdoc} + */ + public function getSummary() { + return array( + '#theme' => 'image_scale_summary', + '#data' => $this->configuration, + ); + } + + /** + * {@inheritdoc} + */ + public function getForm() { + $form = parent::getForm(); + $form['#element_validate'] = array('image_effect_scale_validate'); + $form['width']['#required'] = FALSE; + $form['height']['#required'] = FALSE; + $form['upscale'] = array( + '#type' => 'checkbox', + '#default_value' => (isset($this->configuration['upscale'])) ? $this->configuration['upscale'] : 0, + '#title' => t('Allow Upscaling'), + '#description' => t('Let scale make images larger than their original size'), + ); + return $form; + } + } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php index 51b64da..1f842a1 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php @@ -252,7 +252,7 @@ function testStyleReplacement() { // Create a new style. $style_name = strtolower($this->randomName(10)); $style_label = $this->randomString(); - $style = entity_create('image_style', array('id' => $style_name, 'label' => $style_label)); + $style = entity_create('image_style', array('name' => $style_name, 'label' => $style_label)); $style->save(); $style_path = 'admin/config/media/image-styles/manage/'; diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php index e7552d2..802690d 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php @@ -59,7 +59,7 @@ function testImageDimensions() { // Scale an image that is wider than it is high. $effect = array( - 'name' => 'image_scale', + 'id' => 'image_scale', 'data' => array( 'width' => 120, 'height' => 90, @@ -81,7 +81,7 @@ function testImageDimensions() { // Rotate 90 degrees anticlockwise. $effect = array( - 'name' => 'image_rotate', + 'id' => 'image_rotate', 'data' => array( 'degrees' => -90, 'random' => FALSE, @@ -102,7 +102,7 @@ function testImageDimensions() { // Scale an image that is higher than it is wide (rotated by previous effect). $effect = array( - 'name' => 'image_scale', + 'id' => 'image_scale', 'data' => array( 'width' => 120, 'height' => 90, @@ -124,7 +124,7 @@ function testImageDimensions() { // Test upscale disabled. $effect = array( - 'name' => 'image_scale', + 'id' => 'image_scale', 'data' => array( 'width' => 400, 'height' => 200, @@ -146,7 +146,7 @@ function testImageDimensions() { // Add a desaturate effect. $effect = array( - 'name' => 'image_desaturate', + 'id' => 'image_desaturate', 'data' => array(), 'weight' => 4, ); @@ -164,7 +164,7 @@ function testImageDimensions() { // Add a random rotate effect. $effect = array( - 'name' => 'image_rotate', + 'id' => 'image_rotate', 'data' => array( 'degrees' => 180, 'random' => TRUE, @@ -183,7 +183,7 @@ function testImageDimensions() { // Add a crop effect. $effect = array( - 'name' => 'image_crop', + 'id' => 'image_crop', 'data' => array( 'width' => 30, 'height' => 30, @@ -205,7 +205,7 @@ function testImageDimensions() { // Rotate to a non-multiple of 90 degrees. $effect = array( - 'name' => 'image_rotate', + 'id' => 'image_rotate', 'data' => array( 'degrees' => 57, 'random' => FALSE, @@ -226,7 +226,7 @@ function testImageDimensions() { // Ensure that an effect with no dimensions callback unsets the dimensions. // This ensures compatibility with 7.0 contrib modules. $effect = array( - 'name' => 'image_module_test_null', + 'id' => 'image_module_test_null', 'data' => array(), 'weight' => 8, ); diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php index a237136..4ec5fb5 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageEffectsTest.php @@ -22,6 +22,13 @@ class ImageEffectsTest extends ToolkitTestBase { */ public static $modules = array('image', 'image_test', 'image_module_test'); + /** + * The image effect manager. + * + * @var \Drupal\image\ImageEffectManager + */ + protected $manager; + public static function getInfo() { return array( 'name' => 'Image effects', @@ -30,17 +37,19 @@ public static function getInfo() { ); } - function setUp() { + public function setUp() { parent::setUp(); - - module_load_include('inc', 'image', 'image.effects'); + $this->manager = $this->container->get('plugin.manager.image.effect'); } /** * Test the image_resize_effect() function. */ function testResizeEffect() { - $this->assertTrue(image_resize_effect($this->image, array('width' => 1, 'height' => 2)), 'Function returned the expected value.'); + $this->assertImageEffect('image_resize', array( + 'width' => 1, + 'height' => 2, + )); $this->assertToolkitOperationsCalled(array('resize')); // Check the parameters. @@ -54,7 +63,10 @@ function testResizeEffect() { */ function testScaleEffect() { // @todo: need to test upscaling. - $this->assertTrue(image_scale_effect($this->image, array('width' => 10, 'height' => 10)), 'Function returned the expected value.'); + $this->assertImageEffect('image_scale', array( + 'width' => 10, + 'height' => 10, + )); $this->assertToolkitOperationsCalled(array('resize')); // Check the parameters. @@ -68,7 +80,11 @@ function testScaleEffect() { */ function testCropEffect() { // @todo should test the keyword offsets. - $this->assertTrue(image_crop_effect($this->image, array('anchor' => 'top-1', 'width' => 3, 'height' => 4)), 'Function returned the expected value.'); + $this->assertImageEffect('image_crop', array( + 'anchor' => 'top-1', + 'width' => 3, + 'height' => 4, + )); $this->assertToolkitOperationsCalled(array('crop')); // Check the parameters. @@ -83,7 +99,10 @@ function testCropEffect() { * Test the image_scale_and_crop_effect() function. */ function testScaleAndCropEffect() { - $this->assertTrue(image_scale_and_crop_effect($this->image, array('width' => 5, 'height' => 10)), 'Function returned the expected value.'); + $this->assertImageEffect('image_scale_and_crop', array( + 'width' => 5, + 'height' => 10, + )); $this->assertToolkitOperationsCalled(array('resize', 'crop')); // Check the parameters. @@ -98,7 +117,7 @@ function testScaleAndCropEffect() { * Test the image_desaturate_effect() function. */ function testDesaturateEffect() { - $this->assertTrue(image_desaturate_effect($this->image, array()), 'Function returned the expected value.'); + $this->assertImageEffect('image_desaturate', array()); $this->assertToolkitOperationsCalled(array('desaturate')); // Check the parameters. @@ -111,7 +130,10 @@ function testDesaturateEffect() { */ function testRotateEffect() { // @todo: need to test with 'random' => TRUE - $this->assertTrue(image_rotate_effect($this->image, array('degrees' => 90, 'bgcolor' => '#fff')), 'Function returned the expected value.'); + $this->assertImageEffect('image_rotate', array( + 'degrees' => 90, + 'bgcolor' => '#fff', + )); $this->assertToolkitOperationsCalled(array('rotate')); // Check the parameters. @@ -132,11 +154,30 @@ function testImageEffectsCaching() { $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.'); // Second call should come from cache. - drupal_static_reset('image_effect_definitions'); drupal_static_reset('image_module_test_image_effect_info_alter'); $cached_effects = $manager->getDefinitions(); $this->assertTrue(is_null($image_effect_definitions_called), 'image_effect_definitions() returned data from cache.'); $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.'); } + + /** + * Asserts the effect processing of an image effect plugin. + * + * @param string $effect_name + * The name of the image effect to test. + * @param array $data + * The data to pass to the image effect. + * + * @return bool + * TRUE if the assertion succeeded, FALSE otherwise. + */ + protected function assertImageEffect($effect_name, array $data) { + $effect = $this->manager->getInstance(array( + 'id' => $effect_name, + 'data' => $data, + )); + return $this->assertTrue($effect->processEffect($this->image), 'Function returned the expected value.'); + } + } diff --git a/core/modules/image/tests/modules/image_module_test/image_module_test.module b/core/modules/image/tests/modules/image_module_test/image_module_test.module index a8f8aa3..7d64d2c 100644 --- a/core/modules/image/tests/modules/image_module_test/image_module_test.module +++ b/core/modules/image/tests/modules/image_module_test/image_module_test.module @@ -13,25 +13,9 @@ function image_module_test_file_download($uri) { } /** - * Image effect callback; Null. - * - * @param $image - * An image object returned by image_load(). - * @param $data - * An array with no attributes. - * - * @return - * TRUE - */ -function image_module_test_null_effect(array &$image, array $data) { - return TRUE; -} - -/** * Implements hook_image_effect_info_alter(). * - * Used to keep a count of cache misses in image_effect_definitions(). - * + * Used to keep a count of cache misses in \Drupal\image\ImageEffectManager. */ function image_module_test_image_effect_info_alter(&$effects) { $image_effects_definition_called = &drupal_static(__FUNCTION__, 0); diff --git a/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php b/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php index 942a202..2550f56 100644 --- a/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php +++ b/core/modules/image/tests/modules/image_module_test/lib/Drupal/image_module_test/Plugin/ImageEffect/NullTestImageEffect.php @@ -16,9 +16,16 @@ * @ImageEffect( * id = "image_module_test_null", * label = @Translation("Image module test"), - * effect_callback = "image_module_test_null_effect" + * no_form = TRUE * ) */ class NullTestImageEffect extends ImageEffectBase { + /** + * {@inheritdoc} + */ + public function processEffect($image) { + return TRUE; + } + }