image.post_update.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * @file
  4. * Post-update functions for Image.
  5. */
  6. use Drupal\Core\Config\Entity\ConfigEntityUpdater;
  7. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  8. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  9. /**
  10. * Saves the image style dependencies into form and view display entities.
  11. */
  12. function image_post_update_image_style_dependencies() {
  13. // Merge view and form displays. Use array_values() to avoid key collisions.
  14. $displays = array_merge(array_values(EntityViewDisplay::loadMultiple()), array_values(EntityFormDisplay::loadMultiple()));
  15. /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface[] $displays */
  16. foreach ($displays as $display) {
  17. // Re-save each config entity to add missed dependencies.
  18. $display->save();
  19. }
  20. }
  21. /**
  22. * Add 'anchor' setting to 'Scale and crop' effects.
  23. */
  24. function image_post_update_scale_and_crop_effect_add_anchor(&$sandbox = NULL) {
  25. \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'image_style', function ($image_style) {
  26. /** @var \Drupal\image\ImageStyleInterface $image_style */
  27. $effects = $image_style->getEffects();
  28. foreach ($effects as $effect) {
  29. if ($effect->getPluginId() === 'image_scale_and_crop') {
  30. return TRUE;
  31. }
  32. }
  33. return FALSE;
  34. });
  35. }