You are here

public static function ScannerForm::batchSearch in Search and Replace Scanner 8

Batch operation function.

Parameters

string $field: The name of the field.

array $values: The $form_state values.

array $context: An array containin data that is persisted across batch jobs.

See also

https://api.drupal.org/api/drupal/core%21includes%21form.inc/group/batch...

File

src/Form/ScannerForm.php, line 244

Class

ScannerForm
Form for performing searching.

Namespace

Drupal\scanner\Form

Code

public static function batchSearch($field, array $values, array &$context) {
  $pluginManager = \Drupal::service('plugin.manager.scanner');
  list($entityType, $bundle, $fieldname) = explode(':', $field);

  // Attempt to load the plugin.
  try {
    $plugin = $pluginManager
      ->createInstance('scanner_entity');
  } 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 = $plugin
    ->search($field, $values);
  if (!empty($results)) {
    $context['results'][$entityType][$bundle][$fieldname] = $results;

    // Number of entities with search term.
    $context['results']['count']['entities'] += count($results);
    foreach ($results as $data) {

      // Number of matches within each field of each entity.
      $context['results']['count']['matches'] += count($data['field']);
    }
    $context['message'] = 'Searching through field...';
  }
}