You are here

function data_policy_update_8011 in Data Policy 8

Fix issues with Data Policy translation.

File

./data_policy.install, line 188
Install, update and uninstall functions for the data_policy module.

Code

function data_policy_update_8011() {

  // Make "name" field translatable.
  $manager = \Drupal::entityDefinitionUpdateManager();
  $storage_definition = $manager
    ->getFieldStorageDefinition('name', 'data_policy');
  $storage_definition
    ->setTranslatable(TRUE);
  $manager
    ->updateFieldStorageDefinition($storage_definition);

  // Make field "Description" translatable.
  $config = \Drupal::configFactory()
    ->getEditable('field.field.data_policy.data_policy.field_description');
  $config
    ->set('translatable', TRUE);
  $config
    ->save();

  // If there are updates for data policy translations - lets run them.
  $config = \Drupal::config('language.content_settings.data_policy.data_policy');
  $translatable = $config instanceof ImmutableConfig && $config
    ->get('status');
  $change_list = \Drupal::entityDefinitionUpdateManager()
    ->getChangeList();
  if ($translatable && !empty($change_list['data_policy'])) {
    if (function_exists('_content_translation_install_field_storage_definitions')) {
      _content_translation_install_field_storage_definitions('data_policy');

      // Set default values for new fields.
      $database = \Drupal::database();
      $storage = \Drupal::entityTypeManager()
        ->getStorage('data_policy');
      foreach ($storage
        ->loadMultiple() as $entity) {
        $langcodes = array_keys($entity
          ->getTranslationLanguages());
        foreach ($langcodes as $langcode) {

          // Update data policy entity. Let's do it with database query
          // to avoid to make the entity changed.
          $translation = $entity
            ->getTranslation($langcode);
          $database
            ->update('data_policy_field_data')
            ->fields([
            'content_translation_uid' => $translation
              ->getOwnerId(),
            'content_translation_created' => $translation
              ->getCreatedTime(),
            'content_translation_changed' => $translation
              ->getChangedTime(),
          ])
            ->condition('id', $translation
            ->id())
            ->condition('langcode', $langcode)
            ->execute();

          // Update revisions.
          $vids = $storage
            ->revisionIds($translation);
          foreach ($vids as $vid) {
            $revision = $storage
              ->loadRevision($vid);
            $database
              ->update('data_policy_field_revision')
              ->fields([
              'content_translation_uid' => $revision
                ->getOwnerId(),
              'content_translation_created' => $revision
                ->getCreatedTime(),
              'content_translation_changed' => $revision
                ->getChangedTime(),
            ])
              ->condition('vid', $vid)
              ->execute();
          }
        }
      }
    }
  }
}