You are here

function duration_field_update_db_v1_v2 in Duration Field 8.2

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

Helper function to convert 8.x-1.x DB tables to 8.x-2.x DB tables.

Parameters

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

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

File

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

Code

function duration_field_update_db_v1_v2(FieldConfigInterface $field) {
  $duration_service = \Drupal::service('duration_field.service');
  $entity_type_id = $field
    ->getTargetEntityTypeId();
  $field_name = $field
    ->getName();

  // Ignore entity manager caches.

  /** @var \Drupal\Core\Entity\EntityManager $entity_manager */
  $entity_manager = \Drupal::service('entity_type.manager');
  $entity_manager
    ->useCaches(FALSE);

  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
  $schema_repository = \Drupal::service('entity.last_installed_schema.repository');

  /** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $field_storage_definitions = $schema_repository
    ->getLastInstalledFieldStorageDefinitions($entity_type_id);
  $schema = $field_storage_definitions[$field_name]
    ->getSchema();

  // Update the 'value' field to be 'duration'.
  \Drupal::database()
    ->schema()
    ->changeField($entity_type_id . '__' . $field_name, $field_name . '_value', $field_name . '_duration', $schema['columns']['duration']);

  // Add the 'seconds' field.
  \Drupal::database()
    ->schema()
    ->addField($entity_type_id . '__' . $field_name, $field_name . '_seconds', $schema['columns']['seconds']);

  // Update the 'value' field on the revisions table to be 'duration'.
  \Drupal::database()
    ->schema()
    ->changeField($entity_type_id . '_revision__' . $field_name, $field_name . '_value', $field_name . '_duration', $schema['columns']['duration']);

  // Add the 'seconds' field to the revisions table.
  \Drupal::database()
    ->schema()
    ->addField($entity_type_id . '_revision__' . $field_name, $field_name . '_seconds', $schema['columns']['seconds']);
  $database = \Drupal::database();

  // Get the existing database value from the field table.
  $values = $database
    ->select($entity_type_id . '__' . $field_name, 'fieldtable')
    ->fields('fieldtable', [
    'entity_id',
    'revision_id',
    $field_name . '_duration',
  ])
    ->execute();

  // Set the 'seconds' column value in each row based on the 'duration' column
  // value.
  foreach ($values as $value) {
    $database
      ->update($entity_type_id . '__' . $field_name)
      ->fields([
      $field_name . '_seconds' => $duration_service
        ->getSecondsFromDurationString($value->{$field_name . '_duration'}),
    ])
      ->condition('entity_id', $value->entity_id)
      ->condition('revision_id', $value->revision_id)
      ->execute();
  }

  // Get the existing database value from the field revision table.
  $values = $database
    ->select($entity_type_id . '_revision__' . $field_name, 'fieldtable')
    ->fields('fieldtable', [
    'entity_id',
    'revision_id',
    $field_name . '_duration',
  ])
    ->execute();

  // Set the 'seconds' column value for each row in the revisions table, based
  // on the 'duration' column value.
  foreach ($values as $value) {
    $database
      ->update($entity_type_id . '_revision__' . $field_name)
      ->fields([
      $field_name . '_seconds' => $duration_service
        ->getSecondsFromDurationString($value->{$field_name . '_duration'}),
    ])
      ->condition('entity_id', $value->entity_id)
      ->condition('revision_id', $value->revision_id)
      ->execute();
  }
}