You are here

public static function WebformSubmissionExportImportUploadForm::displayStats in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_submission_export_import/src/Form/WebformSubmissionExportImportUploadForm.php \Drupal\webform_submission_export_import\Form\WebformSubmissionExportImportUploadForm::displayStats()

Disply import status.

Parameters

array $stats: Import stats.

2 calls to WebformSubmissionExportImportUploadForm::displayStats()
WebformSubmissionExportImportUploadForm::batchProcess in modules/webform_submission_export_import/src/Form/WebformSubmissionExportImportUploadForm.php
Batch API callback; Write the header and rows of the export to the export file.
WebformSubmissionExportImportUploadForm::submitImportForm in modules/webform_submission_export_import/src/Form/WebformSubmissionExportImportUploadForm.php
Import submission handler.

File

modules/webform_submission_export_import/src/Form/WebformSubmissionExportImportUploadForm.php, line 671

Class

WebformSubmissionExportImportUploadForm
Upload webform submission export import CSV.

Namespace

Drupal\webform_submission_export_import\Form

Code

public static function displayStats(array $stats) {
  $is_cli = PHP_SAPI === 'cli';
  $number_of_errors = 0;
  $error_limit = $is_cli ? NULL : 50;
  $t_args = [
    '@total' => $stats['total'],
    '@created' => $stats['created'],
    '@updated' => $stats['updated'],
    '@skipped' => $stats['skipped'],
  ];
  if ($is_cli) {
    \Drupal::logger('webform')
      ->notice(t('Submission import completed. (total: @total; created: @created; updated: @updated; skipped: @skipped)', $t_args));
  }
  else {
    \Drupal::messenger()
      ->addStatus(t('Submission import completed. (total: @total; created: @created; updated: @updated; skipped: @skipped)', $t_args));
  }
  $message_types = [
    'warnings' => MessengerInterface::TYPE_WARNING,
    'errors' => MessengerInterface::TYPE_ERROR,
  ];
  foreach ($message_types as $message_group => $message_type) {
    foreach ($stats[$message_group] as $row_number => $messages) {
      $row_prefix = [
        '#markup' => t('Row #@number', [
          '@number' => $row_number,
        ]),
        '#prefix' => $is_cli ? '' : '<strong>',
        '#suffix' => $is_cli ? ': ' : ':</strong> ',
      ];
      foreach ($messages as $message) {
        if ($is_cli) {
          $message = strip_tags($message);
        }
        $build = [
          'row' => $row_prefix,
          'message' => [
            '#markup' => $message,
          ],
        ];
        $message = \Drupal::service('renderer')
          ->renderPlain($build);
        if ($is_cli) {
          \Drupal::logger('webform_submission_export_import')
            ->{$message_type}($message);
        }
        else {
          \Drupal::messenger()
            ->addMessage($message, $message_type);
        }
        if ($error_limit && ++$number_of_errors >= $error_limit) {
          return;
        }
      }
    }
  }
}