diff --git a/core/modules/config/config.api.php b/core/modules/config/config.api.php index 147412d..19d3b4f 100644 --- a/core/modules/config/config.api.php +++ b/core/modules/config/config.api.php @@ -112,7 +112,7 @@ function hook_config_import_delete($name, $new_config, $old_config) { if (strpos($name, 'config_test.dynamic.') !== 0) { return FALSE; } - // @todo image_style_delete() supports the notion of a "replacement style" + // @todo Image styles support the notion of a "replacement style" // to be used by other modules instead of the deleted style. Essential! // But that is impossible currently, since the config system only knows // about deleted and added changes. Introduce an 'old_ID' key within diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc index ca55227..e480a5f 100644 --- a/core/modules/image/image.admin.inc +++ b/core/modules/image/image.admin.inc @@ -206,23 +206,11 @@ function image_style_form_submit($form, &$form_state) { } } - // Update the image style name if it has changed. We can not rename a piece - // of config, all we can do is create a new one and delete the old one. - if (isset($form_state['values']['name']) && $style->id() != $form_state['values']['name']) { - $old_style = $style; - $data = array( - 'effects' => $style->effects, - 'name' => $form_state['values']['name'], - ); - $style = entity_create('image_style', $data); - } + $style->set('name', $form_state['values']['name']); $style->set('label', $form_state['values']['label']); - $style->save(); - if (isset($old_style)) { - image_style_delete($old_style, $style->id()); - } + $status = $style->save(); - if ($form_state['values']['op'] == t('Update style')) { + if ($status == SAVED_UPDATED) { drupal_set_message(t('Changes to the style have been saved.')); } $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style->id(); @@ -262,11 +250,10 @@ 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( + $style = entity_create('image_style', array( 'name' => $form_state['values']['name'], 'label' => $form_state['values']['label'], - ); - $style = entity_create('image_style', $style); + )); $style->save(); drupal_set_message(t('Style %name was created.', array('%name' => $style->label()))); $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style->id(); @@ -306,8 +293,8 @@ function image_style_delete_form($form, &$form_state, $style) { */ function image_style_delete_form_submit($form, &$form_state) { $style = $form_state['image_style']; - - image_style_delete($style, $form_state['values']['replacement']); + $style->set('replacementID', $form_state['values']['replacement']); + $style->delete(); drupal_set_message(t('Style %name was deleted.', array('%name' => $style->label()))); $form_state['redirect'] = 'admin/config/media/image-styles'; } @@ -316,8 +303,8 @@ function image_style_delete_form_submit($form, &$form_state) { * Form builder; Form for adding and editing image effects. * * This form is used universally for editing all image effects. Each effect adds - * its own custom section to the form by calling the form function specified in - * hook_image_effects(). + * its own custom section to the form by calling the 'form callback' specified + * in hook_image_effect_info(). * * @param $form_state * An associative array containing the current state of the form. @@ -327,8 +314,6 @@ function image_style_delete_form_submit($form, &$form_state) { * An image effect array. * * @ingroup forms - * @see hook_image_effects() - * @see image_effects() * @see image_resize_form() * @see image_scale_form() * @see image_rotate_form() diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 315f99a..ab4438e 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -72,6 +72,35 @@ function image_help($path, $arg) { } /** + * Implements hook_entity_info(). + */ +function image_entity_info() { + return array( + 'image_style' => array( + 'label' => t('Image style'), + 'entity class' => 'Drupal\image\ImageStyle', + 'controller class' => 'Drupal\Core\Config\Entity\ConfigStorageController', + 'uri callback' => 'image_style_entity_uri', + 'config prefix' => 'image.style', + 'entity keys' => array( + 'id' => 'name', + 'label' => 'label', + 'uuid' => 'uuid', + ), + ), + ); +} + +/** + * Entity URI callback for image style. + */ +function image_style_entity_uri(ImageStyle $style) { + return array( + 'path' => 'admin/config/media/image-styles/manage/' . $style->id(), + ); +} + +/** * Implements hook_menu(). */ function image_menu() { @@ -326,9 +355,9 @@ function image_file_predelete(File $file) { } /** - * Implements hook_image_style_save(). + * Implements hook_image_style_update(). */ -function image_image_style_save($style) { +function image_image_style_update(ImageStyle $style) { if ($style->id() != $style->getOriginalID()) { $instances = field_read_instances(); // Loop through all fields searching for image fields. @@ -359,10 +388,17 @@ function image_image_style_save($style) { /** * Implements hook_image_style_delete(). */ -function image_image_style_delete($style) { - image_image_style_save($style); +function image_image_style_delete(ImageStyle $style) { // Flush cached media for the style. image_style_flush($style); + // Check whether field instance settings need to be updated. + // In case no replacement style was specified, all image fields that are using + // the deleted style are left in a broken state. + if ($new_id = $style->get('replacementID')) { + // The deleted ID is still set as originalID. + $style->set('name', $new_id); + image_image_style_update($style); + } } /** @@ -549,14 +585,15 @@ function image_config_import_delete($name, $new_config, $old_config) { if (strpos($name, 'image.style.') !== 0) { return FALSE; } - // @todo image_style_delete() supports the notion of a "replacement style" + // @todo Image styles support the notion of a "replacement style" // to be used by other modules instead of the deleted style. Essential! // But that is impossible currently, since the config system only knows // about deleted and added changes. Introduce an 'old_ID' key within // config objects as a standard? list(, , $id) = explode('.', $name); $style = entity_load('image_style', $id); - return image_style_delete($style); + $style->delete(); + return TRUE; } /** @@ -587,38 +624,6 @@ function image_image_style_load($styles) { } /** - * Implements hook_image_style_update(). - */ -function image_image_style_update($style) { - // Flush cached media for the style. - image_style_flush($style); -} - -/** - * Delete an image style. - * - * @param Drupal\image\ImageStyle $style - * An image style array. - * @param $replacement_style_name - * (optional) When deleting a style, specify a replacement style name so - * that existing settings (if any) may be converted to a new style. - * - * @return - * TRUE on success. - */ -function image_style_delete($style, $replacement_style_name = '') { - $style->delete(); - - // Let other modules update as necessary on save. - if ($replacement_style_name) { - $style->set('name', $replacement_style_name); - } - module_invoke_all('image_style_delete', $style); - - return TRUE; -} - -/** * Get an array of image styles suitable for using as select list options. * * @param $include_empty @@ -823,12 +828,6 @@ function image_style_flush($style) { // Let other modules update as necessary on flush. module_invoke_all('image_style_flush', $style); -// // Clear image style and effect caches. -// cache()->delete('image_styles'); -// cache()->deletePrefix('image_effects:'); -// drupal_static_reset('image_styles'); -// drupal_static_reset('image_effects'); - // Clear field caches so that formatters may be added for this style. field_info_cache_clear(); drupal_theme_rebuild(); @@ -963,43 +962,6 @@ function image_effect_definition_load($effect) { } /** - * Load all image effects from the database. - * - * @return - * An array of all image effects. - * @see image_effect_load() - * - * @todo Remove after moving/resolving the todo. - */ -function image_effects() { - $effects = &drupal_static(__FUNCTION__); - - if (!isset($effects)) { - $effects = array(); - - // Add database image effects. - // @todo Strictly speaking, this is obsolete. However, it demonstrates a - // use-case for retrieving/listing configuration objects using a wildcard - // within the name (instead of only the suffix). - $result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC)) - ->fields('image_effects') - ->orderBy('image_effects.weight', 'ASC') - ->execute(); - foreach ($result as $effect) { - $effect['data'] = unserialize($effect['data']); - $definition = image_effect_definition_load($effect['name']); - // Do not load image effects whose definition cannot be found. - if ($definition) { - $effect = array_merge($definition, $effect); - $effects[$effect['ieid']] = $effect; - } - } - } - - return $effects; -} - -/** * Load a single image effect. * * @param $ieid @@ -1048,6 +1010,10 @@ function image_effect_load($ieid, $style_name) { * The saved image effect array. The 'ieid' key will be set for the effect. */ function image_effect_save($style, &$effect) { + // Remove all values that are not properties of an image effect. + // @todo Convert image effects into plugins. + $effect = array_intersect_key($effect, array_flip(array('ieid', 'module', 'name', 'data', 'weight'))); + // Generate a unique image effect ID for a new effect. if (empty($effect['ieid'])) { $uuid = new Uuid(); @@ -1163,31 +1129,3 @@ function _image_effect_definitions_sort($a, $b) { return strcasecmp($a['name'], $b['name']); } -/** - * Implements hook_entity_info(). - */ -function image_entity_info() { - return array( - 'image_style' => array( - 'label' => t('Image style'), - 'entity class' => 'Drupal\image\ImageStyle', - 'controller class' => 'Drupal\Core\Config\Entity\ConfigStorageController', - 'uri callback' => 'image_style_uri', - 'config prefix' => 'image.style', - 'entity keys' => array( - 'id' => 'name', - 'label' => 'label', - 'uuid' => 'uuid', - ), - ), - ); -} - -/** - * URI callbacks for image styles. - */ -function image_style_uri(ImageStyle $image_style) { - return array( - 'path' => 'admin/config/media/image-styles/manage/' . $image_style->id(), - ); -} diff --git a/core/modules/image/lib/Drupal/image/ImageStyle.php b/core/modules/image/lib/Drupal/image/ImageStyle.php index fae896f..422bcba 100644 --- a/core/modules/image/lib/Drupal/image/ImageStyle.php +++ b/core/modules/image/lib/Drupal/image/ImageStyle.php @@ -15,6 +15,13 @@ class ImageStyle extends ConfigEntityBase { /** + * The name of the image style to use as replacement upon delete. + * + * @var string + */ + protected $replacementID; + + /** * The name of the image style. * * @var string @@ -35,7 +42,6 @@ class ImageStyle extends ConfigEntityBase { */ public $effects; - /** * Overrides Drupal\Core\Entity\Entity::id(). */ diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php index fc964ac..7ad5d46 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php @@ -17,7 +17,6 @@ * image_style_create_derivative() * * image.module: - * image_style_delete() * image_style_options() * image_style_flush() * image_effect_definition_load() diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 570a199..0ded8b2 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2907,15 +2907,24 @@ function user_node_load($nodes, $types) { /** * Implements hook_image_style_delete(). + * + * @todo Obsolete with http://drupal.org/node/1292470 */ function user_image_style_delete($style) { - // If a style is deleted, update the variables. - // Administrators choose a replacement style when deleting. - user_image_style_save($style); + // Update the variable, in case a replacement style has been specified. + if ($style->id() != $style->getOriginalID()) { + user_image_style_save($style); + } + // Otherwise, user pictures are left in a broken state. + else { + variable_del('user_picture_style'); + } } /** * Implements hook_image_style_save(). + * + * @todo Obsolete with http://drupal.org/node/1292470 */ function user_image_style_save($style) { // If a style is renamed, update the variables that use it.