You are here

public function WebformSubmissionResendForm::buildForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/WebformSubmissionResendForm.php \Drupal\webform\Form\WebformSubmissionResendForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/WebformSubmissionResendForm.php, line 71

Class

WebformSubmissionResendForm
Defines a webform that resends webform submission.

Namespace

Drupal\webform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission = NULL) {
  $this->webformSubmission = $webform_submission;

  // Apply variants to the webform.
  $webform = $webform_submission
    ->getWebform();
  $webform
    ->applyVariants($webform_submission);

  // Get header.
  $header = [];
  $header['title'] = [
    'data' => $this
      ->t('Title / Description'),
  ];
  $header['id'] = [
    'data' => $this
      ->t('ID'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  $header['summary'] = [
    'data' => $this
      ->t('summary'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  $header['status'] = [
    'data' => $this
      ->t('Status'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];

  // Get options.
  $options = $this
    ->getMessageHandlerOptions($webform_submission);

  // Get message handler id from form state or use the first message handler.
  if (!empty($form_state
    ->getValue('message_handler_id'))) {
    $message_handler_id = $form_state
      ->getValue('message_handler_id');
  }
  else {
    $message_handler_id = key($options);
  }

  // Display message handler with change message Ajax submit button.
  $form['message_handler'] = [];
  $form['message_handler']['message_handler_id'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#js_select' => TRUE,
    '#empty' => $this
      ->t('No messages are available.'),
    '#multiple' => FALSE,
    '#default_value' => $message_handler_id,
  ];

  // Message.
  $message_handler = $this->webformSubmission
    ->getWebform()
    ->getHandler($message_handler_id);
  $message = $message_handler
    ->getMessage($webform_submission);
  $resend_form = $message_handler
    ->resendMessageForm($message);
  $form['message'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Message'),
    '#open' => TRUE,
    '#tree' => TRUE,
  ] + $resend_form;

  // Add resend button.
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Resend message'),
  ];

  // Add submission navigation.
  $source_entity = $this->requestHandler
    ->getCurrentSourceEntity('webform_submission');
  $form['navigation'] = [
    '#type' => 'webform_submission_navigation',
    '#webform_submission' => $webform_submission,
    '#weight' => -20,
  ];
  $form['information'] = [
    '#type' => 'webform_submission_information',
    '#webform_submission' => $webform_submission,
    '#source_entity' => $source_entity,
    '#weight' => -19,
  ];
  $form['#attached']['library'][] = 'webform/webform.admin';
  $this
    ->buildAjaxElement('webform-message-handler', $form['message'], $form['message_handler']['message_handler_id'], $form['message_handler']);
  return $form;
}