color_field.post_update.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * @file
  4. * Contains post update functionality for the color field module.
  5. */
  6. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  7. /**
  8. * Update spectrum widget configuration to allow multiple palettes.
  9. */
  10. function color_field_post_update_spectrum_palette() {
  11. /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $entity_form_display */
  12. foreach (EntityFormDisplay::loadMultiple() as $entity_form_display) {
  13. $changed = FALSE;
  14. foreach ($entity_form_display->getComponents() as $name => $options) {
  15. if (isset($options['type']) && $options['type'] === 'color_field_widget_spectrum') {
  16. if ($options['settings']['palette']) {
  17. $palette = explode(',', $options['settings']['palette']);
  18. foreach ($palette as &$color) {
  19. $color = '"' . trim($color) . '"';
  20. }
  21. $options['settings']['palette'] = '[' . implode(',', $palette) . ']';
  22. $entity_form_display->setComponent($name, $options);
  23. $changed = TRUE;
  24. }
  25. }
  26. }
  27. if ($changed) {
  28. $entity_form_display->save();
  29. }
  30. }
  31. return t('The new palette format for spectrum color field widgets has been applied.');
  32. }