You are here

protected function ContactStorageExportForm::exportForm in Contact Storage Export 8

Form for exporting a particular form.

Parameters

string $contact_form: The machine name of the contact form.

array $form: The Drupal form.

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

Return value

array The Drupal form.

1 call to ContactStorageExportForm::exportForm()
ContactStorageExportForm::buildForm in src/Form/ContactStorageExportForm.php
Form constructor.

File

src/Form/ContactStorageExportForm.php, line 51

Class

ContactStorageExportForm
Settings form for config devel.

Namespace

Drupal\contact_storage_export\Form

Code

protected function exportForm($contact_form, array $form, FormStateInterface $form_state) {
  $contact_message = $this
    ->getSingleMessage($contact_form);
  if (!$contact_message) {
    $message = $this
      ->t('There are no messages to be exported for this form.');
    $messenger = \Drupal::messenger();
    $messenger
      ->addWarning($message);
  }
  else {

    // Store the requested contact form.
    $form['contact_form'] = [
      '#type' => 'hidden',
      '#value' => $contact_form,
    ];

    // Allow the editor to only export messages since last export.
    $form['since_last_export'] = [
      '#type' => 'checkbox',
      '#title' => t('Only export new messages since the last export'),
    ];
    $last_id = ContactStorageExport::getLastExportId($contact_form);
    if (!$this
      ->getSingleMessage($contact_form, $last_id)) {
      $form['since_last_export']['#disabled'] = TRUE;
      $form['since_last_export']['#description'] = $this
        ->t('This checkbox has been disabled as there are not new messages since your last export.');
    }
    $form['advanced'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced Settings'),
      '#open' => FALSE,
    ];

    // Allow the editor to control which columns are to be exported.
    $labels = \Drupal::service('contact_storage_export.exporter')
      ->getLabels($contact_message);
    unset($labels['uuid']);
    $form['advanced']['columns'] = [
      '#type' => 'checkboxes',
      '#title' => t('Columns to be exported'),
      '#required' => TRUE,
      '#options' => $labels,
      '#default_value' => array_keys($labels),
    ];

    // Allow the editor to override the default file name.
    $filename = str_replace('_', '-', $contact_form);
    $filename .= '-' . date('Y-m-d--h-i-s');
    $filename .= '.csv';
    $form['advanced']['filename'] = [
      '#type' => 'textfield',
      '#title' => t('File name'),
      '#required' => TRUE,
      '#default_value' => $filename,
      '#maxlength' => 240,
    ];
    $form['advanced']['date_format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Created date format'),
      '#options' => $this
        ->getDateFormats(),
      '#default_value' => 'medium',
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Export'),
      '#button_type' => 'primary',
    ];

    // Open form in new window as our batch finish downloads the file.
    if (!isset($form['#attributes'])) {
      $form['#attributes'] = [];
    }
  }
  return $form;
}