You are here

public static function ScannerConfirmForm::batchReplace in Search and Replace Scanner 8

Process the replacement.

File

src/Form/ScannerConfirmForm.php, line 108

Class

ScannerConfirmForm
Form for configure messages.

Namespace

Drupal\scanner\Form

Code

public static function batchReplace($field, $values, &$context) {
  $pluginManager = \Drupal::service('plugin.manager.scanner');
  list($fieldname) = explode(':', $field);
  try {
    $plugin = $pluginManager
      ->createInstance('scanner_entity');

    // This process can take a while so we want to extend the execution time
    // if it's less then 300 (5 minutes).
    if (ini_get('max_execution_time') < 300) {
      ini_set('max_execution_time', 300);
    }
  } catch (PluginException $e) {

    // The instance could not be found so fail gracefully and let the user
    // know.
    \Drupal::logger('scanner')
      ->error($e
      ->getMessage());
    \Drupal::messenger()
      ->addError($this
      ->t('An error occured @e:', [
      '@e' => $e
        ->getMessage(),
    ]));
  }
  $results_data = '';
  if (isset($context['results']['data'])) {
    $results_data = $context['results']['data'];
  }
  if (is_string($results_data)) {
    $results = $plugin
      ->replace($field, $values, []);
  }
  else {
    $results = $plugin
      ->replace($field, $values, $results_data);
  }
  if (!empty($results)) {
    $entityKeys = array_keys($results);
    foreach ($entityKeys as $entityKey) {
      $context['results']['data'][$entityKey] = $results[$entityKey];
    }
    $context['results']['inputs'] = [
      'search' => $values['search'],
      'replace' => $values['replace'],
    ];
    $context['message'] = 'Searching through field: ' . $fieldname;
  }
}