You are here

public static function ImporterService::import in Path redirect import 8

Main method: execute parsing and saving of redirects.

Parameters

mixed $file: Either a Drupal file object (ui) or a path to a file (drush).

str[] $options: User-supplied default flags.

3 calls to ImporterService::import()
drush_path_redirect_import in ./path_redirect_import.drush.inc
Implements the path-redirect-import drush command.
PathRedirectImport::pathRedirectImportCsv in src/Commands/PathRedirectImport.php
Import redirects.
RedirectImportForm::submitForm in src/Form/RedirectImportForm.php
Form submission handler.

File

src/ImporterService.php, line 34

Class

ImporterService
Class ImporterService.

Namespace

Drupal\path_redirect_import

Code

public static function import($file, array $options) {

  // Parse the CSV file into a readable array.
  $data = self::read($file, $options);
  self::$options = $options;

  // Perform Drupal-specific validation logic on each row.
  $data = array_filter($data, [
    'self',
    'preSave',
  ]);
  if ($options['suppress_messages'] != 1 && !empty(self::$messages['warning'])) {

    // Messaging/logging is separated out in case we want to suppress these.
    foreach (self::$messages['warning'] as $warning) {
      \Drupal::messenger()
        ->addWarning($warning, 'warning');
    }
  }
  if (empty($data)) {
    \Drupal::messenger()
      ->addWarning(t('The uploaded file contains no rows with compatible redirect data. No redirects have imported. Compare your file to <a href=":sample">this sample data.</a>', [
      ':sample' => '/' . drupal_get_path('module', 'path_redirect_import') . '/redirect-example-file.csv',
    ]));
  }
  else {
    if (PHP_SAPI == 'cli' && function_exists('drush_main')) {
      foreach ($data as $redirect_array) {
        self::save($redirect_array, $options['override'], []);
      }
    }
    else {

      // Save valid redirects.
      foreach ($data as $row) {
        $operations[] = [
          [
            '\\Drupal\\path_redirect_import\\ImporterService',
            'save',
          ],
          [
            $row,
            $options['override'],
          ],
        ];
      }
      $batch = [
        'title' => t('Saving Redirects'),
        'operations' => $operations,
        'finished' => [
          '\\Drupal\\path_redirect_import\\ImporterService',
          'finish',
        ],
        'file' => drupal_get_path('module', 'path_redirect_import') . '/path_redirect_import.module',
      ];
      batch_set($batch);
    }
  }
}