Wbm2cmCommands.php in Workbench Moderation to Content Moderation 8.2
File
src/Commands/Wbm2cmCommands.php
View source
<?php
namespace Drupal\wbm2cm\Commands;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\wbm2cm\MigrationController;
use Drush\Commands\DrushCommands;
class Wbm2cmCommands extends DrushCommands {
protected $controller;
protected $moduleInstaller;
public function __construct(MigrationController $controller, ModuleInstallerInterface $module_installer) {
$this->controller = $controller;
$this->moduleInstaller = $module_installer;
}
public function migrate() {
$out = $this
->output();
$this
->save();
$this
->clear(FALSE);
$fields = $this->controller
->getOverriddenFields();
if ($fields) {
$out
->writeln('It looks like you have overridden the moderation_state base field. These overrides will be reverted because they are incompatible with Content Moderation. You will also need to delete these from your exported config.');
foreach ($fields as $field) {
$field
->delete();
$message = sprintf('Reverted %s. Delete %s.yml from your exported config.', $field
->id(), $field
->getConfigDependencyName());
$out
->writeln($message);
}
}
$out
->writeln('Installing Content Moderation...');
$this->moduleInstaller
->uninstall([
'workbench_moderation',
]);
if (\Drupal::hasContainer()) {
$this->controller = $this
->reloadService($this->controller);
$this->moduleInstaller = $this
->reloadService($this->moduleInstaller);
}
$this->moduleInstaller
->install([
'content_moderation',
]);
$this
->restore();
$out
->writeln('Yay! You have been migrated to Content Moderation.');
}
public function save() {
$out = $this
->output();
$out
->writeln('Saving existing moderation states to temporary tables...');
$messages = $this->controller
->executeStepWithMessages('save');
array_walk($messages, [
$out,
'writeln',
]);
}
public function clear($standalone = TRUE) {
$out = $this
->output();
$out
->writeln('Removing Workbench Moderation data...');
$messages = $this->controller
->executeStepWithMessages('clear');
array_walk($messages, [
$out,
'writeln',
]);
if ($standalone) {
$out
->writeln('You should now be able to uninstall Workbench Moderation and install Content Moderation.');
}
}
public function restore() {
$out = $this
->output();
$out
->writeln('Restoring moderation states from temporary tables...');
$messages = $this->controller
->executeStepWithMessages('restore');
array_walk($messages, [
$out,
'writeln',
]);
}
protected function reloadService($service) {
return \Drupal::service($service->_serviceId);
}
}