You are here

public function MaestroInteractiveFormBase::getExecutableFormFields in Maestro 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/MaestroInteractiveFormBase.php \Drupal\maestro\Form\MaestroInteractiveFormBase::getExecutableFormFields()

Returns the executable form fields for this interactive task.

Need a different form? no problem, specify the form in the UI and we'll fetch it instead.

1 call to MaestroInteractiveFormBase::getExecutableFormFields()
MaestroExecuteInteractive::buildForm in src/Form/MaestroExecuteInteractive.php
Overridden method to build the form for the interactive task. We are fetching off the interactive function's form fields for this task. .

File

src/Form/MaestroInteractiveFormBase.php, line 212

Class

MaestroInteractiveFormBase
The Maestro Interactive task form base.

Namespace

Drupal\maestro\Form

Code

public function getExecutableFormFields() {
  $processID = MaestroEngine::getProcessIdFromQueueId($this->queueID);

  // Lets load the actual task for this queue item and return its form fields.
  $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();
    }

    // Do you have a handler?  if so, use that function's form elements.
    if ($queueEntry->handler
      ->getString() != '') {

      // We must execute the handler here.  This is a simple function declaration in a .module file traditionally.
      $handler = $queueEntry->handler
        ->getString();
      $form = [];

      // You can override this weight in your own handler code!
      $form['actions']['#weight'] = 100;

      // We force down a submit button.  You need to have a complete task somewhere.
      $form['actions']['submit'] = [
        '#type' => 'submit',
        // You can override the #value in your own handler.
        '#value' => t('Complete'),
      ];
      call_user_func_array($handler, [
        &$form,
        $this->queueID,
        $this,
      ]);

      // Non-array return types are possible, e.g. RedirectResponse objects.
      if (is_array($form)) {

        // Not overridable in your handler!  we do this on purpose here if this is a known modal based on the task option.
        if ($this->modal == 'modal') {
          $form['actions']['submit']['#ajax'] = [
            // We use our helper method of completeForm to close the modal.
            'callback' => [
              $this,
              'completeForm',
            ],
            'wrapper' => '',
          ];
        }
        $form['return_path'] = [
          '#type' => 'hidden',
          '#default_value' => $this->returnPath,
        ];
      }
    }
    else {
      $task = NULL;
      $task = MaestroEngine::getPluginTask($queueEntry->task_class_name
        ->getString(), $processID, $this->queueID);
      if ($task != NULL) {
        $form = $task
          ->getExecutableForm($this->modal, $this);
      }
    }

    // Non-array return types are possible, e.g. RedirectResponse objects.
    if (!is_array($form)) {
      return $form;
    }

    // We add our own queue ID to the form mix to be absolutely sure we have a queue ID submitted to us.
    $form['maestro_queue_id'] = [
      '#type' => 'hidden',
      '#default_value' => $this->queueID,
    ];

    /*
     * We are using the $form[actions] to house our button commands.  For the submit form, if this ia a modal
     * dialog we're using, we'll add the submit form modal command
     */
  }
  else {

    // There is no queue record.
    $form['error'] = [
      '#markup' => $this
        ->t('The task is no longer valid.'),
    ];
  }
  return $form;
}