You are here

function duration_field_update_config_v1_v2 in Duration Field 8.2

Same name and namespace in other branches
  1. 3.0.x duration_field.install \duration_field_update_config_v1_v2()

Helper function to convert 8.x-1.x configuration to 8.x-2.x configuration.

Handles the following tasks:

  • Converts default value for each field instance a single value of 'value' to 'duration' (an ISO 1806 duration string) and 'seconds' (the number of seconds the duration string represents)
  • Converts the granularity settings for each field instance from an array to a granularity string.
  • Removes the now unused 'duration' setting for duration field widget settings.

Parameters

\Drupal\Core\Field\FieldConfigInterface $field: The field to be updated.

1 call to duration_field_update_config_v1_v2()
duration_field_update_8200 in ./duration_field.install
Implements hook_update_N().

File

./duration_field.install, line 45
Holds install hooks for the Duration Field module.

Code

function duration_field_update_config_v1_v2(FieldConfigInterface $field) {
  $duration_service = \Drupal::service('duration_field.service');
  $granularity_service = \Drupal::service('duration_field.granularity.service');
  $entity_type_id = $field
    ->getTargetEntityTypeId();
  $bundle = $field
    ->getTargetBundle();
  $field_name = $field
    ->getName();
  $new_field = $field
    ->toArray();

  // Convert the original default value from 'value' to 'duration' and
  // 'seconds'.
  $new_field['default_value'][0]['duration'] = $new_field['default_value'][0]['value'];
  $new_field['default_value'][0]['seconds'] = $duration_service
    ->getSecondsFromDurationString($new_field['default_value'][0]['value']);

  // Unset the 'value' key, as it is not used anymore.
  unset($new_field['default_value'][0]['value']);

  // Change the field settings from a granularity array to a granularity
  // string. The old settings used different keys than the new settings, so they
  // first need to be recast.
  $granularity_array = [
    'y' => $new_field['settings']['granularity']['year'],
    'm' => $new_field['settings']['granularity']['month'],
    'd' => $new_field['settings']['granularity']['day'],
    'h' => $new_field['settings']['granularity']['hour'],
    'i' => $new_field['settings']['granularity']['minute'],
    's' => $new_field['settings']['granularity']['second'],
  ];

  // Update the field's granularity settings to a granularity string.
  $new_field['settings']['granularity'] = $granularity_service
    ->convertGranularityArrayToGranularityString($granularity_array);

  // Do some cleanup and save the new configuration.
  $new_field = FieldConfig::create($new_field);
  $new_field->original = $field;
  $new_field
    ->enforceIsNew(FALSE);
  $new_field
    ->save();

  // Next the widget settings need to be updated, to remove the now unused
  // 'duration' setting.
  $properties = [
    'targetEntityType' => $entity_type_id,
    'bundle' => $bundle,
  ];

  // Load any form displays for this field.
  if ($form_displays = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->loadByProperties($properties)) {

    // Loop through any found form displays.
    foreach ($form_displays as $form_display) {

      // Load the component for the field from the form display.
      if ($component = $form_display
        ->getComponent($field_name)) {

        // Unset the new unused duration setting.
        unset($component['settings']['duration']);

        // Save the updated component.
        $form_display
          ->setComponent($field_name, $component)
          ->save();
      }
    }
  }
}