Porting Scanner Module to D8 - Part 2

2019-05-06

Christian Crawford

Porting Scanner Module to D8 - Part 2

Second part covering Drupal 8 plugin system, batch API, and completing the scanner module port.

This is a continuation of a previous post.

Plugins

Drupal 8 introduced the idea of plugins. To create a brand new plugin you must write:

  1. Plugin manager
  2. Plugin interface
  3. Plugin base
  4. Annotation for discoverability
class ScannerPluginManager extends DefaultPluginManager {
  public function __construct(...) {
    parent::__construct('Plugin/Scanner', $namespaces, $module_handler, 'Drupal\scanner\Plugin\ScannerPluginInterface', 'Drupal\scanner\Annotation\Scanner');
  }
}

Batch API

Batch jobs are most commonly initiated in the submit handler of a form:

$batch = [
  'title' => t('Scanner Replace Batch'),
  'operations' => $operations,
  'finished' => '\Drupal\scanner\Form\ScannerConfirmUndoForm::batchFinished',
  'progress_message' => t('Processed @current out of @total'),
];
batch_set($batch);

The code for the full module can be found on BitBucket.