You are here

public function WebformScheduledTaskForm::form in Webform Scheduled Tasks 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/WebformScheduledTaskForm.php, line 35

Class

WebformScheduledTaskForm
The scheduled task form.

Namespace

Drupal\webform_scheduled_tasks\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\webform_scheduled_tasks\Entity\WebformScheduledTaskInterface $schedule */
  $schedule = $this->entity;
  if ($schedule
    ->isNew()) {
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Task name'),
      '#maxlength' => 255,
      '#default_value' => $schedule
        ->label(),
      '#required' => TRUE,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $schedule
        ->id(),
      '#machine_name' => [
        'exists' => [
          WebformScheduledTask::class,
          'load',
        ],
      ],
    ];
    $form['task_type'] = [
      '#title' => $this
        ->t('Task to run'),
      '#type' => 'select',
      '#required' => TRUE,
      '#description' => $this
        ->t('Select the task which should be run when this scheduled task is executed.'),
      '#options' => $this
        ->pluginManagerOptionsList('plugin.manager.webform_scheduled_tasks.task'),
    ];
    $form['result_set_type'] = [
      '#title' => $this
        ->t('Submissions to process'),
      '#type' => 'select',
      '#required' => TRUE,
      '#description' => $this
        ->t('Select the results which should be used for .'),
      '#options' => $this
        ->pluginManagerOptionsList('plugin.manager.webform_scheduled_tasks.result_set'),
    ];
    $form['webform'] = [
      '#type' => 'value',
      '#value' => $this->webform
        ->id(),
    ];
  }
  else {
    $form['scheduled_task_info'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Scheduled task information'),
      'children' => [
        'task' => [
          '#type' => 'container',
          '#markup' => $this
            ->t('<strong>Task type:</strong> @type', [
            '@type' => $schedule
              ->getTaskPlugin()
              ->label(),
          ]),
        ],
        'result_set' => [
          '#type' => 'container',
          '#markup' => $this
            ->t('<strong>Result set type:</strong> @type', [
            '@type' => $schedule
              ->getResultSetPlugin()
              ->label(),
          ]),
        ],
        'status' => [
          '#type' => 'container',
          '#markup' => $this
            ->t('<strong>Status:</strong> @status', [
            '@status' => $schedule
              ->isHalted() ? $this
              ->t('Halted') : $this
              ->t('Active'),
          ]),
        ],
      ],
    ];
    if ($schedule
      ->isHalted()) {
      $form['scheduled_task_info']['children']['halted_info'] = [
        'message' => [
          '#type' => 'container',
          '#markup' => $this
            ->t('<strong>Halted with message:</strong> @message', [
            '@message' => $schedule
              ->getHaltedReason(),
          ]),
        ],
        'resume' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Resume task'),
          '#description' => $this
            ->t('If the reason this task was halted has been resolved and operations can resume safely, this task may be resumed.'),
          '#submit' => [
            '::submitForm',
            '::resume',
          ],
        ],
      ];
    }
    $form['schedule_settings'] = [
      '#type' => 'details',
      '#open' => TRUE,
      '#tree' => FALSE,
      '#title' => $this
        ->t('Schedule'),
      'children' => [],
    ];
    $form['schedule_settings']['next_run'] = [
      '#title' => $this
        ->t('Next scheduled run'),
      '#type' => 'datetime',
      '#description' => $this
        ->t('You may use this field to manually adjust the next time the task will run. If left blank this calculated from the current time.'),
      '#default_value' => $schedule
        ->getNextTaskRunDate() ? DrupalDateTime::createFromTimestamp($schedule
        ->getNextTaskRunDate()) : NULL,
    ];
    $form['schedule_settings']['interval'] = [
      '#type' => 'container',
      '#tree' => TRUE,
    ];
    $form['schedule_settings']['interval']['amount'] = [
      '#title' => $this
        ->t('Interval'),
      '#type' => 'number',
      '#field_prefix' => $this
        ->t('Run this scheduled task every') . ' ',
      '#required' => TRUE,
      '#min' => 1,
      '#wrapper_attributes' => [
        'style' => 'display: inline-block;',
      ],
      '#default_value' => $schedule
        ->getRunIntervalAmount(),
    ];
    $form['schedule_settings']['interval']['multiplier'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#wrapper_attributes' => [
        'style' => 'display: inline-block;',
      ],
      '#options' => [
        60 => $this
          ->t('Minutes'),
        60 * 60 => $this
          ->t('Hours'),
        60 * 60 * 24 => $this
          ->t('Days'),
        60 * 60 * 24 * 7 => $this
          ->t('Weeks'),
      ],
      '#default_value' => $schedule
        ->getRunIntervalMultiplier(),
    ];
    $form['task_settings'] = $this
      ->createPluginForm('task_settings', $this
      ->t('Task settings'), $schedule
      ->getTaskPlugin(), $form, $form_state);
    $form['result_set_settings'] = $this
      ->createPluginForm('result_set_settings', $this
      ->t('Result set settings'), $schedule
      ->getResultSetPlugin(), $form, $form_state);
  }
  return $form;
}