View source
<?php
namespace Drupal\maestro_template_builder\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\maestro_template_builder\Ajax\FireJavascriptCommand;
class MaestroTemplateBuilderEditTask extends FormBase {
public function getFormId() {
return 'template_edit_task';
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$templateMachineName = $form_state
->getValue('template_machine_name');
$taskID = $form_state
->getValue('task_id');
$template = MaestroEngine::getTemplate($templateMachineName);
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskID);
$executableTask = MaestroEngine::getPluginTask($task['tasktype']);
$executableTask
->validateTaskEditForm($form, $form_state);
}
public function cancelForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$_SESSION['maestro_template_builder']['maestro_editing_task'] = '';
$response
->addCommand(new HtmlCommand('#edit-task-form', $form));
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#edit-task-form', $form));
return $response;
}
else {
$templateMachineName = $form_state
->getValue('template_machine_name');
$taskID = $form_state
->getValue('task_id');
$template = MaestroEngine::getTemplate($templateMachineName);
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskID);
$executableTask = MaestroEngine::getPluginTask($task['tasktype']);
$executableTask
->prepareTaskForSave($form, $form_state, $task);
$result = $executableTask
->saveTask($form, $form_state, $task);
if ($result === FALSE) {
\Drupal::messenger()
->addError(t('Error saving your task.'));
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
}
}
$form_state
->setRebuild(TRUE);
}
public function saveForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response = new AjaxResponse();
$response
->addCommand(new HtmlCommand('#edit-task-form', $form));
return $response;
}
$templateMachineName = $form_state
->getValue('template_machine_name');
$taskID = $form_state
->getValue('task_id');
$task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskID);
$update = [
'label' => $task['label'],
'taskid' => $task['id'],
'body' => 'placeholder',
'participate_in_workflow_status_stage' => $task['participate_in_workflow_status_stage'],
'workflow_status_stage_number' => $task['workflow_status_stage_number'],
'workflow_status_stage_message' => $task['workflow_status_stage_message'],
];
$response = new AjaxResponse();
$response
->addCommand(new FireJavascriptCommand('maestroUpdateMetaData', $update));
$response
->addCommand(new HtmlCommand('#edit-task-form', $form));
$response
->addCommand(new FireJavascriptCommand('maestroShowSavedMessage', []));
return $response;
}
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;
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>',
];
$executableTask = MaestroEngine::getPluginTask($task['tasktype']);
$form += $executableTask
->getBaseEditForm($task, $templateMachineName);
$form += $executableTask
->getTaskEditForm($task, $templateMachineName);
if ($executableTask
->isInteractive()) {
$form += $executableTask
->getAssignmentsAndNotificationsForm($task, $templateMachineName);
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save'] = [
'#type' => 'submit',
'#value' => $this
->t('Save Task'),
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'saveForm',
],
'wrapper' => '',
],
];
$form['actions']['close'] = [
'#type' => 'button',
'#value' => $this
->t('Close'),
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'cancelForm',
],
'wrapper' => '',
],
];
return $form;
}
}