You are here

function lingotek_post_update_lingotek_content_metadata_job_id in Lingotek Translation 8.2

Update job ids for content metadata, replacing invalid chars.

File

./lingotek.post_update.php, line 48
Post update functions for Lingotek.

Code

function lingotek_post_update_lingotek_content_metadata_job_id(&$sandbox = NULL) {

  // Initialize sandbox info.
  $pendingEntities =& $sandbox['entities'];
  if (!isset($pendingEntities)) {
    $storage = \Drupal::entityTypeManager()
      ->getStorage('lingotek_content_metadata');
    $query = $storage
      ->getQuery();
    $pendingEntities = $query
      ->execute();
    $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
    $sandbox['total'] = count($pendingEntities);
    $sandbox['entities'] = $pendingEntities;
  }

  /** @var \Drupal\lingotek\LingotekInterface $lingotek */
  $lingotek = \Drupal::service('lingotek');
  $ids = [];
  for ($var = 0; $var < $sandbox['limit'] && !empty($pendingEntities); $var++) {
    $ids[] = array_pop($pendingEntities);
  }
  $metadatas = LingotekContentMetadata::loadMultiple($ids);
  array_walk($metadatas, function (LingotekContentMetadata $metadata) use ($lingotek) {
    $job_id = $metadata
      ->getJobId();
    if (preg_match('@[\\/\\\\]+@', $job_id)) {
      $new_job_id = str_replace('/', '-', $job_id);
      $new_job_id = str_replace('\\', '-', $new_job_id);
      if ($new_job_id !== $job_id) {
        $metadata
          ->setJobId($new_job_id);
        $metadata
          ->save();
        if ($document_id = $metadata
          ->getDocumentId()) {
          try {
            $lingotek
              ->updateDocument($document_id, NULL, NULL, NULL, NULL, $new_job_id);
          } catch (LingotekApiException $exception) {
            \Drupal::logger('lingotek')
              ->error("Update of job_id %job failed for document %document", [
              '%job' => $new_job_id,
              '%document' => $document_id,
            ]);
          }
        }
      }
    }
  });
  $sandbox['entities'] = $pendingEntities;
  $sandbox['#finished'] = empty($sandbox['entities']) ? 1 : ($sandbox['total'] - count($sandbox['entities'])) / $sandbox['total'];
}