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

merge-requests/26/head
Angie Byron 2009-10-16 00:52:46 +00:00
parent 6abcc47e25
commit 5faaa376aa
5 changed files with 431 additions and 64 deletions

View File

@ -39,21 +39,46 @@ 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['name']));
drupal_set_title($title, PASS_THROUGH); 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_state['image_style'] = $style;
$form['#tree'] = TRUE; $form['#tree'] = TRUE;
$form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array('preprocess' => FALSE); $form['#attached']['css'][drupal_get_path('module', 'image') . '/image.admin.css'] = array('preprocess' => FALSE);
// Allow the name of the style to be changed. // Show the thumbnail preview.
$form['name'] = array( $form['preview'] = array(
'#type' => 'textfield', '#type' => 'item',
'#size' => '64', '#title' => t('Preview'),
'#title' => t('Image style name'), '#markup' => theme('image_style_preview', array('style' => $style)),
'#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,
); );
// 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. // Build the list of existing image effects for this image style.
$form['effects'] = array( $form['effects'] = array(
'#theme' => 'image_style_effects', '#theme' => 'image_style_effects',
@ -69,12 +94,15 @@ function image_style_form($form, &$form_state, $style) {
$form['effects'][$ieid]['weight'] = array( $form['effects'][$ieid]['weight'] = array(
'#type' => 'weight', '#type' => 'weight',
'#default_value' => $effect['weight'], '#default_value' => $effect['weight'],
'#access' => $editable,
); );
$form['effects'][$ieid]['configure'] = array( $form['effects'][$ieid]['configure'] = array(
'#markup' => isset($effect['form callback']) ? l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $effect['ieid'] ) : '', '#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( $form['effects'][$ieid]['remove'] = array(
'#markup' => l(t('delete'), 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $effect['ieid'] . '/delete'), '#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( $form['effects']['new'] = array(
'#tree' => FALSE, '#tree' => FALSE,
'#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL, '#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
'#access' => $editable,
); );
$form['effects']['new']['new'] = array( $form['effects']['new']['new'] = array(
'#type' => 'select', '#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'), '#submit' => array('image_style_form_submit', 'image_style_form_add_submit'),
); );
// Show the current preview of the style and the submit button. // Show the Override or Submit button for this style.
$form['preview'] = array( $form['override'] = array(
'#type' => 'item', '#type' => 'submit',
'#title' => t('Preview'), '#value' => t('Override defaults'),
'#markup' => theme('image_style_preview', array('style' => $style)), '#validate' => array(),
'#submit' => array('image_style_form_override_submit'),
'#access' => !$editable,
); );
$form['submit'] = array( $form['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
'#value' => t('Update style'), '#value' => t('Update style'),
'#access' => $editable,
); );
return $form; 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. * Submit handler for saving an image style.
*/ */
function image_style_form_submit($form, &$form_state) { function image_style_form_submit($form, &$form_state) {
// Update the image style name if it has changed. // Update the image style name if it has changed.
$style = $form_state['image_style']; $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']; $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'; $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. * Form builder; Form for adding and editing image effects.
* *
@ -565,7 +629,7 @@ function image_rotate_form($data) {
function theme_image_style_list($variables) { function theme_image_style_list($variables) {
$styles = $variables['styles']; $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(); $rows = array();
foreach ($styles as $style) { foreach ($styles as $style) {
$row = array(); $row = array();
@ -575,8 +639,21 @@ function theme_image_style_list($variables) {
'class' => array('image-style-link'), 'class' => array('image-style-link'),
), ),
); );
$row[] = l(t('edit'), 'admin/config/media/image-styles/edit/' . $style['name'], $link_attributes); if ($style['storage'] == IMAGE_STORAGE_NORMAL) {
$row[] = l(t('delete'), 'admin/config/media/image-styles/delete/' . $style['name'], $link_attributes); $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; $rows[] = $row;
} }
@ -623,10 +700,12 @@ function theme_image_style_effects($variables) {
$row[] = array('data' => '', 'colspan' => 2); $row[] = array('data' => '', 'colspan' => 2);
} }
$rows[] = array( if (!isset($form[$key]['#access']) || $form[$key]['#access']) {
'data' => $row, $rows[] = array(
'class' => array('draggable'), 'data' => $row,
); 'class' => !empty($form[$key]['weight']['#access']) ? array('draggable') : array(),
);
}
} }
$header = array( $header = array(
@ -635,7 +714,7 @@ function theme_image_style_effects($variables) {
array('data' => t('Operations'), 'colspan' => 2), array('data' => t('Operations'), 'colspan' => 2),
); );
if (count($rows) == 1) { if (count($rows) == 1 && $form['new']['#access']) {
array_unshift($rows, array(array( array_unshift($rows, array(array(
'data' => t('There are currently no effects in this style. Add one by selecting an option below.'), 'data' => t('There are currently no effects in this style. Add one by selecting an option below.'),
'colspan' => 4, 'colspan' => 4,

View File

@ -98,6 +98,81 @@ function hook_image_style_flush($style) {
// Empty cached data that contains information about the style. // Empty cached data that contains information about the style.
cache_clear_all('*', 'cache_mymodule', TRUE); 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". * @} End of "addtogroup hooks".
*/ */

View File

@ -6,6 +6,31 @@
* Exposes global functionality for creating image styles. * Exposes global functionality for creating image styles.
*/ */
/**
* Image style constant for user presets in the database.
*/
define('IMAGE_STORAGE_NORMAL', 1);
/**
* Image style constant for user presets that override module-defined presets.
*/
define('IMAGE_STORAGE_OVERRIDE', 2);
/**
* Image style constant for module-defined presets in code.
*/
define('IMAGE_STORAGE_DEFAULT', 4);
/**
* Image style constant to represent an editable preset.
*/
define('IMAGE_STORAGE_EDITABLE', IMAGE_STORAGE_NORMAL | IMAGE_STORAGE_OVERRIDE);
/**
* Image style constant to represent any module-based preset.
*/
define('IMAGE_STORAGE_MODULE', IMAGE_STORAGE_OVERRIDE | IMAGE_STORAGE_DEFAULT);
// Load all Field module hooks for Image. // Load all Field module hooks for Image.
require_once DRUPAL_ROOT . '/modules/image/image.field.inc'; require_once DRUPAL_ROOT . '/modules/image/image.field.inc';
@ -31,7 +56,7 @@ function image_help($path, $arg) {
return '<p>' . t('Image styles commonly provide thumbnail sizes by scaling and cropping images, but can also add various effects before an image is displayed. When an image is displayed with a style, a new file is created and the original image is left unchanged.') . '</p>'; return '<p>' . t('Image styles commonly provide thumbnail sizes by scaling and cropping images, but can also add various effects before an image is displayed. When an image is displayed with a style, a new file is created and the original image is left unchanged.') . '</p>';
case 'admin/config/media/image-styles/edit/%/add/%': case 'admin/config/media/image-styles/edit/%/add/%':
case 'admin/config/media/image-styles/edit/%/effects/%': case 'admin/config/media/image-styles/edit/%/effects/%':
$effect = ($arg[5] == 'add') ? image_effect_definition_load($arg[6]) : image_effect_load($arg[6]); $effect = ($arg[5] == 'add') ? image_effect_definition_load($arg[6]) : image_effect_load($arg[6], $arg[4]);
return isset($effect['help']) ? ('<p>' . $effect['help'] . '</p>') : NULL; return isset($effect['help']) ? ('<p>' . $effect['help'] . '</p>') : NULL;
} }
} }
@ -87,8 +112,19 @@ function image_menu() {
$items['admin/config/media/image-styles/delete/%image_style'] = array( $items['admin/config/media/image-styles/delete/%image_style'] = array(
'title' => 'Delete style', 'title' => 'Delete style',
'description' => 'Delete an image style.', 'description' => 'Delete an image style.',
'load arguments' => array(NULL, (string) IMAGE_STORAGE_NORMAL),
'page callback' => 'drupal_get_form', 'page callback' => 'drupal_get_form',
'page arguments' => array('image_style_delete_form', 5, TRUE), 'page arguments' => array('image_style_delete_form', 5),
'access arguments' => array('administer image styles'),
'type' => MENU_CALLBACK,
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/revert/%image_style'] = array(
'title' => 'Revert style',
'description' => 'Revert an image style.',
'load arguments' => array(NULL, (string) IMAGE_STORAGE_OVERRIDE),
'page callback' => 'drupal_get_form',
'page arguments' => array('image_style_revert_form', 5),
'access arguments' => array('administer image styles'), 'access arguments' => array('administer image styles'),
'type' => MENU_CALLBACK, 'type' => MENU_CALLBACK,
'file' => 'image.admin.inc', 'file' => 'image.admin.inc',
@ -96,6 +132,7 @@ function image_menu() {
$items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect'] = array( $items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect'] = array(
'title' => 'Edit image effect', 'title' => 'Edit image effect',
'description' => 'Edit an exiting effect within a style.', 'description' => 'Edit an exiting effect within a style.',
'load arguments' => array(5, (string) IMAGE_STORAGE_EDITABLE),
'page callback' => 'drupal_get_form', 'page callback' => 'drupal_get_form',
'page arguments' => array('image_effect_form', 5, 7), 'page arguments' => array('image_effect_form', 5, 7),
'access arguments' => array('administer image styles'), 'access arguments' => array('administer image styles'),
@ -105,6 +142,7 @@ function image_menu() {
$items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect/delete'] = array( $items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect/delete'] = array(
'title' => 'Delete image effect', 'title' => 'Delete image effect',
'description' => 'Delete an exiting effect from a style.', 'description' => 'Delete an exiting effect from a style.',
'load arguments' => array(5, (string) IMAGE_STORAGE_EDITABLE),
'page callback' => 'drupal_get_form', 'page callback' => 'drupal_get_form',
'page arguments' => array('image_effect_delete_form', 5, 7), 'page arguments' => array('image_effect_delete_form', 5, 7),
'access arguments' => array('administer image styles'), 'access arguments' => array('administer image styles'),
@ -114,6 +152,7 @@ function image_menu() {
$items['admin/config/media/image-styles/edit/%image_style/add/%image_effect_definition'] = array( $items['admin/config/media/image-styles/edit/%image_style/add/%image_effect_definition'] = array(
'title' => 'Add image effect', 'title' => 'Add image effect',
'description' => 'Add a new effect to a style.', 'description' => 'Add a new effect to a style.',
'load arguments' => array(5),
'page callback' => 'drupal_get_form', 'page callback' => 'drupal_get_form',
'page arguments' => array('image_effect_form', 5, 7), 'page arguments' => array('image_effect_form', 5, 7),
'access arguments' => array('administer image styles'), 'access arguments' => array('administer image styles'),
@ -277,6 +316,45 @@ function image_file_references($file) {
return $count ? array('image' => $count) : NULL; return $count ? array('image' => $count) : NULL;
} }
/**
* Implement hook_image_default_styles().
*/
function image_image_default_styles() {
$styles = array();
$styles['thumbnail'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array('width' => 100, 'height' => 100, 'upscale' => 1),
'weight' => 0,
),
)
);
$styles['medium'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array('width' => 220, 'height' => 220, 'upscale' => 1),
'weight' => 0,
),
)
);
$styles['large'] = array(
'effects' => array(
array(
'name' => 'image_scale',
'data' => array('width' => 640, 'height' => 640, 'upscale' => 1),
'weight' => 0,
),
)
);
return $styles;
}
/** /**
* Clear cached versions of a specific file in all styles. * Clear cached versions of a specific file in all styles.
* *
@ -310,15 +388,44 @@ function image_styles() {
} }
else { else {
$styles = array(); $styles = array();
$result = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('image_styles') // Select the module-defined styles.
->orderBy('name') foreach (module_implements('image_default_styles') as $module) {
->execute(); $module_styles = module_invoke($module, 'image_default_styles');
foreach ($result as $style) { foreach ($module_styles as $style_name => $style) {
$styles[$style['name']] = $style; $style['name'] = $style_name;
$styles[$style['name']]['effects'] = image_style_effects($style); $style['module'] = $module;
$style['storage'] = IMAGE_STORAGE_DEFAULT;
foreach ($style['effects'] as $ieid => $effect) {
$definition = image_effect_definition_load($effect['name']);
$effect = array_merge($definition, $effect);
$effect['ieid'] = $ieid;
$style['effects'][$ieid] = $effect;
}
$styles[$style_name] = $style;
}
} }
// Select all the user-defined styles.
$user_styles = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('image_styles')
->orderBy('name')
->execute()
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
// Allow the user styles to override the module styles.
foreach ($user_styles as $style_name => $style) {
$style['module'] = NULL;
$style['storage'] = IMAGE_STORAGE_NORMAL;
$style['effects'] = image_style_effects($style);
if (isset($styles[$style_name]['module'])) {
$style['module'] = $styles[$style_name]['module'];
$style['storage'] = IMAGE_STORAGE_OVERRIDE;
}
$styles[$style_name] = $style;
}
drupal_alter('image_styles', $styles);
cache_set('image_styles', $styles); cache_set('image_styles', $styles);
} }
} }
@ -333,6 +440,9 @@ function image_styles() {
* The name of the style. * The name of the style.
* @param $isid * @param $isid
* Optional. The numeric id of a style if the name is not known. * Optional. The numeric id of a style if the name is not known.
* @param $include
* If set, this loader will restrict to a specific type of image style, may be
* one of the defined Image style storage constants.
* @return * @return
* An image style array containing the following keys: * An image style array containing the following keys:
* - "isid": The unique image style ID. * - "isid": The unique image style ID.
@ -341,23 +451,29 @@ function image_styles() {
* If the image style name or ID is not valid, an empty array is returned. * If the image style name or ID is not valid, an empty array is returned.
* @see image_effect_load() * @see image_effect_load()
*/ */
function image_style_load($name = NULL, $isid = NULL) { function image_style_load($name = NULL, $isid = NULL, $include = NULL) {
$styles = image_styles(); $styles = image_styles();
// If retrieving by name. // If retrieving by name.
if (isset($name) && isset($styles[$name])) { if (isset($name) && isset($styles[$name])) {
return $styles[$name]; $style = $styles[$name];
} }
// If retrieving by image style id. // If retrieving by image style id.
if (isset($isid)) { if (!isset($name) && isset($isid)) {
foreach ($styles as $name => $style) { foreach ($styles as $name => $database_style) {
if ($style['isid'] == $isid) { if (isset($database_style['isid']) && $database_style['isid'] == $isid) {
return $style; break;
} }
} }
} }
// Restrict to the specific type of flag. This bitwise operation basically
// states "if the storage is X, then allow".
if (isset($style) && (!isset($include) || ($style['storage'] & (int) $include))) {
return $style;
}
// Otherwise the style was not found. // Otherwise the style was not found.
return FALSE; return FALSE;
} }
@ -658,6 +774,44 @@ function image_style_path($style_name, $uri) {
return $scheme . '://styles/' . $style_name . '/' . $path; return $scheme . '://styles/' . $style_name . '/' . $path;
} }
/**
* Save a default image style to the database.
*
* @param style
* An image style array provided by a module.
* @return
* An image style array. The returned style array will include the new 'isid'
* assigned to the style.
*/
function image_default_style_save($style) {
$style = image_style_save($style);
$effects = array();
foreach ($style['effects'] as $effect) {
$effect['isid'] = $style['isid'];
image_effect_save($effect);
$effects[$effect['ieid']] = $effect;
}
$style['effects'] = $effects;
return $style;
}
/**
* Revert the changes made by users to a default image style.
*
* @param style
* An image style array.
* @return
* Boolean TRUE if the operation succeeded.
*/
function image_default_style_revert($style) {
image_style_flush($style);
db_delete('image_effects')->condition('isid', $style['isid'])->execute();
db_delete('image_styles')->condition('isid', $style['isid'])->execute();
return TRUE;
}
/** /**
* Pull in image effects exposed by modules implementing hook_image_effect_info(). * Pull in image effects exposed by modules implementing hook_image_effect_info().
* *
@ -704,6 +858,8 @@ function image_effect_definitions() {
* *
* @param $effect * @param $effect
* The name of the effect definition to load. * The name of the effect definition to load.
* @param $style
* An image style array to which this effect will be added.
* @return * @return
* An array containing the image effect definition with the following keys: * An array containing the image effect definition with the following keys:
* - "effect": The unique name for the effect being performed. Usually prefixed * - "effect": The unique name for the effect being performed. Usually prefixed
@ -711,12 +867,22 @@ function image_effect_definitions() {
* - "module": The module providing the effect. * - "module": The module providing the effect.
* - "help": A description of the effect. * - "help": A description of the effect.
* - "function": The name of the function that will execute the effect. * - "function": The name of the function that will execute the effect.
* - "form": i'm (optional) The name of a function to configure 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 * - "summary": (optional) The name of a theme function that will display a
* one-line summary of the effect. Does not include the "theme_" prefix. * one-line summary of the effect. Does not include the "theme_" prefix.
*/ */
function image_effect_definition_load($effect) { function image_effect_definition_load($effect, $style_name = NULL) {
$definitions = image_effect_definitions(); $definitions = image_effect_definitions();
// If a style is specified, do not allow loading of default style
// effects.
if (isset($style_name)) {
$style = image_style_load($style_name, NULL);
if ($style['storage'] == IMAGE_STORAGE_DEFAULT) {
return FALSE;
}
}
return isset($definitions[$effect]) ? $definitions[$effect] : FALSE; return isset($definitions[$effect]) ? $definitions[$effect] : FALSE;
} }
@ -757,6 +923,11 @@ function image_effects() {
* *
* @param $ieid * @param $ieid
* The image effect ID. * The image effect ID.
* @param $style_name
* The image style name.
* @param $include
* If set, this loader will restrict to a specific type of image style, may be
* one of the defined Image style storage constants.
* @return * @return
* An image effect array, consisting of the following keys: * An image effect array, consisting of the following keys:
* - "ieid": The unique image effect ID. * - "ieid": The unique image effect ID.
@ -770,9 +941,11 @@ function image_effects() {
* @see image_style_load() * @see image_style_load()
* @see image_effect_definition_load() * @see image_effect_definition_load()
*/ */
function image_effect_load($ieid) { function image_effect_load($ieid, $style_name, $include = NULL) {
$effects = image_effects(); if (($style = image_style_load($style_name, NULL, $include)) && isset($style['effects'][$ieid])) {
return isset($effects[$ieid]) ? $effects[$ieid] : FALSE; return $style['effects'][$ieid];
}
return FALSE;
} }
/** /**

View File

@ -133,17 +133,6 @@ class ImageEffectsUnitTest extends ImageToolkitTestCase {
module_load_include('inc', 'image', 'image.effects'); 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. * 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']))); $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.'));
}
} }

View File

@ -173,16 +173,6 @@ function default_install() {
// Don't display date and author information for page nodes by default. // Don't display date and author information for page nodes by default.
variable_set('node_submitted_page', FALSE); 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. // Enable user picture support and set the default to a square thumbnail option.
variable_set('user_pictures', '1'); variable_set('user_pictures', '1');
variable_set('user_picture_dimensions', '1024x1024'); variable_set('user_picture_dimensions', '1024x1024');