You are here

function media_acquiadam_update_8104 in Media: Acquia DAM 8

Migrate time mappings to the new datetime properties.

File

./media_acquiadam.install, line 174
Installation related hooks and functions.

Code

function media_acquiadam_update_8104(&$sandbox) {
  $media_bundles = Drupal::entityTypeManager()
    ->getStorage('media_type')
    ->loadByProperties([
    'source' => 'acquiadam_asset',
  ]);
  $mappings = [
    'datecaptured' => 'datecaptured_unix',
    'datecreated' => 'datecreated_unix',
    'datemodified' => 'datemodified_unix',
  ];

  /** @var \Drupal\media\Entity\MediaType $bundle */
  foreach ($media_bundles as $bundle) {
    $field_mapping = $bundle
      ->getFieldMap();
    foreach ($mappings as $from => $to) {
      if (isset($field_mapping[$from]) && !isset($field_mapping[$to])) {
        $field_mapping[$to] = $field_mapping[$from];
        unset($field_mapping[$from]);
      }
    }
    $bundle
      ->setFieldMap($field_mapping);
    $bundle
      ->save();
  }
}