From 8445043c36e00bf364dc753daa4bd8058f5f9349 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Wed, 9 Jan 2013 18:26:20 -0800
Subject: [PATCH] Issue #1782244 follow-up by sun, znerol: Fixed Convert image
 styles  array into ImageStyle (extends ConfigEntity).

---
 core/modules/image/image.admin.inc            |  33 ++----
 core/modules/image/image.module               | 111 ++++--------------
 .../image/Plugin/Core/Entity/ImageStyle.php   |  10 +-
 .../Drupal/image/Tests/ImageFieldTestBase.php |   1 -
 4 files changed, 41 insertions(+), 114 deletions(-)

diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc
index 933a4ade3c3b..851b00999336 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 556467bf55c4..b9ad53c45b26 100644
--- a/core/modules/image/image.module
+++ b/core/modules/image/image.module
@@ -71,6 +71,15 @@ function image_help($path, $arg) {
   }
 }
 
+/**
+ * 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().
  */
@@ -326,9 +335,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.
@@ -361,10 +370,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);
+  }
 }
 
 /**
@@ -529,38 +545,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\Plugin\Core\Entity\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.
  *
@@ -766,12 +750,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();
@@ -905,43 +883,6 @@ function image_effect_definition_load($effect) {
   return isset($definitions[$effect]) ? $definitions[$effect] : FALSE;
 }
 
-/**
- * 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.
  *
@@ -991,6 +932,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();
@@ -1106,11 +1051,3 @@ function _image_effect_definitions_sort($a, $b) {
   return strcasecmp($a['name'], $b['name']);
 }
 
-/**
- * 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/Plugin/Core/Entity/ImageStyle.php b/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php
index 8110b7767738..eda9f5ca697e 100644
--- a/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php
+++ b/core/modules/image/lib/Drupal/image/Plugin/Core/Entity/ImageStyle.php
@@ -19,7 +19,7 @@
  *   label = @Translation("Image style"),
  *   module = "image",
  *   controller_class = "Drupal\image\ImageStyleStorageController",
- *   uri_callback = "image_style_uri",
+ *   uri_callback = "image_style_entity_uri",
  *   config_prefix = "image.style",
  *   entity_keys = {
  *     "id" = "name",
@@ -30,6 +30,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.
    *
@@ -51,7 +58,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 439db01ebc80..760e72d70f7c 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()
-- 
GitLab