diff --git a/core/modules/image/image.admin.inc b/core/modules/image/image.admin.inc index a2d86af..9742240 100644 --- a/core/modules/image/image.admin.inc +++ b/core/modules/image/image.admin.inc @@ -34,7 +34,7 @@ function image_style_list() { * @see image_style_form_submit() */ function image_style_form($form, &$form_state, $style) { - $title = t('Edit style %name', array('%name' => $style['label'])); + $title = t('Edit style %name', array('%name' => $style->label())); drupal_set_title($title, PASS_THROUGH); $form_state['image_style'] = $style; @@ -51,12 +51,12 @@ function image_style_form($form, &$form_state, $style) { $form['label'] = array( '#type' => 'textfield', '#title' => t('Administrative label'), - '#default_value' => $style['label'], + '#default_value' => $style->label(), '#required' => TRUE, ); $form['name'] = array( '#type' => 'machine_name', - '#default_value' => $style['name'], + '#default_value' => $style->id(), '#machine_name' => array( 'exists' => 'image_style_load', 'source' => array('label'), @@ -68,8 +68,8 @@ function image_style_form($form, &$form_state, $style) { $form['effects'] = array( '#theme' => 'image_style_effects', ); - if (!empty($style['effects'])) { - foreach ($style['effects'] as $key => $effect) { + if (!empty($style->effects)) { + foreach ($style->effects as $key => $effect) { $form['effects'][$key]['#weight'] = isset($form_state['input']['effects']) ? $form_state['input']['effects'][$key]['weight'] : NULL; $form['effects'][$key]['label'] = array( '#markup' => $effect['label'], @@ -88,12 +88,12 @@ function image_style_form($form, &$form_state, $style) { if (isset($effect['form callback'])) { $links['edit'] = array( 'title' => t('edit'), - 'href' => 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $key, + 'href' => 'admin/config/media/image-styles/edit/' . $style->id() . '/effects/' . $key, ); } $links['delete'] = array( 'title' => t('delete'), - 'href' => 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $key . '/delete', + 'href' => 'admin/config/media/image-styles/edit/' . $style->id() . '/effects/' . $key . '/delete', ); $form['effects'][$key]['operations'] = array( '#type' => 'operations', @@ -102,13 +102,13 @@ function image_style_form($form, &$form_state, $style) { $form['effects'][$key]['configure'] = array( '#type' => 'link', '#title' => t('edit'), - '#href' => 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $key, + '#href' => 'admin/config/media/image-styles/edit/' . $style->id() . '/effects/' . $key, '#access' => isset($effect['form callback']), ); $form['effects'][$key]['remove'] = array( '#type' => 'link', '#title' => t('delete'), - '#href' => 'admin/config/media/image-styles/edit/' . $style['name'] . '/effects/' . $key . '/delete', + '#href' => 'admin/config/media/image-styles/edit/' . $style->id() . '/effects/' . $key . '/delete', ); } } @@ -171,7 +171,7 @@ function image_style_form_add_submit($form, &$form_state) { // Load the configuration form for this option. if (isset($effect['form callback'])) { - $path = 'admin/config/media/image-styles/edit/' . $form_state['image_style']['name'] . '/add/' . $form_state['values']['new']; + $path = 'admin/config/media/image-styles/edit/' . $style->id() . '/add/' . $form_state['values']['new']; $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight']))); } // If there's no form, immediately add the image effect. @@ -181,7 +181,7 @@ function image_style_form_add_submit($form, &$form_state) { 'data' => array(), 'weight' => $form_state['values']['weight'], ); - image_effect_save($style['name'], $effect); + image_effect_save($style, $effect); drupal_set_message(t('The image effect was successfully applied.')); } } @@ -195,40 +195,38 @@ function image_style_form_submit($form, &$form_state) { // Update image effect weights. if (!empty($form_state['values']['effects'])) { foreach ($form_state['values']['effects'] as $ieid => $effect_data) { - if (isset($style['effects'][$ieid])) { + if (isset($style->effects[$ieid])) { $effect = array( - 'name' => $style['effects'][$ieid]['name'], - 'data' => $style['effects'][$ieid]['data'], + 'name' => $style->effects[$ieid]['name'], + 'data' => $style->effects[$ieid]['data'], 'weight' => $effect_data['weight'], 'ieid' => $ieid, ); - $style['effects'][$ieid] = $effect; + $style->effects[$ieid] = $effect; } } } - // Update the image style name if it has changed. We also need to delete the - // old style, because there is no concept of rename at the moment, just - // create and delete. - // @todo The config API does not yet support the concept of a rename, but - // hooks need to be able to change old style name references to the new - // name, so first save the new style, then delete the old, so that the - // delete function can receive the name of a fully saved style to update - // references to. - if (isset($form_state['values']['name']) && $style['name'] != $form_state['values']['name']) { + // 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; - $style['name'] = $form_state['values']['name']; + $data = array( + 'effects' => $style->effects, + 'name' => $form_state['values']['name'], + ); + $style = entity_create('image_style', $data); } - $style['label'] = $form_state['values']['label']; + $style->set('label', $form_state['values']['label']); image_style_save($style); if (isset($old_style)) { - image_style_delete($old_style, $style['name']); + image_style_delete($old_style, $style->id()); } if ($form_state['values']['op'] == t('Update style')) { drupal_set_message(t('Changes to the style have been saved.')); } - $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name']; + $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style->id(); } /** @@ -270,9 +268,10 @@ function image_style_add_form_submit($form, &$form_state) { '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['label']))); - $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name']; + $style = entity_create('image_style', $style); + image_style_save($style); + drupal_set_message(t('Style %name was created.', array('%name' => $style->label()))); + $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style->id(); } /** @@ -287,7 +286,7 @@ function image_style_add_form_submit($form, &$form_state) { function image_style_delete_form($form, &$form_state, $style) { $form_state['image_style'] = $style; - $replacement_styles = array_diff_key(image_style_options(), array($style['name'] => '')); + $replacement_styles = array_diff_key(image_style_options(), array($style->id() => '')); $form['replacement'] = array( '#title' => t('Replacement style'), '#type' => 'select', @@ -297,7 +296,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['label'])), + 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') @@ -311,7 +310,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['label']))); + drupal_set_message(t('Style %name was deleted.', array('%name' => $style->label()))); $form_state['redirect'] = 'admin/config/media/image-styles'; } @@ -342,7 +341,7 @@ function image_effect_form($form, &$form_state, $style, $effect) { // If there's no configuration for this image effect, return to // the image style page. if (!isset($effect['form callback'])) { - drupal_goto('admin/config/media/image-styles/edit/' . $style['name']); + drupal_goto('admin/config/media/image-styles/edit/' . $style->id()); } $form_state['image_style'] = $style; $form_state['image_effect'] = $effect; @@ -372,7 +371,7 @@ function image_effect_form($form, &$form_state, $style, $effect) { // Check the URL for a weight, then the image effect, otherwise use default. $form['weight'] = array( '#type' => 'hidden', - '#value' => isset($_GET['weight']) ? intval($_GET['weight']) : (isset($effect['weight']) ? $effect['weight'] : count($style['effects'])), + '#value' => isset($_GET['weight']) ? intval($_GET['weight']) : (isset($effect['weight']) ? $effect['weight'] : count($style->effects)), ); $form['actions'] = array('#type' => 'actions'); @@ -383,7 +382,7 @@ function image_effect_form($form, &$form_state, $style, $effect) { $form['actions']['cancel'] = array( '#type' => 'link', '#title' => t('Cancel'), - '#href' => 'admin/config/media/image-styles/edit/' . $style['name'], + '#href' => 'admin/config/media/image-styles/edit/' . $style->id(), ); return $form; @@ -396,10 +395,11 @@ function image_effect_form_submit($form, &$form_state) { form_state_values_clean($form_state); $effect = $form_state['values']; - image_effect_save($form_state['image_style']['name'], $effect); + $style = $form_state['image_style']; + image_effect_save($style, $effect); drupal_set_message(t('The image effect was successfully applied.')); - $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $form_state['image_style']['name']; + $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style->id(); } /** @@ -416,8 +416,8 @@ 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['label'], '@effect' => $effect['label'])); - return confirm_form($form, $question, 'admin/config/media/image-styles/edit/' . $style['name'], '', t('Delete')); + $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->id(), '', t('Delete')); } /** @@ -427,9 +427,9 @@ function image_effect_delete_form_submit($form, &$form_state) { $style = $form_state['image_style']; $effect = $form_state['image_effect']; - image_effect_delete($style['name'], $effect); + image_effect_delete($style, $effect); drupal_set_message(t('The image effect %name has been deleted.', array('%name' => $effect['label']))); - $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style['name']; + $form_state['redirect'] = 'admin/config/media/image-styles/edit/' . $style->id(); } /** @@ -600,18 +600,19 @@ function theme_image_style_list($variables) { $header = array(t('Style name'), t('Operations')); $rows = array(); + foreach ($styles as $style) { $row = array(); - $row[] = l($style['label'], 'admin/config/media/image-styles/edit/' . $style['name']); + $row[] = l($style->label(), 'admin/config/media/image-styles/edit/' . $style->id()); $links = array(); $links['edit'] = array( 'title' => t('edit'), - 'href' => 'admin/config/media/image-styles/edit/' . $style['name'], + 'href' => 'admin/config/media/image-styles/edit/' . $style->id(), 'class' => array('image-style-link'), ); $links['delete'] = array( 'title' => t('delete'), - 'href' => 'admin/config/media/image-styles/delete/' . $style['name'], + 'href' => 'admin/config/media/image-styles/delete/' . $style->id(), 'class' => array('image-style-link'), ); $row[] = array( @@ -717,7 +718,7 @@ function theme_image_style_preview($variables) { $original_attributes['style'] = 'width: ' . $original_width . 'px; height: ' . $original_height . 'px;'; // Set up preview file information. - $preview_file = image_style_path($style['name'], $original_path); + $preview_file = image_style_path($style->id(), $original_path); if (!file_exists($preview_file)) { image_style_create_derivative($style, $original_path, $preview_file); } @@ -750,7 +751,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['label']) . ' (' . 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('uri' => $preview_url, 'alt' => t('Sample modified image'), 'title' => '', 'attributes' => $preview_attributes)) . ''; $output .= '
' . $preview_image['height'] . 'px
'; diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 01d2b3b..de90eb7 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -10,6 +10,7 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Drupal\Component\Uuid\Uuid; use Drupal\file\File; +use Drupal\image\ImageStyle; /** * Image style constant for user presets in the database. @@ -328,7 +329,7 @@ function image_file_predelete(File $file) { * Implements hook_image_style_save(). */ function image_image_style_save($style) { - if (isset($style['old_name']) && $style['old_name'] != $style['name']) { + if ($style->id() != $style->getOriginalID()) { $instances = field_read_instances(); // Loop through all fields searching for image fields. foreach ($instances as $instance) { @@ -336,15 +337,15 @@ function image_image_style_save($style) { $instance_changed = FALSE; foreach ($instance['display'] as $view_mode => $display) { // Check if the formatter involves an image style. - if ($display['type'] == 'image' && $display['settings']['image_style'] == $style['old_name']) { + if ($display['type'] == 'image' && $display['settings']['image_style'] == $style->getOriginalID()) { // Update display information for any instance using the image // style that was just deleted. - $instance['display'][$view_mode]['settings']['image_style'] = $style['name']; + $instance['display'][$view_mode]['settings']['image_style'] = $style->id(); $instance_changed = TRUE; } } - if ($instance['widget']['settings']['preview_image_style'] == $style['old_name']) { - $instance['widget']['settings']['preview_image_style'] = $style['name']; + if ($instance['widget']['settings']['preview_image_style'] == $style->getOriginalID()) { + $instance['widget']['settings']['preview_image_style'] = $style->id(); $instance_changed = TRUE; } if ($instance_changed) { @@ -490,7 +491,7 @@ function image_field_update_instance($instance, $prior_instance) { function image_path_flush($path) { $styles = image_styles(); foreach ($styles as $style) { - $image_path = image_style_path($style['name'], $path); + $image_path = image_style_path($style->id(), $path); if (file_exists($image_path)) { file_unmanaged_delete($image_path); } @@ -506,8 +507,9 @@ function image_config_import_create($name, $new_config, $old_config) { if (strpos($name, 'image.style.') !== 0) { return FALSE; } - $style = $new_config->get(); - return image_style_save($style); + $style = entity_create('image_style', $new_config->get()); + image_style_save($style); + return TRUE; } /** @@ -519,7 +521,7 @@ function image_config_import_change($name, $new_config, $old_config) { if (strpos($name, 'image.style.') !== 0) { return FALSE; } - $style = $new_config->get(); + $style = image_style_load($new_config->get('name')); return image_style_save($style); } @@ -537,7 +539,7 @@ function image_config_import_delete($name, $new_config, $old_config) { // 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? - $style = $old_config->get(); + $style = image_style_load($old_config->get('name')); return image_style_delete($style); } @@ -546,67 +548,37 @@ function image_config_import_delete($name, $new_config, $old_config) { * * @return * An array of styles keyed by the image style ID (isid). - * @see image_style_load() */ function image_styles() { - // @todo Configuration must not be statically cached nor cache-system cached. - // However, there's a drupal_alter() involved here. - -// $styles = &drupal_static(__FUNCTION__); -// -// // Grab from cache or build the array. -// if (!isset($styles)) { -// if ($cache = cache()->get('image_styles')) { -// $styles = $cache->data; -// } -// else { - $styles = array(); - - // Select the styles we have configured. - $configured_styles = config_get_storage_names_with_prefix('image.style'); - foreach ($configured_styles as $config_name) { - // @todo Allow to retrieve the name without prefix only. - $style = image_style_load(str_replace('image.style.', '', $config_name)); - $styles[$style['name']] = $style; - } - drupal_alter('image_styles', $styles); -// cache()->set('image_styles', $styles); -// } -// } - - return $styles; + $controller = entity_list_controller('image_style'); + return $controller->load(); } /** - * Load a style by style name or ID. May be used as a loader for menu items. + * Loads an ImageStyle object. * - * @param $name - * The name of the style. - * @return - * An image style array containing the following keys: - * - "name": The unique image style name. - * - "effects": An array of image effects within this image style. - * If the image style name is not valid, an empty array is returned. - * @see image_effect_load() + * @param string $id + * The ID of the ImageStyle object to load. */ function image_style_load($name) { - $config = config('image.style.' . $name); - if ($config->isNew()) { - return FALSE; - } - $style = $config->get(); + return entity_load('image_style', $name); +} - if (!empty($style['effects'])) { - foreach ($style['effects'] as $ieid => $effect) { - $definition = image_effect_definition_load($effect['name']); - $effect = array_merge($definition, $effect); - $style['effects'][$ieid] = $effect; +/** + * Implements hook_image_style_load. + */ +function image_image_style_load($styles) { + foreach ($styles as $style) { + if (!empty($style->effects)) { + foreach ($style->effects as $ieid => $effect) { + $definition = image_effect_definition_load($effect['name']); + $effect = array_merge($definition, $effect); + $style->effects[$ieid] = $effect; + } + // Sort effects by weight. + uasort($style->effects, 'drupal_sort_weight'); } - // Sort effects by weight. - uasort($style['effects'], 'drupal_sort_weight'); } - - return $style; } /** @@ -625,22 +597,8 @@ function image_style_load($name) { * existing style. */ function image_style_save($style) { - $config = config('image.style.' . $style['name']); - $is_new = $config->isNew(); - - $config - ->set('name', $style['name']) - ->set('label', $style['label']); - if (isset($style['effects'])) { - $config->set('effects', $style['effects']); - } - else { - $config->set('effects', array()); - } - $config->save(); + $style->save(); - // Let other modules update as necessary on save. - $style['is_new'] = $is_new; module_invoke_all('image_style_save', $style); // Clear all caches and flush. @@ -663,34 +621,18 @@ function image_style_save($style) { function image_style_delete($style, $replacement_style_name = '') { image_style_flush($style); - $config = config('image.style.' . $style['name']); - $config->delete(); + $style->delete(); // Let other modules update as necessary on save. - $style['old_name'] = $style['name']; - $style['name'] = $replacement_style_name; + if ($replacement_style_name) { + $style->set('name', $replacement_style_name); + } module_invoke_all('image_style_delete', $style); return TRUE; } /** - * Load all the effects for an image style. - * - * @param $style - * An image style array. - * @return - * An array of image effects associated with specified image style in the - * format array('isid' => array()), or an empty array if the specified style - * has no effects. - * - * @todo Remove this function; it's entirely obsolete. - */ -function image_style_effects($style) { - return $style['effects']; -} - -/** * Get an array of image styles suitable for using as select list options. * * @param $include_empty @@ -705,7 +647,7 @@ function image_style_options($include_empty = TRUE) { $options[''] = t(''); } foreach ($styles as $name => $style) { - $options[$name] = $style['label']; + $options[$name] = $style->label(); } if (empty($options)) { @@ -734,7 +676,7 @@ function image_style_deliver($style, $scheme) { $target = implode('/', $args); $image_uri = $scheme . '://' . $target; - $derivative_uri = image_style_path($style['name'], $image_uri); + $derivative_uri = image_style_path($style->id(), $image_uri); // If using the private scheme, let other modules provide headers and // control access to the file. @@ -763,7 +705,7 @@ function image_style_deliver($style, $scheme) { // Don't start generating the image if the derivative already exists or if // generation is in progress in another thread. - $lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($image_uri); + $lock_name = 'image_style_deliver:' . $style->id() . ':' . drupal_hash_base64($image_uri); if (!file_exists($derivative_uri)) { $lock_acquired = lock()->acquire($lock_name); if (!$lock_acquired) { @@ -803,7 +745,7 @@ function image_style_deliver($style, $scheme) { * Creates a new image derivative based on an image style. * * Generates an image derivative by creating the destination folder (if it does - * not already exist), applying all image effects defined in $style['effects'], + * not already exist), applying all image effects defined in $style->effects, * and saving a cached version of the resulting image. * * @param $style @@ -833,8 +775,8 @@ function image_style_create_derivative($style, $source, $destination) { return FALSE; } - if (!empty($style['effects'])) { - foreach ($style['effects'] as $effect) { + if (!empty($style->effects)) { + foreach ($style->effects as $effect) { image_effect_apply($image, $effect); } } @@ -864,12 +806,8 @@ function image_style_transform_dimensions($style_name, array &$dimensions) { module_load_include('inc', 'image', 'image.effects'); $style = image_style_load($style_name); - if (!is_array($style)) { - return; - } - - if (!empty($style['effects'])) { - foreach ($style['effects'] as $effect) { + if (!empty($style->effects)) { + foreach ($style->effects as $effect) { if (isset($effect['dimensions passthrough'])) { continue; } @@ -891,7 +829,7 @@ function image_style_transform_dimensions($style_name, array &$dimensions) { * An image style array. */ function image_style_flush($style) { - $style_directory = drupal_realpath(file_default_scheme() . '://styles/' . $style['name']); + $style_directory = drupal_realpath(file_default_scheme() . '://styles/' . $style->id()); if (is_dir($style_directory)) { file_unmanaged_delete_recursive($style_directory); } @@ -1018,7 +956,7 @@ function image_effect_definitions() { * containing any user-settings. The definition defines various functions to * call when configuring or executing an image effect. This loader is mostly for * internal use within image.module. Use image_effect_load() or - * image_style_load() to get image effects that contain configuration. + * entity_load() to get image effects that contain configuration. * * @param $effect * The name of the effect definition to load. @@ -1092,12 +1030,11 @@ function image_effects() { * Besides these keys, the entirety of the image definition is merged into * the image effect array. Returns FALSE if the specified effect cannot be * found. - * @see image_style_load() * @see image_effect_definition_load() */ function image_effect_load($ieid, $style_name) { - if (($style = image_style_load($style_name)) && isset($style['effects'][$ieid])) { - $effect = $style['effects'][$ieid]; + if (($style = image_style_load($style_name)) && isset($style->effects[$ieid])) { + $effect = $style->effects[$ieid]; $definition = image_effect_definition_load($effect['name']); $effect = array_merge($definition, $effect); // @todo The effect's key name within the style is unknown. It *should* be @@ -1116,7 +1053,7 @@ function image_effect_load($ieid, $style_name) { /** * Save an image effect. * - * @param string $style_name + * @param ImageStyle $style * The image style this effect belongs to. * @param array $effect * An image effect array. Passed by reference. @@ -1124,38 +1061,31 @@ function image_effect_load($ieid, $style_name) { * @return array * The saved image effect array. The 'ieid' key will be set for the effect. */ -function image_effect_save($style_name, &$effect) { - $config = config('image.style.' . $style_name); - +function image_effect_save($style, &$effect) { // Generate a unique image effect ID for a new effect. if (empty($effect['ieid'])) { $uuid = new Uuid(); $effect['ieid'] = $uuid->generate(); } - $config->set('effects.' . $effect['ieid'], $effect); - $config->save(); + $style->effects[$effect['ieid']] = $effect; + image_style_save($style); // Flush all derivatives that exist for this style, so they are regenerated // with the new or updated effect. - $style = image_style_load($style_name); image_style_flush($style); - - return $effect; } /** * Delete an image effect. * - * @param $style_name + * @param ImageStyle $style * The image style this effect belongs to. * @param $effect * An image effect array. */ -function image_effect_delete($style_name, $effect) { - $config = config('image.style.' . $style_name); - $config->clear('effects.' . $effect['ieid']); - $config->save(); - $style = image_style_load($style_name); +function image_effect_delete($style, $effect) { + unset($style->effects['ieid']); + image_style_save($style); image_style_flush($style); } @@ -1246,3 +1176,32 @@ function image_filter_keyword($value, $current_pixels, $new_pixels) { 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->name, + ); +} diff --git a/core/modules/image/lib/Drupal/image/ImageStyle.php b/core/modules/image/lib/Drupal/image/ImageStyle.php new file mode 100644 index 0000000..13efa74 --- /dev/null +++ b/core/modules/image/lib/Drupal/image/ImageStyle.php @@ -0,0 +1,45 @@ +name; + } + +} diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php index 7437b10..d27c910 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageAdminStylesTest.php @@ -34,14 +34,14 @@ function createSampleImage($style) { $file_path = file_unmanaged_copy($file->uri); } - return image_style_url($style['name'], $file_path) ? $file_path : FALSE; + return image_style_url($style->id(), $file_path) ? $file_path : FALSE; } /** * Count the number of images currently create for a style. */ function getImageCount($style) { - return count(file_scan_directory('public://styles/' . $style['name'], '/.*/')); + return count(file_scan_directory('public://styles/' . $style->id(), '/.*/')); } /** @@ -124,7 +124,7 @@ function testStyle() { drupal_static_reset('image_styles'); $style = image_style_load($style_name); - foreach ($style['effects'] as $ieid => $effect) { + foreach ($style->effects as $ieid => $effect) { $this->drupalGet($style_path . '/effects/' . $ieid); foreach ($effect_edits[$effect['name']] as $field => $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))); @@ -136,7 +136,7 @@ function testStyle() { // Confirm the order of effects is maintained according to the order we // added the fields. $effect_edits_order = array_keys($effect_edits); - $effects_order = array_values($style['effects']); + $effects_order = array_values($style->effects); $order_correct = TRUE; foreach ($effects_order as $index => $effect) { if ($effect_edits_order[$index] != $effect['name']) { @@ -154,14 +154,14 @@ function testStyle() { 'name' => $style_name, 'label' => $style_label, ); - foreach ($style['effects'] as $ieid => $effect) { + foreach ($style->effects as $ieid => $effect) { $edit['effects[' . $ieid . '][weight]'] = $weight; $weight--; } // Create an image to make sure it gets flushed after saving. $image_path = $this->createSampleImage($style); - $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%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')); @@ -170,12 +170,12 @@ function testStyle() { // Check that the URL was updated. $this->drupalGet($style_path); - $this->assertResponse(200, format_string('Image style %original renamed to %new', array('%original' => $style['label'], '%new' => $style_name))); + $this->assertResponse(200, format_string('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, format_string('Image style %style was flushed after renaming the style and updating the order of effects.', array('%style' => $style['label']))); + $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'); @@ -183,7 +183,7 @@ function testStyle() { // Confirm the new style order was saved. $effect_edits_order = array_reverse($effect_edits_order); - $effects_order = array_values($style['effects']); + $effects_order = array_values($style->effects); $order_correct = TRUE; foreach ($effects_order as $index => $effect) { if ($effect_edits_order[$index] != $effect['name']) { @@ -196,10 +196,10 @@ function testStyle() { // Create an image to make sure it gets flushed after deleting an effect. $image_path = $this->createSampleImage($style); - $this->assertEqual($this->getImageCount($style), 1, format_string('Image style %style image %file successfully generated.', array('%style' => $style['label'], '%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']); + $effect = array_pop($style->effects); $this->drupalPost($style_path . '/effects/' . $effect['ieid'] . '/delete', array(), t('Delete')); $this->assertRaw(t('The image effect %name has been deleted.', array('%name' => $effect['label']))); @@ -210,10 +210,10 @@ function testStyle() { // Confirm the style directory has been removed. $directory = file_default_scheme() . '://styles/' . $style_name; - $this->assertFalse(is_dir($directory), format_string('Image style %style directory removed on style deletion.', array('%style' => $style['label']))); + $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), format_string('Image style %style successfully deleted.', array('%style' => $style['label']))); + $this->assertFalse(image_style_load($style_name), format_string('Image style %style successfully deleted.', array('%style' => $style->label()))); } @@ -224,7 +224,8 @@ function testStyleReplacement() { // Create a new style. $style_name = strtolower($this->randomName(10)); $style_label = $this->randomString(); - image_style_save(array('name' => $style_name, 'label' => $style_label)); + $style = entity_create('image_style', array('name' => $style_name, 'label' => $style_label)); + image_style_save($style); $style_path = 'admin/config/media/image-styles/edit/' . $style_name; // Create an image field that uses the new style. diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php index c2734f0..96c3193 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php @@ -41,7 +41,8 @@ function testImageDimensions() { $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); // Create a style. - $style = image_style_save(array('name' => 'test', 'label' => 'Test')); + $style = entity_create('image_style', array('name' => 'test', 'label' => 'Test')); + image_style_save($style); $generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri); $url = image_style_url('test', $original_uri); @@ -67,7 +68,7 @@ function testImageDimensions() { 'weight' => 0, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -88,7 +89,7 @@ function testImageDimensions() { 'weight' => 1, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -110,7 +111,7 @@ function testImageDimensions() { 'weight' => 2, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -132,7 +133,7 @@ function testImageDimensions() { 'weight' => 3, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -150,7 +151,7 @@ function testImageDimensions() { 'weight' => 4, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -171,7 +172,7 @@ function testImageDimensions() { 'weight' => 5, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -191,7 +192,7 @@ function testImageDimensions() { 'weight' => 6, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -212,7 +213,7 @@ function testImageDimensions() { 'weight' => 7, ); - $effect = image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); @@ -220,7 +221,7 @@ function testImageDimensions() { $this->assertResponse(200, 'Image was generated at the URL.'); $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); - image_effect_delete('test', $effect); + image_effect_delete($style, $effect); // Ensure that an effect with no dimensions callback unsets the dimensions. // This ensures compatibility with 7.0 contrib modules. @@ -230,7 +231,7 @@ function testImageDimensions() { 'weight' => 8, ); - image_effect_save('test', $effect); + image_effect_save($style, $effect); $img_tag = theme_image_style($variables); $this->assertEqual($img_tag, ''); } diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php b/core/modules/image/lib/Drupal/image/Tests/ImageFieldTestBase.php index e78a364..1ace332 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_load() * image_style_save() * image_style_delete() * image_style_options() diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php index f38df1e..8efb1b5 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageStylesPathAndUrlTest.php @@ -37,7 +37,8 @@ function setUp() { parent::setUp(); $this->style_name = 'style_foo'; - image_style_save(array('name' => $this->style_name, 'label' => $this->randomString())); + $style = entity_create('image_style', array('name' => $this->style_name, 'label' => $this->randomString())); + image_style_save($style); } /** diff --git a/core/modules/image/lib/Drupal/image/Tests/ImageThemeFunctionTest.php b/core/modules/image/lib/Drupal/image/Tests/ImageThemeFunctionTest.php index 58faa03..9fc1a43 100644 --- a/core/modules/image/lib/Drupal/image/Tests/ImageThemeFunctionTest.php +++ b/core/modules/image/lib/Drupal/image/Tests/ImageThemeFunctionTest.php @@ -39,7 +39,8 @@ function testImageFormatterTheme() { $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); // Create a style. - image_style_save(array('name' => 'test', 'label' => 'Test')); + $style = entity_create('image_style', array('name' => 'test', 'label' => 'Test')); + image_style_save($style); $url = image_style_url('test', $original_uri); // Test using theme_image_formatter() without an image title, alt text, or @@ -81,7 +82,8 @@ function testImageStyleTheme() { $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); // Create a style. - image_style_save(array('name' => 'image_test', 'label' => 'Test')); + $style = entity_create('image_style', array('name' => 'image_test', 'label' => 'Test')); + image_style_save($style); $url = image_style_url('image_test', $original_uri); $path = $this->randomName(); diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php index ebd2a5d..b7ad2ff 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php @@ -32,7 +32,7 @@ function testNormal() { // Create derivative image. $style = image_style_load(key(image_styles())); - $derivative_uri = image_style_path($style['name'], $file->uri); + $derivative_uri = image_style_path($style->id(), $file->uri); image_style_create_derivative($style, $file->uri, $derivative_uri); // Check if derivative image exists. diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php index fc24813..23b0336 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/ImageUpgradePathTest.php @@ -73,11 +73,11 @@ public function testImageStyleUpgrade() { // during by the image style upgrade functions. foreach ($config->get('effects') as $uuid => $effect) { // Copy placeholder data. - $style['effects'][$uuid] = $style['effects'][$effect['name']]; + $style->effects[$uuid] = $style->effects[$effect['name']]; // Set the missing ieid key as this is unknown because it is a UUID. - $style['effects'][$uuid]['ieid'] = $uuid; + $style->effects[$uuid]['ieid'] = $uuid; // Remove the placeholder data. - unset($style['effects'][$effect['name']]); + unset($style->effects[$effect['name']]); } $this->assertEqual($this->sortByKey($style), $config->get(), format_string('@first is equal to @second.', array( '@first' => var_export($this->sortByKey($style), TRUE), diff --git a/core/modules/user/user.module b/core/modules/user/user.module index e42002b..4c952aa 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -2961,8 +2961,8 @@ function user_image_style_delete($style) { */ function user_image_style_save($style) { // If a style is renamed, update the variables that use it. - if (isset($style['old_name']) && $style['old_name'] == variable_get('user_picture_style', '')) { - variable_set('user_picture_style', $style['name']); + if ($style->id() != $style->getOriginalID()) { + variable_set('user_picture_style', $style->id()); } }