You are here

public function MigrateManager::recreateWorkbenchModerationWorkflow in Workbench Moderation to Content Moderation 8

Create the Workflow based on info from WBM states and transitions.

File

src/MigrateManager.php, line 349

Class

MigrateManager
Manages migrating from WBM to CM.

Namespace

Drupal\wbm2cm

Code

public function recreateWorkbenchModerationWorkflow() {
  $states = $this->migrateStore
    ->get('states');
  $transitions = $this->migrateStore
    ->get('transitions');
  $enabled_bundles = $this->migrateStore
    ->get('enabled_bundles');

  // Create and save a workflow entity with the information gathered.
  // Note: this implies all entities will be squished into a single workflow.
  $workflow_config = [
    'id' => 'content_moderation_workflow',
    'label' => 'Content Moderation Workflow',
    'type' => 'content_moderation',
    'type_settings' => [
      'states' => [],
      'transitions' => [],
    ],
  ];
  foreach ($states as $state) {
    $workflow_config['type_settings']['states'][$state['id']] = [
      'label' => $state['label'],
      'published' => $state['published'],
      'default_revision' => $state['default_revision'],
    ];
  }
  foreach ($transitions as $transition) {
    $workflow_config['type_settings']['transitions'][$transition['id']] = [
      'label' => $transition['label'],
      'to' => $transition['stateTo'],
      'from' => explode(',', $transition['stateFrom']),
    ];
  }

  // Instantiate the workflow from the config.
  $this->logger
    ->info('Create workflow from config: %config', [
    '%config' => print_r($workflow_config, 1),
  ]);
  $workflow = new Workflow($workflow_config, 'workflow');
  $workflow_type_plugin = $workflow
    ->getTypePlugin();

  // Add Content Moderation moderation to bundles that were Workbench
  // Moderation moderated.
  foreach ($enabled_bundles as $entity_type_id => $bundles) {
    foreach ($bundles as $bundle_id) {
      $workflow_type_plugin
        ->addEntityTypeAndBundle($entity_type_id, $bundle_id);
      $this->logger
        ->notice('Setting Content Moderation to be enabled on %bundle_id', [
        '%bundle_id' => $bundle_id,
      ]);
    }
  }

  // Save the workflow now that it has all the configurations set.
  $workflow
    ->save();
  $this->logger
    ->notice('Content Moderation Workflow created.');
}