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:
- Plugin manager
- Plugin interface
- Plugin base
- 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.