Commit 5faaa376 authored by webchick's avatar webchick

#602938 by quicksketch: Provide default styles in image.module (add hook_image_default_styles()).

parent 6abcc47e
......@@ -39,21 +39,46 @@ function image_style_form($form, &$form_state, $style) {
$title = t('Edit %name style', array('%name' => $style['name']));
drupal_set_title($title, PASS_THROUGH);
// Adjust this form for styles that must be overridden to edit.
$editable = (bool) ($style['storage'] & IMAGE_STORAGE_EDITABLE);
if (!$editable && empty($form_state['input'])) {
drupal_set_message(t('This image style is currently being provided by a module. Click the "Override defaults" button to change its settings.'), 'warning');
}
$form_state['image_style'] = $style;
$form['#tree'] = TRUE;
$form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array('preprocess' => FALSE);
// Allow the name of the style to be changed.
$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,
// Show the thumbnail preview.
$form['preview'] = array(
'#type' => 'item',
'#title' => t('Preview'),
'#markup' => theme('image_style_preview', array('style' => $style)),
);
// 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,
);
}
// Build the list of existing image effects for this image style.
$form['effects'] = array(
'#theme' => 'image_style_effects',
......@@ -69,12 +94,15 @@ function image_style_form($form, &$form_state, $style) {
$form['effects'][$ieid]['weight'] = array(
'#type' => 'weight',
'#default_value' => $effect['weight'],
'#access' => $editable,
);
$form['effects'][$ieid]['configure'] = array(
'#markup' => isset($effect['form callback']) ? l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $effect['ieid'] ) : '',
'#access' => $editable,
);
$form['effects'][$ieid]['remove'] = array(
'#markup' => l(t('delete'), 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $effect['ieid'] . '/delete'),
'#access' => $editable,
);
}
......@@ -86,6 +114,7 @@ function image_style_form($form, &$form_state, $style) {
$form['effects']['new'] = array(
'#tree' => FALSE,
'#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
'#access' => $editable,
);
$form['effects']['new']['new'] = array(
'#type' => 'select',
......@@ -102,15 +131,18 @@ function image_style_form($form, &$form_state, $style) {
'#submit' => array('image_style_form_submit', 'image_style_form_add_submit'),
);
// Show the current preview of the style and the submit button.
$form['preview'] = array(
'#type' => 'item',
'#title' => t('Preview'),
'#markup' => theme('image_style_preview', array('style' => $style)),
// Show the Override or Submit button for this style.
$form['override'] = array(
'#type' => 'submit',
'#value' => t('Override defaults'),
'#validate' => array(),
'#submit' => array('image_style_form_override_submit'),
'#access' => !$editable,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Update style'),
'#access' => $editable,
);
return $form;
......@@ -148,13 +180,21 @@ 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'])));
image_default_style_save($form_state['image_style']);
}
/**
* Submit handler for saving an image style.
*/
function image_style_form_submit($form, &$form_state) {
// Update the image style name if it has changed.
$style = $form_state['image_style'];
if ($style['name'] != $form_state['values']['name']) {
if (isset($form_state['values']['name']) && $style['name'] != $form_state['values']['name']) {
$style['name'] = $form_state['values']['name'];
}
......@@ -268,6 +308,30 @@ function image_style_delete_form_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/media/image-styles';
}
/**
* Confirmation form to revert a database style to its default.
*/
function image_style_revert_form($form, $form_state, $style) {
$form_state['image_style'] = $style;
return confirm_form(
$form,
t('Revert the %style style?', array('%style' => $style['name'])),
'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')
);
}
/**
* 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 revert to its defaults.', array('%style' => $form_state['image_style']['name'])));
image_default_style_revert($form_state['image_style']);
$form_state['redirect'] = 'admin/config/media/image-styles';
}
/**
* Form builder; Form for adding and editing image effects.
*
......@@ -565,7 +629,7 @@ function image_rotate_form($data) {
function theme_image_style_list($variables) {
$styles = $variables['styles'];
$header = array(t('Style name'), array('data' => t('Operations'), 'colspan' => 3));
$header = array(t('Style name'), t('Settings'), array('data' => t('Operations'), 'colspan' => 3));
$rows = array();
foreach ($styles as $style) {
$row = array();
......@@ -575,8 +639,21 @@ function theme_image_style_list($variables) {
'class' => array('image-style-link'),
),
);
$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);
if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
$row[] = t('Custom');
$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);
}
elseif ($style['storage'] == IMAGE_STORAGE_OVERRIDE) {
$row[] = t('Overridden');
$row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
$row[] = l(t('revert'), 'admin/config/media/image-styles/revert/' . $style['name'], $link_attributes);
}
else {
$row[] = t('Default');
$row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes);
$row[] = '';
}
$rows[] = $row;
}
......@@ -623,10 +700,12 @@ function theme_image_style_effects($variables) {
$row[] = array('data' => '', 'colspan' => 2);
}
$rows[] = array(
'data' => $row,
'class' => array('draggable'),
);
if (!isset($form[$key]['#access']) || $form[$key]['#access']) {
$rows[] = array(
'data' => $row,
'class' => !empty($form[$key]['weight']['#access']) ? array('draggable') : array(),
);
}
}
$header = array(
......@@ -635,7 +714,7 @@ function theme_image_style_effects($variables) {
array('data' => t('Operations'), 'colspan' => 2),
);
if (count($rows) == 1) {
if (count($rows) == 1 && $form['new']['#access']) {
array_unshift($rows, array(array(
'data' => t('There are currently no effects in this style. Add one by selecting an option below.'),
'colspan' => 4,
......
......@@ -98,6 +98,81 @@ function hook_image_style_flush($style) {
// Empty cached data that contains information about the style.
cache_clear_all('*', 'cache_mymodule', TRUE);
}
/**
* 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.
*
* If your module needs to provide a new style (rather than modify an existing
* one) use hook_image_default_styles() instead.
*
* @see hook_image_default_styles()
*/
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,
);
}
}
/**
* Provide module-based image styles for reuse throughout Drupal.
*
* This hook allows your module to provide image styles. This may be useful if
* you require images to fit within exact dimensions. Note that you should
* attempt to re-use the default styles provided by Image module whenever
* possible, rather than creating image styles that are specific to your module.
* Image provides the styles "thumbnail", "medium", and "large".
*
* You may use this hook to more easily manage your site's changes by moving
* existing image styles from the database to a custom module. Note however that
* moving image styles to code instead storing them in the database has a
* negligible effect on performance, since custom image styles are loaded
* from the database all at once. Even if all styles are pulled from modules,
* Image module will still perform the same queries to check the database for
* any custom styles.
*
* @return
* An array of image styles, keyed by the style name.
* @see image_image_default_styles()
*/
function hook_image_default_styles() {
$styles = array();
$styles['mymodule_preview'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array('width' => 400, 'height' => 400, 'upscale' => 1),
'weight' => 0,
),
array(
'name' => 'image_desaturate',
'data' => array(),
'weight' => 1,
),
),
);
return $styles;
}
/**
* @} End of "addtogroup hooks".
*/
This diff is collapsed.
......@@ -133,17 +133,6 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase {
module_load_include('inc', 'image', 'image.effects');
}
/**
* Test the image_effects() and image_effect_definitions() functions.
*/
function testEffects() {
$effects = image_effects();
$this->assertEqual(count($effects), 1, t("Found core's image effect."));
$effect_definitions = image_effect_definitions();
$this->assertEqual(count($effect_definitions), 6, t("Found core's image effects."));
}
/**
* Test the image_resize_effect() function.
*/
......@@ -428,4 +417,65 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase {
$this->assertFalse(image_style_load($style_name), t('Image style %style successfully deleted.', array('%style' => $style['name'])));
}
/**
* Test to override, edit, then revert a style.
*/
function testDefaultStyle() {
// Setup a style to be created and effects to add to it.
$style_name = 'thumbnail';
$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;
// Ensure deleting a default is not possible.
$this->drupalGet($delete_path);
$this->assertText(t('Page not found'), t('Default styles may not be deleted.'));
// Ensure that editing a default is not possible (without overriding).
$this->drupalGet($edit_path);
$this->assertNoField('edit-name', t('Default styles may not be renamed.'));
$this->assertNoField('edit-submit', t('Default styles may not be edited.'));
$this->assertNoField('edit-add', t('Default styles may not have new effects added.'));
// Create an image to make sure the default works before overriding.
drupal_static_reset('image_styles');
$style = image_style_load($style_name);
$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)));
// 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.'));
// Add sample effect to the overridden style.
$this->drupalPost($edit_path, array('new' => 'image_desaturate'), t('Add'));
drupal_static_reset('image_styles');
$style = image_style_load($style_name);
// The style should now have 2 effect, the original scale provided by core
// and the desaturate effect we added in the override.
$effects = array_values($style['effects']);
$this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the overridden style.'));
$this->assertEqual($effects[1]['name'], 'image_desaturate', t('The added effect exists in the overridden style.'));
// Check that we are unable to rename an overridden style.
$this->drupalGet($edit_path);
$this->assertNoField('edit-name', t('Overridden styles may not be renamed.'));
// 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)));
// Revert the image style.
$this->drupalPost($revert_path, array(), t('Revert'));
drupal_static_reset('image_styles');
$style = image_style_load($style_name);
// The style should now have the single effect for scale.
$effects = array_values($style['effects']);
$this->assertEqual($effects[0]['name'], 'image_scale', t('The default effect still exists in the reverted style.'));
$this->assertFalse(array_key_exists(1, $effects), t('The added effect has been removed in the reverted style.'));
}
}
......@@ -173,16 +173,6 @@ function default_install() {
// Don't display date and author information for page nodes by default.
variable_set('node_submitted_page', FALSE);
// Create an image style.
$style = array('name' => 'thumbnail');
$style = image_style_save($style);
$effect = array(
'isid' => $style['isid'],
'name' => 'image_scale_and_crop',
'data' => array('width' => '85', 'height' => '85'),
);
image_effect_save($effect);
// Enable user picture support and set the default to a square thumbnail option.
variable_set('user_pictures', '1');
variable_set('user_picture_dimensions', '1024x1024');
......
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