Commit 4365f507 authored by catch's avatar catch
Browse files

Issue #1782244 by heyrocker, andypost, tstoeckler: Convert image styles array...

Issue #1782244 by heyrocker, andypost, tstoeckler: Convert image styles  array into ImageStyle (extends ConfigEntity).
parent 856e8f75
......@@ -11,7 +11,7 @@
function image_style_list() {
$page = array();
$styles = image_styles();
$styles = entity_load_multiple('image_style');
$page['image_style_list'] = array(
'#markup' => theme('image_style_list', array('styles' => $styles)),
'#attached' => array(
......@@ -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'];
image_style_save($style);
$style->set('label', $form_state['values']['label']);
$style->save();
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);
$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();
}
/**
......@@ -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 .= '<div class="preview-image-wrapper">';
$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 .= '<div class="preview-image modified-image" style="' . $preview_attributes['style'] . '">';
$output .= '<a href="' . file_create_url($preview_file) . '?' . time() . '">' . theme('image', array('uri' => $preview_url, 'alt' => t('Sample modified image'), 'title' => '', 'attributes' => $preview_attributes)) . '</a>';
$output .= '<div class="height" style="height: ' . $preview_height . 'px"><span>' . $preview_image['height'] . 'px</span></div>';
......
......@@ -65,43 +65,6 @@ function hook_image_effect_info_alter(&$effects) {
$effects['image_crop']['form callback'] = 'mymodule_crop_form';
}
/**
* Respond to image style updating.
*
* This hook enables modules to update settings that might be affected by
* changes to an image. For example, updating a module specific variable to
* reflect a change in the image style's name.
*
* @param $style
* The image style array that is being updated.
*/
function hook_image_style_save($style) {
// If a module defines an image style and that style is renamed by the user
// the module should update any references to that style.
if (isset($style['old_name']) && $style['old_name'] == variable_get('mymodule_image_style', '')) {
variable_set('mymodule_image_style', $style['name']);
}
}
/**
* Respond to image style deletion.
*
* This hook enables modules to update settings when a image style is being
* deleted. If a style is deleted, a replacement name may be specified in
* $style['name'] and the style being deleted will be specified in
* $style['old_name'].
*
* @param $style
* The image style array that being deleted.
*/
function hook_image_style_delete($style) {
// Administrators can choose an optional replacement style when deleting.
// Update the modules style variable accordingly.
if (isset($style['old_name']) && $style['old_name'] == variable_get('mymodule_image_style', '')) {
variable_set('mymodule_image_style', $style['name']);
}
}
/**
* Respond to image style flushing.
*
......@@ -111,7 +74,7 @@ function hook_image_style_delete($style) {
* be cleared using this hook. This hook is called whenever a style is updated,
* deleted, or any effect associated with the style is update or deleted.
*
* @param $style
* @param Drupal\image\ImageStyle $style
* The image style array that is being flushed.
*/
function hook_image_style_flush($style) {
......@@ -119,34 +82,6 @@ function hook_image_style_flush($style) {
cache('mymodule')->flush();
}
/**
* 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.
*/
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,
'effect callback' => 'image_desaturate_effect',
);
}
}
/**
* @} End of "addtogroup hooks".
*/
......@@ -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) {
......@@ -360,6 +361,8 @@ function image_image_style_save($style) {
*/
function image_image_style_delete($style) {
image_image_style_save($style);
// Flush cached media for the style.
image_style_flush($style);
}
/**
......@@ -488,9 +491,9 @@ function image_field_update_instance($instance, $prior_instance) {
* The Drupal file path to the original image.
*/
function image_path_flush($path) {
$styles = image_styles();
$styles = entity_load_multiple('image_style');
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);
}
......@@ -498,7 +501,7 @@ function image_path_flush($path) {
}
/**
* Implements MODULE_config_import_create().
* Implements hook_config_import_create().
*/
function image_config_import_create($name, $new_config, $old_config) {
// Only image styles require custom handling. Any other module settings can be
......@@ -506,12 +509,13 @@ 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());
$style->save();
return TRUE;
}
/**
* Implements MODULE_config_import_change().
* Implements hook_config_import_change().
*/
function image_config_import_change($name, $new_config, $old_config) {
// Only image styles require custom handling. Any other module settings can be
......@@ -519,8 +523,21 @@ function image_config_import_change($name, $new_config, $old_config) {
if (strpos($name, 'image.style.') !== 0) {
return FALSE;
}
$style = $new_config->get();
return image_style_save($style);
list(, , $id) = explode('.', $name);
$style = entity_load('image_style', $id);
$style->original = clone $style;
foreach ($old_config->get() as $property => $value) {
$style->original->$property = $value;
}
foreach ($new_config->get() as $property => $value) {
$style->$property = $value;
}
$style->save();
return TRUE;
}
/**
......@@ -537,159 +554,70 @@ 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();
list(, , $id) = explode('.', $name);
$style = entity_load('image_style', $id);
return image_style_delete($style);
}
/**
* Get an array of all styles and their settings.
* Loads an ImageStyle object.
*
* @return
* An array of styles keyed by the image style ID (isid).
* @see image_style_load()
* @param string $name
* The ID of the ImageStyle object to 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;
function image_style_load($name) {
return entity_load('image_style', $name);
}
/**
* Load a style by style name or ID. May be used as a loader for menu items.
*
* @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()
* Implements hook_image_style_load.
*/
function image_style_load($name) {
$config = config('image.style.' . $name);
if ($config->isNew()) {
return FALSE;
}
$style = $config->get();
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;
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;
}
/**
* Save an image style.
*
* @param array $style
* An image style array containing:
* - name: An unique name for the style.
* - effects: An optional array of effects.
*
* @return array
* An image style array containing:
* - name: An unique name for the style.
* - effects: An array of effects.
* - is_new: Is set to TRUE if this is a new style, and FALSE if it is an
* existing style.
* Implements hook_image_style_update().
*/
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();
// Let other modules update as necessary on save.
$style['is_new'] = $is_new;
module_invoke_all('image_style_save', $style);