diff --git a/focal_point.install b/focal_point.install index ad59d03..7988384 100644 --- a/focal_point.install +++ b/focal_point.install @@ -142,3 +142,36 @@ function focal_point_update_8003() { // new configuration. $existing_config->save(); } + +/** + * Migrates image fields to new focus image widget. + */ +function focal_point_update_8004() { + $mapping = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('image'); + $formStorage = \Drupal::entityTypeManager()->getStorage('entity_form_display'); + $previewLink = \Drupal::config('focal_point.preview')->get('display_link'); + + foreach ($mapping as $entity => $fields) { + foreach ($fields as $fieldName => $def) { + foreach ($def['bundles'] as $bundle) { + $properties = [ + 'targetEntityType' => $entity, + 'bundle' => $bundle, + ]; + if ($formDisplays = $formStorage->loadByProperties($properties)) { + /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $formDisplay */ + foreach ($formDisplays as $formDisplay) { + if (is_array($componentConfig = $formDisplay->getComponent($fieldName))) { + $componentConfig['type'] = 'image_focal_point'; + $componentConfig['settings']['preview_link'] = $previewLink; + $componentConfig['settings']['offsets'] = '50,50'; + $formDisplay->setComponent($fieldName, $componentConfig)->save(); + } + } + } + } + } + } + + \Drupal::configFactory()->getEditable('focal_point.preview')->delete(); +}