Commit ba54ec96 authored by frjo's avatar frjo Committed by frjo
Browse files

Issue #2686805 by frjo: Image style dependency is missing on configuration export

parent b4873f35
<?php
/**
* @file
* Post-update functions for Colorbox.
*/
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
/**
* Saves the image style dependencies into form and view display entities.
*/
function colorbox_post_update_image_style_dependencies() {
// Merge view and form displays. Use array_values() to avoid key collisions.
$displays = array_merge(array_values(EntityViewDisplay::loadMultiple()), array_values(EntityFormDisplay::loadMultiple()));
/** @var \Drupal\Core\Entity\Display\EntityDisplayInterface[] $displays */
foreach ($displays as $display) {
// Re-save each config entity to add missed dependencies.
$display->save();
}
}
......@@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
......@@ -331,4 +332,56 @@ class ColorboxFormatter extends ImageFormatterBase implements ContainerFactoryPl
return $elements;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
$style_ids = array();
$style_ids[] = $this->getSetting('colorbox_node_style');
if (!empty($settings['colorbox_node_style_first'])) {
$style_ids[] = $this->getSetting('colorbox_node_style_first');
}
$style_ids[] = $this->getSetting('colorbox_image_style');
/** @var \Drupal\image\ImageStyleInterface $style */
foreach ($style_ids as $style_id) {
if ($style_id && $style = ImageStyle::load($style_id)) {
// If this formatter uses a valid image style to display the image, add
// the image style configuration entity as dependency of this formatter.
$dependencies[$style->getConfigDependencyKey()][] = $style->getConfigDependencyName();
}
}
return $dependencies;
}
/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
$style_ids = array();
$style_ids['colorbox_node_style'] = $this->getSetting('colorbox_node_style');
if (!empty($settings['colorbox_node_style_first'])) {
$style_ids['colorbox_node_style_first'] = $this->getSetting('colorbox_node_style_first');
}
$style_ids['colorbox_image_style'] = $this->getSetting('colorbox_image_style');
/** @var \Drupal\image\ImageStyleInterface $style */
foreach ($style_ids as $name => $style_id) {
if ($style_id && $style = ImageStyle::load($style_id)) {
if (!empty($dependencies[$style->getConfigDependencyKey()][$style->getConfigDependencyName()])) {
$replacement_id = $this->imageStyleStorage->getReplacementId($style_id);
// If a valid replacement has been provided in the storage, replace the
// image style with the replacement and signal that the formatter plugin
// settings were updated.
if ($replacement_id && ImageStyle::load($replacement_id)) {
$this->setSetting($name, $replacement_id);
$changed = TRUE;
}
}
}
}
return $changed;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment