function LocaleImportBatch::batchFinished

Same name and namespace in other branches
  1. 11.x core/modules/locale/src/LocaleImportBatch.php \Drupal\locale\LocaleImportBatch::batchFinished()

Implements callback_batch_finished().

Finished callback of system page locale import batch.

Parameters

bool $success: TRUE if batch successfully completed.

array $results: Batch results.

File

core/modules/locale/src/LocaleImportBatch.php, line 295

Class

LocaleImportBatch
Provides the locale import batch services.

Namespace

Drupal\locale

Code

public function batchFinished(bool $success, array $results) : void {
  $logger = ($this->logger)();
  if ($success) {
    $additions = $updates = $deletes = $skips = 0;
    if (isset($results['failed_files'])) {
      if ($this->moduleHandler
        ->moduleExists('dblog') && $this->currentUser
        ->hasPermission('access site reports')) {
        $message = $this->translation
          ->formatPlural(count($results['failed_files']), 'One translation file could not be imported. <a href=":url">See the log</a> for details.', '@count translation files could not be imported. <a href=":url">See the log</a> for details.', [
          ':url' => Url::fromRoute('dblog.overview')->toString(),
        ]);
      }
      else {
        $message = $this->translation
          ->formatPlural(count($results['failed_files']), 'One translation file could not be imported. See the log for details.', '@count translation files could not be imported. See the log for details.');
      }
      $this->messenger
        ->addError($message);
    }
    if (isset($results['files'])) {
      $skipped_files = [];
      // If there are no results and/or no stats (eg. coping with an empty .po
      // file), simply do nothing.
      if ($results && isset($results['stats'])) {
        foreach ($results['stats'] as $filepath => $report) {
          if ($filepath === 'config') {
            // Ignore the config entry. It is processed in
            // \Drupal\locale\LocaleConfigBatch::batchFinished() below.
            continue;
          }
          $additions += $report['additions'];
          $updates += $report['updates'];
          $deletes += $report['deletes'];
          $skips += $report['skips'];
          if ($report['skips'] > 0) {
            $skipped_files[] = $filepath;
          }
        }
      }
      $this->messenger
        ->addStatus($this->translation
        ->formatPlural(count($results['files']), 'One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', '@count translation files imported. %number translations were added, %update translations were updated and %delete translations were removed.', [
        '%number' => $additions,
        '%update' => $updates,
        '%delete' => $deletes,
      ]));
      $logger->notice('Translations imported: %number added, %update updated, %delete removed.', [
        '%number' => $additions,
        '%update' => $updates,
        '%delete' => $deletes,
      ]);
      if ($skips) {
        if ($this->moduleHandler
          ->moduleExists('dblog') && $this->currentUser
          ->hasPermission('access site reports')) {
          $message = $this->translation
            ->formatPlural($skips, 'One translation string was skipped because of disallowed or malformed HTML. <a href=":url">See the log</a> for details.', '@count translation strings were skipped because of disallowed or malformed HTML. <a href=":url">See the log</a> for details.', [
            ':url' => Url::fromRoute('dblog.overview')->toString(),
          ]);
        }
        else {
          $message = $this->translation
            ->formatPlural($skips, 'One translation string was skipped because of disallowed or malformed HTML. See the log for details.', '@count translation strings were skipped because of disallowed or malformed HTML. See the log for details.');
        }
        $this->messenger
          ->addWarning($message);
        $logger->warning('@count disallowed HTML string(s) in files: @files.', [
          '@count' => $skips,
          '@files' => implode(',', $skipped_files),
        ]);
      }
    }
  }
  // Add messages for configuration too.
  if (isset($results['stats']['config'])) {
    $this->localeConfigBatch
      ->batchFinished($success, $results);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.