Skip to content
Snippets Groups Projects
Commit a0cb0b79 authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2049465 by niko-, andypost, mradcliffe, claudiu.cristea, LinL: Fixed...

Issue #2049465 by niko-, andypost, mradcliffe, claudiu.cristea, LinL: Fixed Upgrade of image styles and effects broken.
parent 2f0a4c42
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -58,7 +58,7 @@ function image_requirements($phase) { ...@@ -58,7 +58,7 @@ function image_requirements($phase) {
} }
/** /**
* Loads all effects for an image style. * Loads all effects for an image style from backend.
* *
* @param array $style * @param array $style
* The image style (array) to retrieve effects for. * The image style (array) to retrieve effects for.
...@@ -68,28 +68,118 @@ function image_requirements($phase) { ...@@ -68,28 +68,118 @@ function image_requirements($phase) {
* *
* @see image_update_8000() * @see image_update_8000()
*/ */
function _image_update_get_style_with_effects(array $style) { function _image_update_get_backend_effects(array $style) {
// Retrieve image effects.
$effects = array();
$result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC)) $result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('image_effects') ->fields('image_effects')
->condition('isid', $style['isid']) ->condition('isid', $style['isid'])
->execute(); ->execute();
$effects = array();
foreach ($result as $effect) { foreach ($result as $effect) {
$effect['data'] = unserialize($effect['data']); $effect['data'] = unserialize($effect['data']);
$effects[] = $effect;
}
return _image_update_convert_effects($effects);
}
/**
* Retuns the default image styles shipped with Drupal 7.
*
* @return array
* An associative array keyed by style id, having the style and associated
* effects as values. The array values are defined to be usable in
* image_update_8000().
*
* @see image_update_8000()
* @see https://api.drupal.org/api/drupal/modules%21image%21image.module/function/image_image_default_styles/7
*/
function _image_update_get_default_styles() {
// Clone from Drupal 7 image_image_default_styles().
$styles = array(
'thumbnail' => array(
'label' => 'Thumbnail (100x100)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => '100',
'height' => '100',
'upscale' => '1',
),
'weight' => '0',
),
),
),
'medium' => array(
'label' => 'Medium (220x220)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => '220',
'height' => '220',
'upscale' => '1',
),
'weight' => '0',
),
),
),
'large' => array(
'label' => 'Large (480x480)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => '480',
'height' => '480',
'upscale' => '0',
),
'weight' => '0',
),
),
),
);
// Add uuid, status, langcode and convert effects.
$langcode = language_default()->id;
$uuid = \Drupal::service('uuid');
foreach ($styles as $id => $style) {
$styles[$id]['name'] = $id;
$styles[$id]['uuid'] = $uuid->generate();
$styles[$id]['status'] = 1;
$styles[$id]['langcode'] = $langcode;
$styles[$id]['effects'] = _image_update_convert_effects($style['effects']);
}
return $styles;
}
// Generate a unique image effect ID for the effect. /**
* Normalizes effects from Drupal 7 to Drupal 8 format.
*
* @param array $legacy_effects
* An array containing a list of effects obtained from database or from code.
*
* @return array
* A list of effects keyed by effect uuid and having the effect definition in
* a Drupal 8 configuration format.
*/
function _image_update_convert_effects(array $legacy_effects) {
$uuid = \Drupal::service('uuid'); $uuid = \Drupal::service('uuid');
$effects = array();
foreach ($legacy_effects as $effect) {
$effect['uuid'] = $uuid->generate(); $effect['uuid'] = $uuid->generate();
// Use 'id' instead of 'name'. // Use 'id' instead of 'name'.
$effect['id'] = $effect['name']; $effect['id'] = $effect['name'];
// Clear out legacy keys. // Clear out legacy keys, if case.
unset($effect['isid'], $effect['ieid'], $effect['name']); unset($effect['isid'], $effect['ieid'], $effect['name']);
$effects[$effect['uuid']] = $effect; $effects[$effect['uuid']] = $effect;
} }
return $effects; return $effects;
} }
...@@ -97,22 +187,36 @@ function _image_update_get_style_with_effects(array $style) { ...@@ -97,22 +187,36 @@ function _image_update_get_style_with_effects(array $style) {
* Convert existing image styles to the new config system. * Convert existing image styles to the new config system.
*/ */
function image_update_8000() { function image_update_8000() {
$styles = array(); $langcode = language_default()->id;
$uuid = \Drupal::service('uuid');
$result = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC)) $result = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('image_styles') ->fields('image_styles')
->execute() ->execute();
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
foreach ($result as $style_name => $style) { // Prepare image styles from backend.
$style['effects'] = _image_update_get_style_with_effects($style); $styles = array();
$styles[$style_name] = $style; foreach ($result as $style) {
} $styles[$style['name']] = array(
'name' => $style['name'],
// Convert each style into a configuration object. 'label' => $style['label'],
foreach ($styles as $name => $style) { 'uuid' => $uuid->generate(),
$config = \Drupal::config('image.style.' . $name); 'status' => 1,
$config->set('name', $name); 'langcode' => $langcode,
$config->set('effects', $style['effects']); 'effects' => _image_update_get_backend_effects($style),
$config->save(); );
}
// Append Drupal 7 default image styles from code. Note that some of them may
// be overwritten in the backend. Using this array operator assures that we
// are migrating the overwritten version of the image style.
$styles += _image_update_get_default_styles();
// Save as Drupal 8 configuration.
foreach ($styles as $id => $style) {
\Drupal::config('image.style.' . $id)
->setData($style)
->save();
} }
} }
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\system\Tests\Upgrade; namespace Drupal\system\Tests\Upgrade;
use Drupal\Component\Utility\String;
/** /**
* Test upgrade of overridden and custom image styles. * Test upgrade of overridden and custom image styles.
*/ */
...@@ -33,9 +35,12 @@ public function setUp() { ...@@ -33,9 +35,12 @@ public function setUp() {
public function testImageStyleUpgrade() { public function testImageStyleUpgrade() {
$this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.');
// Verify that image styles were properly upgraded. // A custom user image style.
$expected_styles['test-custom'] = array( $expected_styles['test-custom'] = array(
'name' => 'test-custom', 'name' => 'test-custom',
'label' => 'Test custom',
'status' => '1',
'langcode' => 'en',
'effects' => array( 'effects' => array(
'image_rotate' => array( 'image_rotate' => array(
'id' => 'image_rotate', 'id' => 'image_rotate',
...@@ -53,8 +58,12 @@ public function testImageStyleUpgrade() { ...@@ -53,8 +58,12 @@ public function testImageStyleUpgrade() {
), ),
), ),
); );
// An image style shipped with Drupal 7 but overwritten by the user.
$expected_styles['thumbnail'] = array( $expected_styles['thumbnail'] = array(
'name' => 'thumbnail', 'name' => 'thumbnail',
'label' => 'Thumbnail (100x100)',
'status' => '1',
'langcode' => 'en',
'effects' => array ( 'effects' => array (
'image_scale' => array( 'image_scale' => array(
'id' => 'image_scale', 'id' => 'image_scale',
...@@ -67,11 +76,38 @@ public function testImageStyleUpgrade() { ...@@ -67,11 +76,38 @@ public function testImageStyleUpgrade() {
), ),
), ),
); );
// A non-overwritten image style. Has to be imported in Drupal 8 as well.
$expected_styles['large'] = array(
'name' => 'large',
'label' => 'Large (480x480)',
'status' => '1',
'langcode' => 'en',
'effects' => array (
'image_scale' => array(
'id' => 'image_scale',
'data' => array (
'width' => '480',
'height' => '480',
'upscale' => '0',
),
'weight' => '0',
),
),
);
$config_factory = $this->container->get('config.factory');
foreach ($expected_styles as $name => $style) { foreach ($expected_styles as $name => $style) {
$config = \Drupal::config('image.style.' . $name); $configured_style = $config_factory->get('image.style.' . $name)->get();
// Maybe the image style hasn't been imported?
$imported = !empty($configured_style);
$this->assertTrue($imported, String::format('%name has been imported', array('%name' => $style['label'])));
if (!$imported) {
continue;
}
// Replace placeholder with image effect name keys with UUID's generated // Replace placeholder with image effect name keys with UUID's generated
// during by the image style upgrade functions. // during by the image style upgrade functions.
foreach ($config->get('effects') as $uuid => $effect) { foreach ($configured_style['effects'] as $uuid => $effect) {
// Copy placeholder data. // Copy placeholder data.
$style['effects'][$uuid] = $style['effects'][$effect['id']]; $style['effects'][$uuid] = $style['effects'][$effect['id']];
// Set the missing uuid key as this is unknown because it is a UUID. // Set the missing uuid key as this is unknown because it is a UUID.
...@@ -79,12 +115,14 @@ public function testImageStyleUpgrade() { ...@@ -79,12 +115,14 @@ public function testImageStyleUpgrade() {
// Remove the placeholder data. // Remove the placeholder data.
unset($style['effects'][$effect['id']]); unset($style['effects'][$effect['id']]);
} }
$this->assertEqual($this->sortByKey($style), $config->get(), format_string('@first is equal to @second.', array( // Make sure UUID assigned to new style.
'@first' => var_export($this->sortByKey($style), TRUE), $this->assertTrue($configured_style['uuid'], 'UUID assigned to converted style.');
'@second' => var_export($config->get(), TRUE), // Copy generated UUID to compared style.
))); $style['uuid'] = $configured_style['uuid'];
$this->assertEqual($this->sortByKey($style), $this->sortByKey($configured_style));
} }
} }
/** /**
* Sorts all keys in configuration data. * Sorts all keys in configuration data.
* *
...@@ -102,7 +140,7 @@ public function sortByKey(array $data) { ...@@ -102,7 +140,7 @@ public function sortByKey(array $data) {
ksort($data); ksort($data);
foreach ($data as &$value) { foreach ($data as &$value) {
if (is_array($value)) { if (is_array($value)) {
$this->sortByKey($value); $value = $this->sortByKey($value);
} }
} }
return $data; return $data;
......
...@@ -14,16 +14,19 @@ ...@@ -14,16 +14,19 @@
db_insert('image_styles')->fields(array( db_insert('image_styles')->fields(array(
'isid', 'isid',
'name', 'name',
'label'
)) ))
// Override thumbnail style. // Override thumbnail style.
->values(array( ->values(array(
'isid' => '1', 'isid' => '1',
'name' => 'thumbnail', 'name' => 'thumbnail',
'label' => 'Thumbnail (100x100)',
)) ))
// Custom style. // Custom style.
->values(array( ->values(array(
'isid' => '2', 'isid' => '2',
'name' => 'test-custom', 'name' => 'test-custom',
'label' => 'Test custom',
)) ))
->execute(); ->execute();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment