Commit 4365f507 authored by catch's avatar catch

Issue #1782244 by heyrocker, andypost, tstoeckler: Convert image styles array...

Issue #1782244 by heyrocker, andypost, tstoeckler: Convert image styles  array into ImageStyle (extends ConfigEntity).
parent 856e8f75
This diff is collapsed.
......@@ -65,43 +65,6 @@ function hook_image_effect_info_alter(&$effects) {
$effects['image_crop']['form callback'] = 'mymodule_crop_form';
}
/**
* Respond to image style updating.
*
* This hook enables modules to update settings that might be affected by
* changes to an image. For example, updating a module specific variable to
* reflect a change in the image style's name.
*
* @param $style
* The image style array that is being updated.
*/
function hook_image_style_save($style) {
// If a module defines an image style and that style is renamed by the user
// the module should update any references to that style.
if (isset($style['old_name']) && $style['old_name'] == variable_get('mymodule_image_style', '')) {
variable_set('mymodule_image_style', $style['name']);
}
}
/**
* Respond to image style deletion.
*
* This hook enables modules to update settings when a image style is being
* deleted. If a style is deleted, a replacement name may be specified in
* $style['name'] and the style being deleted will be specified in
* $style['old_name'].
*
* @param $style
* The image style array that being deleted.
*/
function hook_image_style_delete($style) {
// Administrators can choose an optional replacement style when deleting.
// Update the modules style variable accordingly.
if (isset($style['old_name']) && $style['old_name'] == variable_get('mymodule_image_style', '')) {
variable_set('mymodule_image_style', $style['name']);
}
}
/**
* Respond to image style flushing.
*
......@@ -111,7 +74,7 @@ function hook_image_style_delete($style) {
* be cleared using this hook. This hook is called whenever a style is updated,
* deleted, or any effect associated with the style is update or deleted.
*
* @param $style
* @param Drupal\image\ImageStyle $style
* The image style array that is being flushed.
*/
function hook_image_style_flush($style) {
......@@ -119,34 +82,6 @@ function hook_image_style_flush($style) {
cache('mymodule')->flush();
}
/**
* Modify any image styles provided by other modules or the user.
*
* This hook allows modules to modify, add, or remove image styles. This may
* be useful to modify default styles provided by other modules or enforce
* that a specific effect is always enabled on a style. Note that modifications
* to these styles may negatively affect the user experience, such as if an
* effect is added to a style through this hook, the user may attempt to remove
* the effect but it will be immediately be re-added.
*
* The best use of this hook is usually to modify default styles, which are not
* editable by the user until they are overridden, so such interface
* contradictions will not occur. This hook can target default (or user) styles
* by checking the $style['storage'] property.
*/
function hook_image_styles_alter(&$styles) {
// Check that we only affect a default style.
if ($styles['thumbnail']['storage'] == IMAGE_STORAGE_DEFAULT) {
// Add an additional effect to the thumbnail style.
$styles['thumbnail']['effects'][] = array(
'name' => 'image_desaturate',
'data' => array(),
'weight' => 1,
'effect callback' => 'image_desaturate_effect',
);
}
}
/**
* @} End of "addtogroup hooks".
*/
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\image\ImageStyle.
*/
namespace Drupal\image;
use Drupal\Core\Config\Entity\ConfigEntityBase;
/**
* Defines an image style configuration entity.
*/
class ImageStyle extends ConfigEntityBase {
/**
* The name of the image style.
*
* @var string
*/
public $name;
/**
* The image style label.
*
* @var string
*/
public $label;
/**
* The array of image effects for this image style.
*
* @var string
*/
public $effects;
/**
* Overrides Drupal\Core\Entity\Entity::id().
*/
public function id() {
return $this->name;
}
}
......@@ -34,14 +34,14 @@ function createSampleImage($style) {
$file_path = file_unmanaged_copy($file->uri);
}
return image_style_url($style['name'], $file_path) ? $file_path : FALSE;
return image_style_url($style->id(), $file_path) ? $file_path : FALSE;
}
/**
* Count the number of images currently create for a style.
*/
function getImageCount($style) {
return count(file_scan_directory('public://styles/' . $style['name'], '/.*/'));
return count(file_scan_directory('public://styles/' . $style->id(), '/.*/'));
}
/**
......@@ -121,10 +121,9 @@ function testStyle() {
// Edit effect form.
// Revisit each form to make sure the effect was saved.
drupal_static_reset('image_styles');
$style = image_style_load($style_name);
$style = entity_load('image_style', $style_name);
foreach ($style['effects'] as $ieid => $effect) {
foreach ($style->effects as $ieid => $effect) {
$this->drupalGet($style_path . '/effects/' . $ieid);
foreach ($effect_edits[$effect['name']] as $field => $value) {
$this->assertFieldByName($field, $value, format_string('The %field field in the %effect effect has the correct value of %value.', array('%field' => $field, '%effect' => $effect['name'], '%value' => $value)));
......@@ -136,7 +135,7 @@ function testStyle() {
// Confirm the order of effects is maintained according to the order we
// added the fields.
$effect_edits_order = array_keys($effect_edits);
$effects_order = array_values($style['effects']);
$effects_order = array_values($style->effects);
$order_correct = TRUE;
foreach ($effects_order as $index => $effect) {
if ($effect_edits_order[$index] != $effect['name']) {
......@@ -154,14 +153,14 @@ function testStyle() {
'name' => $style_name,
'label' => $style_label,
);
foreach ($style['effects'] as $ieid => $effect) {
foreach ($style->effects as $ieid => $effect) {
$edit['effects[' . $ieid . '][weight]'] = $weight;
$weight--;
}
// Create an image to make sure it gets flushed after saving.
$image_path = $this->createSampleImage($style);
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path)));
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style->label(), '%file' => $image_path)));
$this->drupalPost($style_path, $edit, t('Update style'));
......@@ -170,20 +169,19 @@ function testStyle() {
// Check that the URL was updated.
$this->drupalGet($style_path);
$this->assertResponse(200, format_string('Image style %original renamed to %new', array('%original' => $style['label'], '%new' => $style_name)));
$this->assertResponse(200, format_string('Image style %original renamed to %new', array('%original' => $style->label(), '%new' => $style_name)));
// Check that the image was flushed after updating the style.
// This is especially important when renaming the style. Make sure that
// the old image directory has been deleted.
$this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style['label'])));
$this->assertEqual($this->getImageCount($style), 0, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style->label())));
// Load the style by the new name with the new weights.
drupal_static_reset('image_styles');
$style = image_style_load($style_name);
$style = entity_load('image_style', $style_name);
// Confirm the new style order was saved.
$effect_edits_order = array_reverse($effect_edits_order);
$effects_order = array_values($style['effects']);
$effects_order = array_values($style->effects);
$order_correct = TRUE;
foreach ($effects_order as $index => $effect) {
if ($effect_edits_order[$index] != $effect['name']) {
......@@ -196,10 +194,10 @@ function testStyle() {
// Create an image to make sure it gets flushed after deleting an effect.
$image_path = $this->createSampleImage($style);
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path)));
$this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style->label(), '%file' => $image_path)));
// Test effect deletion form.
$effect = array_pop($style['effects']);
$effect = array_pop($style->effects);
$this->drupalPost($style_path . '/effects/' . $effect['ieid'] . '/delete', array(), t('Delete'));
$this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $effect['label'])));
......@@ -210,10 +208,9 @@ function testStyle() {
// Confirm the style directory has been removed.
$directory = file_default_scheme() . '://styles/' . $style_name;
$this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', array('%style' => $style['label'])));
$this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', array('%style' => $style->label())));
drupal_static_reset('image_styles');
$this->assertFalse(image_style_load($style_name), format_string('Image style %style successfully deleted.', array('%style' => $style['label'])));
$this->assertFalse(entity_load('image_style', $style_name), format_string('Image style %style successfully deleted.', array('%style' => $style->label())));
}
......@@ -224,7 +221,8 @@ function testStyleReplacement() {
// Create a new style.
$style_name = strtolower($this->randomName(10));
$style_label = $this->randomString();
image_style_save(array('name' => $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/edit/' . $style_name;
// Create an image field that uses the new style.
......
......@@ -41,7 +41,8 @@ function testImageDimensions() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
$style = image_style_save(array('name' => 'test', 'label' => 'Test'));
$style = entity_create('image_style', array('name' => 'test', 'label' => 'Test'));
$style->save();
$generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
$url = image_style_url('test', $original_uri);
......@@ -67,7 +68,7 @@ function testImageDimensions() {
'weight' => 0,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="120" height="60" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -88,7 +89,7 @@ function testImageDimensions() {
'weight' => 1,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="60" height="120" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -110,7 +111,7 @@ function testImageDimensions() {
'weight' => 2,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -132,7 +133,7 @@ function testImageDimensions() {
'weight' => 3,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -150,7 +151,7 @@ function testImageDimensions() {
'weight' => 4,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -171,7 +172,7 @@ function testImageDimensions() {
'weight' => 5,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -191,7 +192,7 @@ function testImageDimensions() {
'weight' => 6,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="30" height="30" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -212,7 +213,7 @@ function testImageDimensions() {
'weight' => 7,
);
$effect = image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
......@@ -220,7 +221,7 @@ function testImageDimensions() {
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
image_effect_delete('test', $effect);
image_effect_delete($style, $effect);
// Ensure that an effect with no dimensions callback unsets the dimensions.
// This ensures compatibility with 7.0 contrib modules.
......@@ -230,7 +231,7 @@ function testImageDimensions() {
'weight' => 8,
);
image_effect_save('test', $effect);
image_effect_save($style, $effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />');
}
......
......@@ -17,8 +17,6 @@
* image_style_create_derivative()
*
* image.module:
* image_style_load()
* image_style_save()
* image_style_delete()
* image_style_options()
* image_style_flush()
......
......@@ -37,7 +37,8 @@ function setUp() {
parent::setUp();
$this->style_name = 'style_foo';
image_style_save(array('name' => $this->style_name, 'label' => $this->randomString()));
$style = entity_create('image_style', array('name' => $this->style_name, 'label' => $this->randomString()));
$style->save();
}
/**
......
......@@ -39,7 +39,8 @@ function testImageFormatterTheme() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
image_style_save(array('name' => 'test', 'label' => 'Test'));
$style = entity_create('image_style', array('name' => 'test', 'label' => 'Test'));
$style->save();
$url = image_style_url('test', $original_uri);
// Test using theme_image_formatter() without an image title, alt text, or
......@@ -81,7 +82,8 @@ function testImageStyleTheme() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
image_style_save(array('name' => 'image_test', 'label' => 'Test'));
$style = entity_create('image_style', array('name' => 'image_test', 'label' => 'Test'));
$style->save();
$url = image_style_url('image_test', $original_uri);
$path = $this->randomName();
......
......@@ -31,8 +31,9 @@ function testNormal() {
$file = entity_create('file', (array) current($this->drupalGetTestFiles('image')));
// Create derivative image.
$style = image_style_load(key(image_styles()));
$derivative_uri = image_style_path($style['name'], $file->uri);
$styles = entity_load_multiple('image_style');
$style = image_style_load(key($styles));
$derivative_uri = image_style_path($style->id(), $file->uri);
image_style_create_derivative($style, $file->uri, $derivative_uri);
// Check if derivative image exists.
......
......@@ -2920,8 +2920,8 @@ function user_image_style_delete($style) {
*/
function user_image_style_save($style) {
// If a style is renamed, update the variables that use it.
if (isset($style['old_name']) && $style['old_name'] == variable_get('user_picture_style', '')) {
variable_set('user_picture_style', $style['name']);
if ($style->id() != $style->getOriginalID()) {
variable_set('user_picture_style', $style->id());
}
}
......
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