You are here

public function MaestroSpawnSubFlowTask::getTaskEditForm in Maestro 3.x

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

Method to allow a task to add their own fields to the task edit form.

Parameters

array $task: This is the array representation of the task from the configuration entity.

string $templateMachineName: The Maestro template machine name.

Return value

array Array Must return form declaration fields for the task editor

Overrides MaestroEngineTaskInterface::getTaskEditForm

File

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

Class

MaestroSpawnSubFlowTask
Maestro Spawn Sub Flow Task Plugin.

Namespace

Drupal\maestro\Plugin\EngineTasks

Code

public function getTaskEditForm(array $task, $templateMachineName) {
  $form = [
    '#markup' => t('Spawn Sub Flow Edit'),
  ];
  $maestro_templates = MaestroEngine::getTemplates();
  $templates = [];
  $templates['none'] = $this
    ->t('Please Select Template');
  foreach ($maestro_templates as $machine_name => $template) {
    $templates[$machine_name] = $template
      ->label();
  }
  $form['maestro_task_machine_name'] = [
    '#type' => 'hidden',
    '#value' => $task['id'],
  ];
  $form['maestro_template_machine_name'] = [
    '#type' => 'hidden',
    '#value' => $templateMachineName,
  ];
  $selected_template = '';
  if (isset($task['data']['maestro_template'])) {
    $selected_template = $task['data']['maestro_template'];
  }
  $form['maestro_template'] = [
    '#type' => 'select',
    '#options' => $templates,
    '#title' => $this
      ->t('Choose the Maestro Template'),
    '#default_value' => $selected_template,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => [
        $this,
        'subFlowChoiceHandlerCallback',
      ],
      'event' => 'change',
      'wrapper' => 'handler-ajax-refresh-wrapper',
      'progress' => [
        'type' => 'throbber',
        'message' => NULL,
      ],
    ],
  ];
  $template_machine_name = $selected_template;
  $form_state_template = $task['form_state']
    ->getValue('maestro_template');
  if (isset($form_state_template)) {
    $template_machine_name = $form_state_template;
  }
  if ($template_machine_name != 'none' && $template_machine_name != '') {
    $template = MaestroEngine::getTemplate($template_machine_name);
    $form['maestro_sub_flow_label'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Chosen Template') . ': ' . $template
        ->label(),
      '#url' => Url::fromRoute('maestro_template_builder', [
        'templateMachineName' => $template_machine_name,
      ]),
      '#attributes' => [
        'class' => [
          'handler-help-message',
        ],
        'target' => '_new',
        'id' => [
          'handler-ajax-refresh-wrapper',
        ],
      ],
    ];
  }
  else {
    $form['maestro_sub_flow_label'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('Please choose a template.'),
      '#attributes' => [
        'class' => [
          'handler-help-message',
        ],
        'id' => [
          'handler-ajax-refresh-wrapper',
        ],
      ],
    ];
  }
  $form['maestro_sub_flow_settings'] = [
    '#type' => 'details',
    '#open' => TRUE,
    '#description' => $this
      ->t('Choose the variables you wish to send from the parent to the child. Variables will be prefixed with "maestro_parent_" when injected into the sub-process.'),
    '#title' => $this
      ->t('Variable Selection'),
  ];
  $template = MaestroEngine::getTemplate($templateMachineName);
  $form['maestro_sub_flow_settings']['variables'] = [];

  // Get all variables other than some of the exclusive Maestro variables.
  foreach ($template->variables as $var_name => $var_definition) {
    $form['maestro_sub_flow_settings']['variables']['variable_' . $var_name] = [
      '#type' => 'checkbox',
      '#title' => $var_name,
      '#default_value' => @array_key_exists($var_name, $task['data']['variables']) ? TRUE : FALSE,
      '#attributes' => [
        'autocomplete' => 'off',
      ],
    ];
  }
  $form['#cache'] = [
    'max-age' => 0,
  ];

  // Hi Firefox, I see you caching.
  $form['#attributes']['autocomplete'] = 'off';
  return $form;
}