You are here

public function YamlFormSubmissionResendForm::buildForm in YAML Form 8

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/YamlFormSubmissionResendForm.php, line 67

Class

YamlFormSubmissionResendForm
Defines a form that resends form submission.

Namespace

Drupal\yamlform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, YamlFormSubmissionInterface $yamlform_submission = NULL) {
  $this->yamlformSubmission = $yamlform_submission;
  $handlers = $yamlform_submission
    ->getYamlForm()
    ->getHandlers();

  /** @var \Drupal\yamlform\YamlFormHandlerMessageInterface[] $message_handlers */
  $message_handlers = [];
  foreach ($handlers as $handler_id => $handler) {
    if ($handler instanceof EmailYamlFormHandler) {
      $message_handlers[$handler_id] = $handler;
    }
  }

  // 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 = [];
  foreach ($message_handlers as $index => $message_handler) {
    $message = $message_handler
      ->getMessage($this->yamlformSubmission);
    $options[$index]['title'] = [
      'data' => [
        'label' => [
          '#type' => 'label',
          '#title' => $message_handler
            ->label() . ': ' . $message_handler
            ->description(),
          '#title_display' => NULL,
          '#for' => 'edit-message-handler-id-' . str_replace('_', '-', $message_handler
            ->getHandlerId()),
        ],
      ],
    ];
    $options[$index]['id'] = [
      'data' => $message_handler
        ->getHandlerId(),
    ];
    $options[$index]['summary'] = [
      'data' => $message_handler
        ->getMessageSummary($message),
    ];
    $options[$index]['status'] = $message_handler
      ->isEnabled() ? $this
      ->t('Enabled') : $this
      ->t('Disabled');
  }

  // Get message handler id.
  if (empty($form_state
    ->getValue('message_handler_id'))) {
    reset($options);
    $message_handler_id = key($options);
    $form_state
      ->setValue('message_handler_id', $message_handler_id);
  }
  else {
    $message_handler_id = $form_state
      ->getValue('message_handler_id');
  }
  $message_handler = $this
    ->getMessageHandler($form_state);
  $form['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,
    '#ajax' => [
      'callback' => '::updateMessage',
      'wrapper' => 'edit-yamlform-message-wrapper',
    ],
  ];

  // Message.
  $form['message'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Message'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#prefix' => '<div id="edit-yamlform-message-wrapper">',
    '#suffix' => '</div>',
  ];
  $message = $message_handler
    ->getMessage($yamlform_submission);
  $form['message'] += $message_handler
    ->resendMessageForm($message);

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

  // Add submission navigation.
  $source_entity = $this->requestHandler
    ->getCurrentSourceEntity('yamlform_submission');
  $form['navigation'] = [
    '#theme' => 'yamlform_submission_navigation',
    '#yamlform_submission' => $yamlform_submission,
    '#weight' => -20,
  ];
  $form['information'] = [
    '#theme' => 'yamlform_submission_information',
    '#yamlform_submission' => $yamlform_submission,
    '#source_entity' => $source_entity,
    '#weight' => -19,
  ];
  $form['#attached']['library'][] = 'yamlform/yamlform.admin';
  return $form;
}