Skip to content

Commit 6ca1792

Browse files
author
Nathaniel Catchpole
committed
Issue #2975539 by mondrake, alexpott, marcoscano, desierto: Changing machine name of image style leads to WSOD when loading widgets that used the old name
(cherry picked from commit 21b62c6)
1 parent 61a300c commit 6ca1792

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

modules/image/src/Entity/ImageStyle.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\Cache\Cache;
66
use Drupal\Core\Config\Entity\ConfigEntityBase;
7+
use Drupal\Core\Entity\Entity\EntityFormDisplay;
78
use Drupal\Core\Entity\EntityStorageInterface;
89
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
910
use Drupal\Core\Routing\RequestHelper;
@@ -154,7 +155,7 @@ protected static function replaceImageStyle(ImageStyleInterface $style) {
154155
}
155156
}
156157
}
157-
foreach (EntityViewDisplay::loadMultiple() as $display) {
158+
foreach (EntityFormDisplay::loadMultiple() as $display) {
158159
foreach ($display->getComponents() as $name => $options) {
159160
if (isset($options['type']) && $options['type'] == 'image_image' && $options['settings']['preview_image_style'] == $style->getOriginalId()) {
160161
$options['settings']['preview_image_style'] = $style->id();

modules/image/tests/src/Kernel/ImageStyleIntegrationTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,70 @@ public function testEntityDisplayDependency() {
109109
$this->assertSame('', $widget['settings']['preview_image_style']);
110110
}
111111

112+
/**
113+
* Tests renaming the ImageStyle.
114+
*/
115+
public function testEntityDisplayDependencyRename() {
116+
// Create an image style.
117+
/** @var \Drupal\image\ImageStyleInterface $style */
118+
$style = ImageStyle::create(['name' => 'main_style']);
119+
$style->save();
120+
121+
// Create a node-type, named 'note'.
122+
$node_type = NodeType::create(['type' => 'note']);
123+
$node_type->save();
124+
125+
// Create an image field and attach it to the 'note' node-type.
126+
FieldStorageConfig::create([
127+
'entity_type' => 'node',
128+
'field_name' => 'sticker',
129+
'type' => 'image',
130+
])->save();
131+
FieldConfig::create([
132+
'entity_type' => 'node',
133+
'field_name' => 'sticker',
134+
'bundle' => 'note',
135+
])->save();
136+
137+
// Create the default entity view display and set the 'sticker' field to use
138+
// the 'main_style' images style in formatter.
139+
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
140+
$view_display = EntityViewDisplay::create([
141+
'targetEntityType' => 'node',
142+
'bundle' => 'note',
143+
'mode' => 'default',
144+
'status' => TRUE,
145+
])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
146+
$view_display->save();
147+
148+
// Create the default entity form display and set the 'sticker' field to use
149+
// the 'main_style' images style in the widget.
150+
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
151+
$form_display = EntityFormDisplay::create([
152+
'targetEntityType' => 'node',
153+
'bundle' => 'note',
154+
'mode' => 'default',
155+
'status' => TRUE,
156+
])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
157+
$form_display->save();
158+
159+
// Check that the entity displays exists before dependency renaming.
160+
$this->assertNotNull(EntityViewDisplay::load($view_display->id()));
161+
$this->assertNotNull(EntityFormDisplay::load($form_display->id()));
162+
163+
// Rename the 'main_style' image style.
164+
$style->setName('main_style_renamed');
165+
$style->save();
166+
167+
// Check that the entity displays exists after dependency renaming.
168+
$this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
169+
$this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
170+
// Check that the 'sticker' formatter component exists in both displays.
171+
$this->assertNotNull($formatter = $view_display->getComponent('sticker'));
172+
$this->assertNotNull($widget = $form_display->getComponent('sticker'));
173+
// Check that both displays are using now 'main_style_renamed' for images.
174+
$this->assertSame('main_style_renamed', $formatter['settings']['image_style']);
175+
$this->assertSame('main_style_renamed', $widget['settings']['preview_image_style']);
176+
}
177+
112178
}

0 commit comments

Comments
 (0)