You are here

public function MaestroTemplateBuilderEditTask::buildForm in Maestro 8.2

Same name and namespace in other branches
  1. 3.x modules/maestro_template_builder/src/Form/MaestroTemplateBuilderEditTask.php \Drupal\maestro_template_builder\Form\MaestroTemplateBuilderEditTask::buildForm()

Ajax callback for add-new-form button click.

Overrides FormInterface::buildForm

File

modules/maestro_template_builder/src/Form/MaestroTemplateBuilderEditTask.php, line 142

Class

MaestroTemplateBuilderEditTask
Maestro Template Editor Edit a Task Form.

Namespace

Drupal\maestro_template_builder\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $templateMachineName = '') {
  $taskID = Xss::filter($_SESSION['maestro_template_builder']['maestro_editing_task']);
  $template = MaestroEngine::getTemplate($templateMachineName);
  $task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskID);
  $task['form_state'] = $form_state;

  // Need to validate this taskID and template to ensure that they exist.
  if ($taskID == '' || $template == NULL || $task == NULL) {
    $form = [
      '#title' => t('Error!'),
      '#markup' => t('The task or template you are attempting to edit does not exist'),
    ];
    return $form;
  }
  $form = [
    '#title' => $this
      ->t('Editing Task') . ': ' . $task['label'] . '(' . $taskID . ')',
    '#prefix' => '<div id="edit-task-form">',
    '#suffix' => '</div>',
  ];
  $form['save_task_notification'] = [
    '#markup' => $this
      ->t('Task Saved'),
    '#prefix' => '<div id="save-task-notificaiton" class="messages messages--status">',
    '#suffix' => '</div>',
  ];

  // Get a handle to the task plugin.
  $executableTask = MaestroEngine::getPluginTask($task['tasktype']);

  // Get the base edit form that all tasks adhere to.
  $form += $executableTask
    ->getBaseEditForm($task, $templateMachineName);

  // We now will pull back the edit form provided to us by the task itself.
  // this gives ultimate flexibility to developers.
  // even form alters work on this form by allowing the dev to detect what task_id is being edited
  // and get the task type and do any modifications on it from there.
  $form += $executableTask
    ->getTaskEditForm($task, $templateMachineName);

  // Now is this thing interactive or not?
  // if so, we show the assignment and notification tabs.  If not, leave it out.
  if ($executableTask
    ->isInteractive()) {
    $form += $executableTask
      ->getAssignmentsAndNotificationsForm($task, $templateMachineName);
  }

  // Save button in an actions bar:
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save Task'),
    '#required' => TRUE,
    '#ajax' => [
      // Use saveFrom rather than submitForm to alleviate the issue of calling a save handler twice.
      'callback' => [
        $this,
        'saveForm',
      ],
      'wrapper' => '',
    ],
  ];
  $form['actions']['close'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Close'),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'cancelForm',
      ],
      'wrapper' => '',
    ],
  ];
  return $form;
}