You are here

function s3fs_cors_update_8001 in S3 File System CORS Upload 8

Update any existing s3cors_file type fields to s3fs_cors_file type.

File

./s3fs_cors.install, line 14
Update functions.

Code

function s3fs_cors_update_8001(&$sandbox) {

  // Updating field storage config items.
  if (!($field_storage_configs = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config')
    ->loadByProperties([
    'type' => 's3cors_file',
  ]))) {
    return;
  }
  foreach ($field_storage_configs as $field_storage) {

    // Since the usual workflow for field storages do not allow changing the
    // field type, we have to work around it in this case.
    $new_field_storage = $field_storage
      ->toArray();
    $new_field_storage['type'] = 's3fs_cors_file';
    $new_field_storage = FieldStorageConfig::create($new_field_storage);
    $new_field_storage->original = $new_field_storage;
    $new_field_storage
      ->enforceIsNew(FALSE);
    $new_field_storage
      ->save();

    // Updating field config items.
    $field_name = $field_storage
      ->getName();
    if (!($fields = \Drupal::entityTypeManager()
      ->getStorage('field_config')
      ->loadByProperties([
      'field_name' => $field_name,
    ]))) {
      continue;
    }

    /** @var \Drupal\field\Entity\FieldConfig $field */
    foreach ($fields as $field) {
      $new_field = $field
        ->toArray();
      $new_field['field_type'] = 's3fs_cors_file';
      $new_field = FieldConfig::create($new_field);
      $new_field->original = $field;
      $new_field
        ->enforceIsNew(FALSE);
      $new_field
        ->save();

      // Updating entity view display configs.
      $properties = [
        'targetEntityType' => $field
          ->getTargetEntityTypeId(),
        'bundle' => $field
          ->getTargetBundle(),
      ];
      if ($view_displays = \Drupal::entityTypeManager()
        ->getStorage('entity_view_display')
        ->loadByProperties($properties)) {
        foreach ($view_displays as $view_display) {
          if ($component = $view_display
            ->getComponent($field_name)) {

            // Map s3cors_file formatters to s3fs_cors_file ones.
            $view_display
              ->setComponent($field_name, [
              'type' => 's3fs_cors_file_default',
              'settings' => [
                'link' => 'Use description as link text',
              ],
            ] + $component);
            $view_display
              ->save();
          }
        }
      }

      // Updating entity form configs.
      $properties = [
        'targetEntityType' => $field
          ->getTargetEntityTypeId(),
        'bundle' => $field
          ->getTargetBundle(),
      ];
      if ($form_displays = \Drupal::entityTypeManager()
        ->getStorage('entity_form_display')
        ->loadByProperties($properties)) {
        foreach ($form_displays as $form_display) {
          if ($component = $form_display
            ->getComponent($field_name)) {
            $form_display
              ->setComponent($field_name, [
              'type' => 's3fs_cors_file_widget',
              'settings' => [
                'progress_indicator' => 'throbber',
                'max_filesize' => '',
              ],
            ] + $component);
            $form_display
              ->save();
          }
        }
      }
    }
  }
}