You are here

public function MaestroWebformTask::getExecutableForm in Maestro 8.2

Same name and namespace in other branches
  1. 3.x modules/maestro_webform/src/Plugin/EngineTasks/MaestroWebformTask.php \Drupal\maestro_webform\Plugin\EngineTasks\MaestroWebformTask::getExecutableForm()

Gets the Maestro executable form for a task console.

Parameters

string $modal: Defines if the form is a modal form or not.

Drupal\maestro\Form\MaestroExecuteInteractive $parent: Parent class for using modal callbacks to the interactive form base if needed.

Return value

array Array Must return form declaration fields if this task is interactive or not.

Overrides MaestroEngineTaskInterface::getExecutableForm

File

modules/maestro_webform/src/Plugin/EngineTasks/MaestroWebformTask.php, line 111

Class

MaestroWebformTask
Maestro Webform Task Plugin.

Namespace

Drupal\maestro_webform\Plugin\EngineTasks

Code

public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {

  /*
   * This will be our base form for displaying the webform submission to the end user through the task console.
   */
  $form['#title'] = $this
    ->t('Submission Review');

  // Load the task's configuration so that we can determine which webform and unique identifier this
  // particular task will be using.
  $templateTask = MaestroEngine::getTemplateTaskByQueueID($this->queueID);
  $taskUniqueSubmissionId = $templateTask['data']['unique_id'];
  $webformMachineName = $templateTask['data']['webform_machine_name'];
  $queueEntry = MaestroEngine::getQueueEntryById($this->queueID);
  if ($queueEntry) {
    $started_date = intval($queueEntry->started_date
      ->getString());
    $created_date = intval($queueEntry->created
      ->getString());

    // We will set the started date to the FIRST time someone clicks on the execute of the task.
    // when we create a task, we set the started_date to the time the entity is created.
    if ($started_date - $created_date < 5) {

      // There could be some slack between the started date and the created date just due to latency in task and entity creation.
      // giving it 5s should be enough time.
      $queueEntry
        ->set('started_date', time());
      $queueEntry
        ->save();
    }
  }

  // Determine if the Webform Task has a node it is attached to set in the task's definition.
  // Signals nothing selected.
  $webformNode = FALSE;
  if (isset($templateTask['data']['use_nodes_attached']) && $templateTask['data']['use_nodes_attached'] == 1) {

    // Task has been flagged as requiring the webform node to be utilized.
    if (isset($templateTask['data']['webform_nodes_attached_variable']) && $templateTask['data']['webform_nodes_attached_variable'] != 'none') {

      // Task is using the process variable to get the node ID. Intval it to ensure it's an integer.
      $node_id = intval(MaestroEngine::getProcessVariable($templateTask['data']['webform_nodes_attached_variable'], $this->processID));
      $webformNode = 'node/' . $node_id;
    }
    elseif (isset($templateTask['data']['webform_nodes_attached_to']) && $templateTask['data']['webform_nodes_attached_to'] != 'none') {

      // Task is using the selectbox value for which node to show.
      $webformNode = $templateTask['data']['webform_nodes_attached_to'];
    }

    // If we get here without the if/elseif firing, webformNode is FALSE.
  }

  // Determine if the webform's $taskUniqueSubmissionId exists in the "webforms" process variable.
  // If it exists, show it to the user.
  // If not, then bring it up to be created.
  // Load a webform submission by loading this task's unique identifier.
  $sid = MaestroEngine::getEntityIdentiferByUniqueID($this->processID, $taskUniqueSubmissionId);
  $webform_submission = NULL;
  if ($sid) {
    $webform_submission = WebformSubmission::load($sid);
  }
  if ($webform_submission) {

    // We have a submission.  Let's now see if we should be showing the edit page.
    if (isset($templateTask['data']['show_edit_form']) && $templateTask['data']['show_edit_form'] == 1) {
      $url = Url::fromUserInput('/admin/structure/webform/manage/contact/submission/' . $sid . '/edit', [
        'query' => [
          'maestro' => 1,
          'queueid' => $this->queueID,
        ],
      ]);
      $response = new RedirectResponse($url
        ->toString());
      $response
        ->send();
    }
    else {
      $container = \Drupal::getContainer();
      $webformSubmissionViewController = WebformSubmissionViewController::create($container);
      $webform_build_view = $webformSubmissionViewController
        ->view($webform_submission, 'table', $langcode = NULL);
      $form['submission_information'] = $webform_build_view['information'];
      $form['submission_information']['#open'] = FALSE;

      // We just want the submission.
      $form['submission_data'] = $webform_build_view['submission'];
    }
  }
  else {

    // Not able to retrieve the webform submission.
    if ($sid !== FALSE && $sid !== NULL) {
      \Drupal::messenger()
        ->addError(t('The submission attached to this workflow was unable to be fetched.'));
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -15,
      ];
    }
    else {
      if ($webformNode && $webformNode !== FALSE) {
        $url = Url::fromUserInput('/' . $webformNode, [
          'query' => [
            'maestro' => 1,
            'queueid' => $this->queueID,
          ],
        ]);
      }
      else {
        $url = Url::fromUserInput('/webform/' . $webformMachineName, [
          'query' => [
            'maestro' => 1,
            'queueid' => $this->queueID,
          ],
        ]);
      }
      $response = new RedirectResponse($url
        ->toString());
      $response
        ->send();
    }
  }
  $form['queueID'] = [
    '#type' => 'hidden',
    '#title' => $this
      ->t('The queue ID of this task'),
    '#default_value' => $this->queueID,
    '#description' => $this
      ->t('queueID'),
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Accept'),
  ];
  $form['actions']['reject'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Reject'),
  ];
  $form['#attached']['library'][] = 'maestro_webform/maestro-webform-css';

  // Does the developer/admin wish to attach any custom form elements?  Let them do so here:
  \Drupal::moduleHandler()
    ->invokeAll('maestro_webform_submission_form', [
    $this->queueID,
    &$form,
    &$this,
  ]);
  return $form;
}