Commit 3c66dae8 authored by webchick's avatar webchick

Issue #606598 by andypost, stBorchert, sun, aspilicious, joachim: Added Human...

Issue #606598 by andypost, stBorchert, sun, aspilicious, joachim: Added Human readable image-style names.
parent 35a37620
name: large
label: Large (480x480)
effects:
ddd73aa7-4bd6-4c85-b600-bdf2b1628d1d:
name: image_scale
......
name: medium
label: Medium (220x220)
effects:
bddf0d06-42f9-4c75-a700-a33cafa25ea0:
name: image_scale
......
name: thumbnail
label: Thumbnail (100x100)
effects:
1cfec298-8620-4749-b100-ccb6c4500779:
name: image_scale
......
......@@ -32,10 +32,9 @@ function image_style_list() {
* An image style array.
* @ingroup forms
* @see image_style_form_submit()
* @see image_style_name_validate()
*/
function image_style_form($form, &$form_state, $style) {
$title = t('Edit %name style', array('%name' => $style['name']));
$title = t('Edit style %name', array('%name' => $style['label']));
drupal_set_title($title, PASS_THROUGH);
$form_state['image_style'] = $style;
......@@ -49,13 +48,19 @@ function image_style_form($form, &$form_state, $style) {
'#markup' => theme('image_style_preview', array('style' => $style)),
);
$form['name'] = array(
$form['label'] = array(
'#type' => 'textfield',
'#size' => '64',
'#title' => t('Image style name'),
'#title' => t('Administrative label'),
'#default_value' => $style['label'],
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => $style['name'],
'#description' => t('The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
'#element_validate' => array('image_style_name_validate'),
'#machine_name' => array(
'exists' => 'image_style_load',
'source' => array('label'),
),
'#required' => TRUE,
);
......@@ -199,6 +204,7 @@ function image_style_form_submit($form, &$form_state) {
$old_style = $style;
$style['name'] = $form_state['values']['name'];
}
$style['label'] = $form_state['values']['label'];
image_style_save($style);
if (isset($old_style)) {
image_style_delete($old_style, $style['name']);
......@@ -215,16 +221,21 @@ function image_style_form_submit($form, &$form_state) {
*
* @ingroup forms
* @see image_style_add_form_submit()
* @see image_style_name_validate()
*/
function image_style_add_form($form, &$form_state) {
$form['name'] = array(
$form['label'] = array(
'#type' => 'textfield',
'#size' => '64',
'#title' => t('Style name'),
'#title' => t('Administrative label'),
'#default_value' => '',
'#required' => TRUE,
);
$form['name'] = array(
'#type' => 'machine_name',
'#machine_name' => array(
'exists' => 'image_style_load',
'source' => array('label'),
),
'#default_value' => '',
'#description' => t('The name is used in URLs for generated images. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
'#element_validate' => array('image_style_name_validate'),
'#required' => TRUE,
);
......@@ -240,28 +251,15 @@ function image_style_add_form($form, &$form_state) {
* Submit handler for adding a new image style.
*/
function image_style_add_form_submit($form, &$form_state) {
$style = array('name' => $form_state['values']['name']);
$style = array(
'name' => $form_state['values']['name'],
'label' => $form_state['values']['label'],
);
$style = image_style_save($style);
drupal_set_message(t('Style %name was created.', array('%name' => $style['name'])));
drupal_set_message(t('Style %name was created.', array('%name' => $style['label'])));
$form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name'];
}
/**
* Element validate function to ensure unique, URL safe style names.
*/
function image_style_name_validate($element, $form_state) {
// Check for duplicates.
$styles = image_styles();
if (isset($styles[$element['#value']]) && (!isset($form_state['image_style']['name']) || $styles[$element['#value']]['name'] != $form_state['image_style']['name'])) {
form_set_error($element['#name'], t('The image style name %name is already in use.', array('%name' => $element['#value'])));
}
// Check for illegal characters in image style names.
if (preg_match('/[^0-9a-z_\-]/', $element['#value'])) {
form_set_error($element['#name'], t('Please only use lowercase alphanumeric characters, underscores (_), and hyphens (-) for style names.'));
}
}
/**
* Form builder; Form for deleting an image style.
*
......@@ -284,7 +282,7 @@ function image_style_delete_form($form, &$form_state, $style) {
return confirm_form(
$form,
t('Optionally select a style before deleting %style', array('%style' => $style['name'])),
t('Optionally select a style before deleting %style', array('%style' => $style['label'])),
'admin/config/media/image-styles',
t('If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted.'),
t('Delete'), t('Cancel')
......@@ -298,7 +296,7 @@ function image_style_delete_form_submit($form, &$form_state) {
$style = $form_state['image_style'];
image_style_delete($style, $form_state['values']['replacement']);
drupal_set_message(t('Style %name was deleted.', array('%name' => $style['name'])));
drupal_set_message(t('Style %name was deleted.', array('%name' => $style['label'])));
$form_state['redirect'] = 'admin/config/media/image-styles';
}
......@@ -403,7 +401,7 @@ function image_effect_delete_form($form, &$form_state, $style, $effect) {
$form_state['image_style'] = $style;
$form_state['image_effect'] = $effect;
$question = t('Are you sure you want to delete the @effect effect from the %style style?', array('%style' => $style['name'], '@effect' => $effect['label']));
$question = t('Are you sure you want to delete the @effect effect from the %style style?', array('%style' => $style['label'], '@effect' => $effect['label']));
return confirm_form($form, $question, 'admin/config/media/image-styles/edit/' . $style['name'], '', t('Delete'));
}
......@@ -595,7 +593,7 @@ function theme_image_style_list($variables) {
foreach ($styles as $style) {
$row = array();
$row[] = l($style['name'], 'admin/config/media/image-styles/edit/' . $style['name']);
$row[] = l($style['label'], 'admin/config/media/image-styles/edit/' . $style['name']);
$row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
$row[] = l(t('delete'), 'admin/config/media/image-styles/delete/' . $style['name'], $link_attributes);
$rows[] = $row;
......@@ -729,7 +727,7 @@ function theme_image_style_preview($variables) {
// Build the preview of the image style.
$preview_url = file_create_url($preview_file) . '?cache_bypass=' . REQUEST_TIME;
$output .= '<div class="preview-image-wrapper">';
$output .= check_plain($style['name']) . ' (' . l(t('view actual size'), file_create_url($preview_file) . '?' . time()) . ')';
$output .= check_plain($style['label']) . ' (' . l(t('view actual size'), file_create_url($preview_file) . '?' . time()) . ')';
$output .= '<div class="preview-image modified-image" style="' . $preview_attributes['style'] . '">';
$output .= '<a href="' . file_create_url($preview_file) . '?' . time() . '">' . theme('image', array('uri' => $preview_url, 'alt' => t('Sample modified image'), 'title' => '', 'attributes' => $preview_attributes)) . '</a>';
$output .= '<div class="height" style="height: ' . $preview_height . 'px"><span>' . $preview_image['height'] . 'px</span></div>';
......
......@@ -628,7 +628,9 @@ function image_style_save($style) {
$config = config('image.style.' . $style['name']);
$is_new = $config->isNew();
$config->set('name', $style['name']);
$config
->set('name', $style['name'])
->set('label', $style['label']);
if (isset($style['effects'])) {
$config->set('effects', $style['effects']);
}
......@@ -702,10 +704,10 @@ function image_style_options($include_empty = TRUE) {
if ($include_empty && !empty($styles)) {
$options[''] = t('<none>');
}
// Use the array concatenation operator '+' here instead of array_merge(),
// because the latter loses the datatype of the array keys, turning
// associative string keys into numeric ones without warning.
$options = $options + drupal_map_assoc(array_keys($styles));
foreach ($styles as $name => $style) {
$options[$name] = $style['label'];
}
if (empty($options)) {
$options[''] = t('No defined styles');
}
......
......@@ -50,13 +50,15 @@ function getImageCount($style) {
*/
function testNumericStyleName() {
$style_name = rand();
$style_label = $this->randomString();
$edit = array(
'name' => $style_name,
'label' => $style_label,
);
$this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
$this->assertRaw(t('Style %name was created.', array('%name' => $style_name)), t('Image style successfully created.'));
$this->assertRaw(t('Style %name was created.', array('%name' => $style_label)));
$options = image_style_options();
$this->assertTrue(array_key_exists($style_name, $options), t('Array key %key exists.', array('%key' => $style_name)));
$this->assertTrue(array_key_exists($style_name, $options), format_string('Array key %key exists.', array('%key' => $style_name)));
}
/**
......@@ -65,6 +67,7 @@ function testNumericStyleName() {
function testStyle() {
// Setup a style to be created and effects to add to it.
$style_name = strtolower($this->randomName(10));
$style_label = $this->randomString();
$style_path = 'admin/config/media/image-styles/edit/' . $style_name;
$effect_edits = array(
'image_resize' => array(
......@@ -99,9 +102,10 @@ function testStyle() {
$edit = array(
'name' => $style_name,
'label' => $style_label,
);
$this->drupalPost('admin/config/media/image-styles/add', $edit, t('Create new style'));
$this->assertRaw(t('Style %name was created.', array('%name' => $style_name)), t('Image style successfully created.'));
$this->assertRaw(t('Style %name was created.', array('%name' => $style_label)));
// Add effect form.
......@@ -123,7 +127,7 @@ function testStyle() {
foreach ($style['effects'] as $ieid => $effect) {
$this->drupalGet($style_path . '/effects/' . $ieid);
foreach ($effect_edits[$effect['name']] as $field => $value) {
$this->assertFieldByName($field, $value, t('The %field field in the %effect effect has the correct value of %value.', array('%field' => $field, '%effect' => $effect['name'], '%value' => $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)));
}
}
......@@ -139,14 +143,16 @@ function testStyle() {
$order_correct = FALSE;
}
}
$this->assertTrue($order_correct, t('The order of the effects is correctly set by default.'));
$this->assertTrue($order_correct, 'The order of the effects is correctly set by default.');
// Test the style overview form.
// Change the name of the style and adjust the weights of effects.
$style_name = strtolower($this->randomName(10));
$style_label = $this->randomString();
$weight = count($effect_edits);
$edit = array(
'name' => $style_name,
'label' => $style_label,
);
foreach ($style['effects'] as $ieid => $effect) {
$edit['effects[' . $ieid . '][weight]'] = $weight;
......@@ -155,7 +161,7 @@ function testStyle() {
// Create an image to make sure it gets flushed after saving.
$image_path = $this->createSampleImage($style);
$this->assertEqual($this->getImageCount($style), 1, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%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'));
......@@ -164,12 +170,12 @@ function testStyle() {
// Check that the URL was updated.
$this->drupalGet($style_path);
$this->assertResponse(200, t('Image style %original renamed to %new', array('%original' => $style['name'], '%new' => $style_name)));
$this->assertResponse(200, t('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, t('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style['name'])));
$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');
......@@ -190,12 +196,12 @@ 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, t('Image style %style image %file successfully generated.', array('%style' => $style['name'], '%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']);
$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'])), t('Image effect deleted.'));
$this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $effect['label'])));
// Style deletion form.
......@@ -204,10 +210,10 @@ function testStyle() {
// Confirm the style directory has been removed.
$directory = file_default_scheme() . '://styles/' . $style_name;
$this->assertFalse(is_dir($directory), t('Image style %style directory removed on style deletion.', array('%style' => $style['name'])));
$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), t('Image style %style successfully deleted.', array('%style' => $style['name'])));
$this->assertFalse(image_style_load($style_name), format_string('Image style %style successfully deleted.', array('%style' => $style['label'])));
}
......@@ -217,7 +223,8 @@ function testStyle() {
function testStyleReplacement() {
// Create a new style.
$style_name = strtolower($this->randomName(10));
image_style_save(array('name' => $style_name));
$style_label = $this->randomString();
image_style_save(array('name' => $style_name, 'label' => $style_label));
$style_path = 'admin/config/media/image-styles/edit/' . $style_name;
// Create an image field that uses the new style.
......@@ -239,8 +246,10 @@ function testStyleReplacement() {
// Rename the style and make sure the image field is updated.
$new_style_name = strtolower($this->randomName(10));
$new_style_label = $this->randomString();
$edit = array(
'name' => $new_style_name,
'label' => $new_style_label,
);
$this->drupalPost('admin/config/media/image-styles/edit/' . $style_name, $edit, t('Update style'));
$this->assertText(t('Changes to the style have been saved.'), t('Style %name was renamed to %new_name.', array('%name' => $style_name, '%new_name' => $new_style_name)));
......@@ -252,8 +261,8 @@ function testStyleReplacement() {
'replacement' => 'thumbnail',
);
$this->drupalPost('admin/config/media/image-styles/delete/' . $new_style_name, $edit, t('Delete'));
$message = t('Style %name was deleted.', array('%name' => $new_style_name));
$this->assertRaw($message, $message);
$message = t('Style %name was deleted.', array('%name' => $new_style_label));
$this->assertRaw($message);
$this->drupalGet('node/' . $nid);
$this->assertRaw(image_style_url('thumbnail', file_load($node->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['fid'])->uri), t('Image displayed using style replacement style.'));
......@@ -265,7 +274,7 @@ function testStyleReplacement() {
function testEditEffect() {
// Add a scale effect.
$this->drupalGet('admin/config/media/image-styles/add');
$this->drupalPost(NULL, array('name' => 'test_style_effect_edit'), t('Create new style'));
$this->drupalPost(NULL, array('label' => 'Test style effect edit', 'name' => 'test_style_effect_edit'), t('Create new style'));
$this->drupalPost(NULL, array('new' => 'image_scale_and_crop'), t('Add'));
$this->drupalPost(NULL, array('data[width]' => '300', 'data[height]' => '200'), t('Add effect'));
$this->assertText(t('Scale and crop 300x200'));
......@@ -280,7 +289,7 @@ function testEditEffect() {
// Add another scale effect.
$this->drupalGet('admin/config/media/image-styles/add');
$this->drupalPost(NULL, array('name' => 'test_style_scale_edit_scale'), t('Create new style'));
$this->drupalPost(NULL, array('label' => 'Test style scale edit scale', 'name' => 'test_style_scale_edit_scale'), t('Create new style'));
$this->drupalPost(NULL, array('new' => 'image_scale'), t('Add'));
$this->drupalPost(NULL, array('data[width]' => '12', 'data[height]' => '19'), t('Add effect'));
......
......@@ -41,7 +41,7 @@ function testImageDimensions() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
$style = image_style_save(array('name' => 'test'));
$style = image_style_save(array('name' => 'test', 'label' => 'Test'));
$generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
$url = image_style_url('test', $original_uri);
......
......@@ -37,7 +37,7 @@ function setUp() {
parent::setUp();
$this->style_name = 'style_foo';
image_style_save(array('name' => $this->style_name));
image_style_save(array('name' => $this->style_name, 'label' => $this->randomString()));
}
/**
......
......@@ -39,7 +39,7 @@ function testImageFormatterTheme() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
image_style_save(array('name' => 'test'));
image_style_save(array('name' => 'test', 'label' => 'Test'));
$url = image_style_url('test', $original_uri);
// Test using theme_image_formatter() without an image title, alt text, or
......@@ -81,7 +81,7 @@ function testImageStyleTheme() {
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
// Create a style.
image_style_save(array('name' => 'image_test'));
image_style_save(array('name' => 'image_test', 'label' => 'Test'));
$url = image_style_url('image_test', $original_uri);
$path = $this->randomName();
......
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