You are here

public function YamlFormSubmissionExporter::buildExportOptionsForm in YAML Form 8

Build export options form.

Parameters

array $form: The form.

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

array $export_options: The default values.

Overrides YamlFormSubmissionExporterInterface::buildExportOptionsForm

File

src/YamlFormSubmissionExporter.php, line 275

Class

YamlFormSubmissionExporter
Form submission exporter.

Namespace

Drupal\yamlform

Code

public function buildExportOptionsForm(array &$form, FormStateInterface $form_state, array $export_options = []) {
  $default_options = $this
    ->getDefaultExportOptions();
  $export_options = NestedArray::mergeDeep($default_options, $export_options);
  $yamlform = $this
    ->getYamlForm();
  $exporter = $this
    ->setExporter($export_options);

  // Get exporter and build #states.
  $exporter_plugins = $this->exporterManager
    ->getInstances($export_options);
  $states_archive = [
    'invisible' => [],
  ];
  $states_options = [
    'invisible' => [],
  ];
  foreach ($exporter_plugins as $plugin_id => $exporter_plugin) {
    if ($exporter_plugin
      ->isArchive()) {
      if ($states_archive['invisible']) {
        $states_archive['invisible'][] = 'or';
      }
      $states_archive['invisible'][] = [
        ':input[name="export[format][exporter]"]' => [
          'value' => $plugin_id,
        ],
      ];
    }
    if (!$exporter_plugin
      ->hasOptions()) {
      if ($states_options['invisible']) {
        $states_options['invisible'][] = 'or';
      }
      $states_options['invisible'][] = [
        ':input[name="export[format][exporter]"]' => [
          'value' => $plugin_id,
        ],
      ];
    }
  }
  $form['export']['#tree'] = TRUE;
  $form['export']['format'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Format options'),
    '#open' => TRUE,
  ];
  $form['export']['format']['exporter'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Export format'),
    '#options' => $this->exporterManager
      ->getOptions(),
    '#default_value' => $export_options['exporter'],
    // Below .js-yamlform-exporter is used for exporter configuration form
    // #states.
    // @see \Drupal\yamlform\YamlFormExporterBase::buildConfigurationForm
    '#attributes' => [
      'class' => [
        'js-yamlform-exporter',
      ],
    ],
  ];
  foreach ($exporter_plugins as $plugin_id => $exporter) {
    $form['export']['format'] = $exporter
      ->buildConfigurationForm($form['export']['format'], $form_state);
  }

  // Header.
  $form['export']['header'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Header options'),
    '#open' => TRUE,
    '#states' => $states_options,
  ];
  $form['export']['header']['header_format'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Column header format'),
    '#description' => $this
      ->t('Choose whether to show the element label or element key in each column header.'),
    '#required' => TRUE,
    '#options' => [
      'label' => $this
        ->t('Element titles (label)'),
      'key' => $this
        ->t('Element keys (key)'),
    ],
    '#default_value' => $export_options['header_format'],
  ];
  $form['export']['header']['header_prefix'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Include an element's title with all sub elements and values in each column header."),
    '#return_value' => TRUE,
    '#default_value' => $export_options['header_prefix'],
  ];
  $form['export']['header']['header_prefix_label_delimiter'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Column header label delimiter'),
    '#required' => TRUE,
    '#default_value' => $export_options['header_prefix_label_delimiter'],
  ];
  $form['export']['header']['header_prefix_key_delimiter'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Column header key delimiter'),
    '#required' => TRUE,
    '#default_value' => $export_options['header_prefix_key_delimiter'],
  ];
  if ($yamlform) {
    $form['export']['header']['header_prefix_label_delimiter']['#states'] = [
      'visible' => [
        ':input[name="export[header][header_prefix]"]' => [
          'checked' => TRUE,
        ],
        ':input[name="export[header][header_format]"]' => [
          'value' => 'label',
        ],
      ],
    ];
    $form['export']['header']['header_prefix_key_delimiter']['#states'] = [
      'visible' => [
        ':input[name="export[header][header_prefix]"]' => [
          'checked' => TRUE,
        ],
        ':input[name="export[header][header_format]"]' => [
          'value' => 'key',
        ],
      ],
    ];
  }

  // Build element specific export forms.
  // Grouping everything in $form['export']['elements'] so that element handlers can
  // assign #weight to its export options form.
  $form['export']['elements'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'form-item',
      ],
    ],
    '#states' => $states_options,
  ];
  $element_types = $this
    ->getYamlFormElementTypes();
  $element_handlers = $this->elementManager
    ->getInstances();
  foreach ($element_handlers as $element_type => $element_handler) {
    if (empty($element_types) || isset($element_types[$element_type])) {
      $element_handler
        ->buildExportOptionsForm($form['export']['elements'], $form_state, $export_options);
    }
  }

  // All the remain options are only applicable to a form's export.
  // @see Drupal\yamlform\Form\YamlFormResultsExportForm
  if (!$yamlform) {
    return;
  }

  // Elements.
  $form['export']['columns'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Column options'),
    '#states' => $states_options,
  ];
  $form['export']['columns']['excluded_columns'] = [
    '#type' => 'yamlform_excluded_columns',
    '#description' => $this
      ->t('The selected columns will be included in the export.'),
    '#yamlform' => $yamlform,
    '#default_value' => $export_options['excluded_columns'],
  ];

  // Download options.
  $form['export']['download'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Download options'),
    '#open' => TRUE,
  ];
  $form['export']['download']['download'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Download export file'),
    '#description' => $this
      ->t('If checked, the export file will be automatically download to your local machine. If unchecked, the export file will be displayed as plain text within your browser.'),
    '#default_value' => $export_options['download'],
    '#access' => !$this
      ->requiresBatch(),
    '#states' => $states_archive,
  ];
  $form['export']['download']['files'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Download uploaded files'),
    '#description' => $this
      ->t('If checked, the exported file and any submission file uploads will be download in a gzipped tar file.'),
    '#access' => $yamlform
      ->hasManagedFile(),
    '#states' => [
      'invisible' => [
        ':input[name="export[download][download]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
    '#default_value' => $yamlform
      ->hasManagedFile() ? $export_options['files'] : 0,
  ];
  $source_entity = $this
    ->getSourceEntity();
  if (!$source_entity) {
    $entity_types = $this->entityStorage
      ->getSourceEntityTypes($yamlform);
    if ($entity_types) {
      $form['export']['download']['submitted'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Submitted to'),
        '#description' => $this
          ->t('Select the entity type and then enter the entity id.'),
        '#field_prefix' => '<div class="container-inline">',
        '#field_suffix' => '</div>',
      ];
      $form['export']['download']['submitted']['entity_type'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Entity type'),
        '#title_display' => 'Invisible',
        '#options' => [
          '' => $this
            ->t('All'),
        ] + $entity_types,
        '#default_value' => $export_options['entity_type'],
      ];
      $form['export']['download']['submitted']['entity_id'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Entity id'),
        '#title_display' => 'Invisible',
        '#min' => 1,
        '#size' => 10,
        '#default_value' => $export_options['entity_id'],
        '#states' => [
          'invisible' => [
            ':input[name="export[download][submitted][entity_type]"]' => [
              'value' => '',
            ],
          ],
        ],
      ];
    }
  }
  $form['export']['download']['range_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Limit to'),
    '#options' => [
      'all' => $this
        ->t('All'),
      'latest' => $this
        ->t('Latest'),
      'serial' => $this
        ->t('Submission number'),
      'sid' => $this
        ->t('Submission ID'),
      'date' => $this
        ->t('Date'),
    ],
    '#default_value' => $export_options['range_type'],
  ];
  $form['export']['download']['latest'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'container-inline',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="export[download][range_type]"]' => [
          'value' => 'latest',
        ],
      ],
    ],
    'range_latest' => [
      '#type' => 'number',
      '#title' => $this
        ->t('Number of submissions'),
      '#min' => 1,
      '#default_value' => $export_options['range_latest'],
    ],
  ];
  $ranges = [
    'serial' => [
      '#type' => 'number',
    ],
    'sid' => [
      '#type' => 'number',
    ],
    'date' => [
      '#type' => 'date',
    ],
  ];
  foreach ($ranges as $key => $range_element) {
    $form['export']['download'][$key] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
      '#states' => [
        'visible' => [
          ':input[name="export[download][range_type]"]' => [
            'value' => $key,
          ],
        ],
      ],
    ];
    $form['export']['download'][$key]['range_start'] = $range_element + [
      '#title' => $this
        ->t('From'),
      '#default_value' => $export_options['range_start'],
    ];
    $form['export']['download'][$key]['range_end'] = $range_element + [
      '#title' => $this
        ->t('To'),
      '#default_value' => $export_options['range_end'],
    ];
  }
  $form['export']['download']['sticky'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Starred/flagged submissions'),
    '#description' => $this
      ->t('If checked, only starred/flagged submissions will be downloaded. If unchecked, all submissions will downloaded.'),
    '#default_value' => $export_options['sticky'],
  ];

  // If drafts are allowed, provide options to filter download based on
  // submission state.
  $form['export']['download']['state'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Submission state'),
    '#default_value' => $export_options['state'],
    '#options' => [
      'all' => $this
        ->t('Completed and draft submissions'),
      'completed' => $this
        ->t('Completed submissions only'),
      'draft' => $this
        ->t('Drafts only'),
    ],
    '#access' => $yamlform
      ->getSetting('draft'),
  ];
}