You are here

public function EntityExportCsv::submitForm in Entity Export CSV 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/EntityExportCsv.php, line 327

Class

EntityExportCsv
Define entity export csv form.

Namespace

Drupal\entity_export_csv\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->cleanValues()
    ->getValues();
  if (!isset($values['entity_type']) || !isset($values['bundle'])) {
    return;
  }
  $save = $values['fields']['save'];
  unset($values['fields']['save']);
  $delimiter = $values['fields']['delimiter'];
  unset($values['fields']['delimiter']);
  $entity_type_id = $values['entity_type'];
  $bundle_id = $values['bundle'];
  $fields = $values['fields'];
  $user_data = $this->userData
    ->get('entity_export_csv', $this
    ->currentUser()
    ->id(), 'entity_export_csv') ?: [];
  if (!empty($save)) {
    $user_data[$entity_type_id][$bundle_id] = [
      'delimiter' => $delimiter,
    ] + $fields;
  }
  else {
    if (isset($user_data[$entity_type_id][$bundle_id])) {
      unset($user_data[$entity_type_id][$bundle_id]);
    }
  }
  $this->userData
    ->set('entity_export_csv', $this
    ->currentUser()
    ->id(), 'entity_export_csv', $user_data);
  $triggering_element = $form_state
    ->getTriggeringElement();
  if ($triggering_element['#name'] === 'save') {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Settings successfully updated.'));
    return;
  }
  $conditions = !empty($fields['conditions']) ? $fields['conditions'] : [];
  unset($fields['conditions']);
  $langcode = isset($values['langcode']) ? $values['langcode'] : NULL;
  $entity_types = $this->manager
    ->getContentEntityTypesEnabled(TRUE);
  $bundles = $this->manager
    ->getBundlesEnabledPerEntityType($entity_type_id, TRUE);
  $batch = [
    'title' => $this
      ->t('Exporting @entity_type of type @bundle', [
      '@bundle' => $bundles[$bundle_id],
      '@entity_type' => $entity_types[$entity_type_id],
    ]),
    'operations' => [
      [
        '\\Drupal\\entity_export_csv\\EntityExportCsvBatch::export',
        [
          $entity_type_id,
          $bundle_id,
          $fields,
          $langcode,
          $conditions,
          $delimiter,
        ],
      ],
    ],
    'finished' => '\\Drupal\\entity_export_csv\\EntityExportCsvBatch::finished',
  ];
  batch_set($batch);
}