diff --git a/modules/image/image.admin.inc b/modules/image/image.admin.inc index 9f0fab2..0f3d939 100644 --- a/modules/image/image.admin.inc +++ b/modules/image/image.admin.inc @@ -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 %name style', array('%name' => $style['label'])); drupal_set_title($title, PASS_THROUGH); // Adjust this form for styles that must be overridden to edit. @@ -56,27 +55,30 @@ function image_style_form($form, &$form_state, $style) { '#markup' => theme('image_style_preview', array('style' => $style)), ); + // Show the Image Style label. + $form['label'] = array( + '#type' => 'textfield', + '#title' => t('Image style name'), + '#default_value' => $style['label'], + '#required' => TRUE, + ); + // Allow the name of the style to be changed, unless this style is // provided by a module's hook_default_image_styles(). - if ($style['storage'] & IMAGE_STORAGE_MODULE) { - $form['name'] = array( - '#type' => 'item', - '#title' => t('Image style name'), - '#markup' => $style['name'], - '#description' => t('This image style is being provided by %module module and may not be renamed.', array('%module' => $style['module'])), - ); - } - else { - $form['name'] = array( - '#type' => 'textfield', - '#size' => '64', - '#title' => t('Image style 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'), - '#required' => TRUE, - ); - } + $form['name'] = array( + '#type' => 'machine_name', + '#size' => '64', + '#default_value' => $style['name'], + '#disabled' => $style['storage'] & IMAGE_STORAGE_MODULE, + '#description' => t('The name is used in URLs for generated images. Use only lowercase letters, numbers, underscores and hyphens.'), + '#required' => TRUE, + '#machine_name' => array( + 'exists' => 'image_style_load', + 'source' => array('label'), + 'replace_pattern' => '[^0-9a-z_\-]', + 'error' => t('The machine-readable name must contain only lowercase letters, numbers, underscores and hyphens.'), + ), + ); // Build the list of existing image effects for this image style. $form['effects'] = array( @@ -199,7 +201,7 @@ function image_style_form_add_submit($form, &$form_state) { * Submit handler for overriding a module-defined style. */ function image_style_form_override_submit($form, &$form_state) { - drupal_set_message(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $form_state['image_style']['name']))); + drupal_set_message(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $form_state['image_style']['label']))); image_default_style_save($form_state['image_style']); } @@ -207,11 +209,10 @@ function image_style_form_override_submit($form, &$form_state) { * Submit handler for saving an image style. */ function image_style_form_submit($form, &$form_state) { - // Update the image style name if it has changed. + // Update the image style. $style = $form_state['image_style']; - if (isset($form_state['values']['name']) && $style['name'] != $form_state['values']['name']) { - $style['name'] = $form_state['values']['name']; - } + $style['name'] = $form_state['values']['name']; + $style['label'] = $form_state['values']['label']; // Update image effect weights. if (!empty($form_state['values']['effects'])) { @@ -236,18 +237,26 @@ 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'), '#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, ); + $form['name'] = array( + '#type' => 'machine_name', + '#description' => t('The name is used in URLs for generated images. Use only lowercase letters, numbers, underscores and hyphens.'), + '#size' => '64', + '#required' => TRUE, + '#machine_name' => array( + 'exists' => 'image_style_load', + 'source' => array('label'), + 'replace_pattern' => '[^0-9a-z_\-]', + 'error' => t('The machine-readable name must contain only lowercase letters, numbers, underscores and hyphens.'), + ), + ); $form['submit'] = array( '#type' => 'submit', @@ -261,29 +270,16 @@ 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']['isid']) || $styles[$element['#value']]['isid'] != $form_state['image_style']['isid'])) { - 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. * * @param $style @@ -305,7 +301,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') @@ -319,7 +315,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'; } @@ -331,7 +327,7 @@ function image_style_revert_form($form, &$form_state, $style) { return confirm_form( $form, - t('Revert the %style style?', array('%style' => $style['name'])), + t('Revert the %style style?', array('%style' => $style['label'])), 'admin/config/media/image-styles', t('Reverting this style will delete the customized settings and restore the defaults provided by the @module module.', array('@module' => $style['module'])), t('Revert'), t('Cancel') @@ -342,7 +338,7 @@ function image_style_revert_form($form, &$form_state, $style) { * Submit handler to convert an overridden style to its default. */ function image_style_revert_form_submit($form, &$form_state) { - drupal_set_message(t('The %style style has been reverted to its defaults.', array('%style' => $form_state['image_style']['name']))); + drupal_set_message(t('The %style style has been reverted to its defaults.', array('%style' => $form_state['image_style']['label']))); image_default_style_revert($form_state['image_style']); $form_state['redirect'] = 'admin/config/media/image-styles'; } @@ -439,7 +435,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')); } @@ -650,7 +646,7 @@ function theme_image_style_list($variables) { $rows = array(); 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']); $link_attributes = array( 'attributes' => array( 'class' => array('image-style-link'), @@ -805,7 +801,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 .= '
'; - $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 .= '
'; $output .= '' . theme('image', array('path' => $preview_url, 'alt' => t('Sample modified image'), 'title' => '', 'attributes' => $preview_attributes)) . ''; $output .= '
' . $preview_image['height'] . 'px
'; diff --git a/modules/image/image.api.php b/modules/image/image.api.php index 1cb2b0d..8115116 100644 --- a/modules/image/image.api.php +++ b/modules/image/image.api.php @@ -177,6 +177,7 @@ function hook_image_default_styles() { $styles = array(); $styles['mymodule_preview'] = array( + 'label' => 'My module preview', 'effects' => array( array( 'name' => 'image_scale', diff --git a/modules/image/image.install b/modules/image/image.install index 1d7bd4e..4b6e53b 100644 --- a/modules/image/image.install +++ b/modules/image/image.install @@ -41,7 +41,13 @@ function image_schema() { 'not null' => TRUE, ), 'name' => array( - 'description' => 'The style name.', + 'description' => 'The style machine name.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + 'label' => array( + 'description' => 'The style administrative name.', 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, @@ -449,6 +455,30 @@ function image_update_7004() { } /** + * Add a column to the 'image_style' table to store administrative labels. + */ +function image_update_7005() { + $field = array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The style administrative name.', + ); + db_add_field('image_styles', 'label', $field); + + // Do a direct query here, rather than calling image_styles(), + // in case Image module is disabled. + $styles = db_query('SELECT name FROM {image_styles}')->fetchCol(); + foreach ($styles as $style) { + db_update('image_styles') + ->fields(array('label' => $style)) + ->condition('name', $style) + ->execute(); + } +} + +/** * @} End of "addtogroup updates-7.x-extra". */ diff --git a/modules/image/image.module b/modules/image/image.module index 0e0e457..abea969 100644 --- a/modules/image/image.module +++ b/modules/image/image.module @@ -342,6 +342,7 @@ function image_image_default_styles() { $styles = array(); $styles['thumbnail'] = array( + 'label' => 'Thumbnail (100x100)', 'effects' => array( array( 'name' => 'image_scale', @@ -352,6 +353,7 @@ function image_image_default_styles() { ); $styles['medium'] = array( + 'label' => 'Medium (220x220)', 'effects' => array( array( 'name' => 'image_scale', @@ -362,6 +364,7 @@ function image_image_default_styles() { ); $styles['large'] = array( + 'label' => 'Large (480x480)', 'effects' => array( array( 'name' => 'image_scale', @@ -570,6 +573,7 @@ function image_styles() { $module_styles = module_invoke($module, 'image_default_styles'); foreach ($module_styles as $style_name => $style) { $style['name'] = $style_name; + $style['label'] = empty($style['label']) ? $style_name : $style['label']; $style['module'] = $module; $style['storage'] = IMAGE_STORAGE_DEFAULT; foreach ($style['effects'] as $key => $effect) { @@ -684,6 +688,10 @@ function image_style_save($style) { } } else { + // Add a default label when not given. + if (empty($style['label'])) { + $style['label'] = $style['name']; + } drupal_write_record('image_styles', $style); $style['is_new'] = TRUE; } @@ -755,7 +763,7 @@ function image_style_effects($style) { * If TRUE a option will be inserted in the options array. * * @return - * Array of image styles both key and value are set to style name. + * Array of image styles with the machine name as key and the label as value. */ function image_style_options($include_empty = TRUE) { $styles = image_styles(); @@ -763,10 +771,10 @@ function image_style_options($include_empty = TRUE) { if ($include_empty && !empty($styles)) { $options[''] = t(''); } - // 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'); } diff --git a/modules/image/image.test b/modules/image/image.test index 1ca8465..3142438 100644 --- a/modules/image/image.test +++ b/modules/image/image.test @@ -122,7 +122,7 @@ class ImageStylesPathAndUrlTestCase extends DrupalWebTestCase { parent::setUp('image_module_test'); $this->style_name = 'style_foo'; - image_style_save(array('name' => $this->style_name)); + image_style_save(array('name' => $this->style_name, 'label' => $this->randomString())); } /** @@ -406,13 +406,15 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { */ 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)), t('Image style successfully created.')); $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))); } /** @@ -421,6 +423,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { 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( @@ -455,9 +458,10 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { $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)), t('Image style successfully created.')); // Add effect form. @@ -479,7 +483,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { 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))); } } @@ -500,9 +504,11 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // 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; @@ -511,7 +517,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // 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')); @@ -520,12 +526,12 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // 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'); @@ -546,7 +552,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // 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']); @@ -560,10 +566,10 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // 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), t('Image style %style successfully deleted.', array('%style' => $style['label']))); } @@ -573,6 +579,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { function testDefaultStyle() { // Setup a style to be created and effects to add to it. $style_name = 'thumbnail'; + $style_label = 'Thumbnail (100x100)'; $edit_path = 'admin/config/media/image-styles/edit/' . $style_name; $delete_path = 'admin/config/media/image-styles/delete/' . $style_name; $revert_path = 'admin/config/media/image-styles/revert/' . $style_name; @@ -600,7 +607,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // Override the default. $this->drupalPost($edit_path, array(), t('Override defaults')); - $this->assertRaw(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $style_name)), t('Default image style may be overridden.')); + $this->assertRaw(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $style_label)), t('Default image style may be overridden.')); // Add sample effect to the overridden style. $this->drupalPost($edit_path, array('new' => 'image_desaturate'), t('Add')); @@ -624,7 +631,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // Create an image to ensure the override works properly. $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, t('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%file' => $image_path))); // Revert the image style. $this->drupalPost($revert_path, array(), t('Revert')); @@ -643,7 +650,8 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { 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. @@ -665,8 +673,10 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { // 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))); @@ -678,7 +688,7 @@ class ImageAdminStylesUnitTest extends ImageFieldTestCase { '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)); + $message = t('Style %name was deleted.', array('%name' => $new_style_label)); $this->assertRaw($message, $message); $this->drupalGet('node/' . $nid); @@ -1037,7 +1047,7 @@ class ImageDimensionsTestCase extends DrupalWebTestCase { $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); @@ -1625,7 +1635,7 @@ class ImageThemeFunctionWebTestCase extends DrupalWebTestCase { $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