You are here

public function MaestroSpawnSubFlowTask::execute in Maestro 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/EngineTasks/MaestroSpawnSubFlowTask.php \Drupal\maestro\Plugin\EngineTasks\MaestroSpawnSubFlowTask::execute()

Part of the ExecutableInterface Execution of the Sub Flow task will create a new process and push all selected parent variables to the newly spawned sub process. The variables pushed to the sub process will be prefixed with "maestro_parent_" and will also include a new variable named "parent_process_id" which will store the process ID of the parent. .

Overrides ExecutableInterface::execute

File

src/Plugin/EngineTasks/MaestroSpawnSubFlowTask.php, line 100

Class

MaestroSpawnSubFlowTask
Maestro Spawn Sub Flow Task Plugin.

Namespace

Drupal\maestro\Plugin\EngineTasks

Code

public function execute() {
  $templateMachineName = MaestroEngine::getTemplateIdFromProcessId($this->processID);
  $taskMachineName = MaestroEngine::getTaskIdFromQueueId($this->queueID);
  $task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskMachineName);
  $spawnTemplate = $task['data']['maestro_template'];
  $variables = [];
  if (isset($task['data']['variables'])) {
    $variables = $task['data']['variables'];
  }

  // Let's first start by adding in the new process ID.
  $maestro = new MaestroEngine();
  $newProcessID = $maestro
    ->newProcess($spawnTemplate);
  if ($newProcessID !== FALSE) {

    // first, create the parent process ID variable.
    $values = [
      'process_id' => $newProcessID,
      'variable_name' => 'maestro_parent_process_id',
      'variable_value' => $this->processID,
    ];
    $new_var = \Drupal::entityTypeManager()
      ->getStorage('maestro_process_variables')
      ->create($values);
    $new_var
      ->save();
    if (!$new_var
      ->id()) {

      // Throw a maestro exception
      // completion should technically end here for this initiation.
      throw new MaestroSaveEntityException('maestro_process_variable', $values['variable_name'] . ' failed saving during new process creation.');
    }
    foreach ($variables as $machine_name => $checked_value) {
      if ($machine_name != '') {

        // We now populate the new process with variables.
        $parent_value = MaestroEngine::getProcessVariable($machine_name, $this->processID);
        $values = [
          'process_id' => $newProcessID,
          'variable_name' => 'maestro_parent_' . $machine_name,
          'variable_value' => $parent_value,
        ];
        $new_var = \Drupal::entityTypeManager()
          ->getStorage('maestro_process_variables')
          ->create($values);
        $new_var
          ->save();
        if (!$new_var
          ->id()) {

          // Throw a maestro exception
          // completion should technically end here for this initiation.
          throw new MaestroSaveEntityException('maestro_process_variable', $values['variable_name'] . ' failed saving during new process creation.');
        }
        $parent_value = '';
      }
    }
    return TRUE;
  }
  else {
    \Drupal::logger('maestro')
      ->error('Unable to spawn sub process.  Process spawn returned an error.');
    return FALSE;
  }
}