You are here

public function ManageFilesForm::submitForm in Minify JS 8.2

Same name and namespace in other branches
  1. 8 src/Form/ManageFilesForm.php \Drupal\minifyjs\Form\ManageFilesForm::submitForm()

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/ManageFilesForm.php, line 189

Class

ManageFilesForm
Manage files form class.

Namespace

Drupal\minifyjs\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if (count($form_state
    ->getValue('files'))) {
    $files = \Drupal::service('minifyjs')
      ->loadAllFiles();

    // Get the files to process.
    $selected_files = [];
    if ($form_state
      ->getValue('scope') == 'selected') {
      foreach ($form_state
        ->getValue('files') as $fid => $selected) {
        if ($selected) {
          $selected_files[] = $fid;
        }
      }
    }
    else {
      $selected_files = array_keys($files);
    }

    // Build operations.
    $operations = [];
    foreach ($selected_files as $fid) {
      switch ($form_state
        ->getValue('action')) {

        // Minify all files.
        case 'minify':
          $operations[] = [
            'minifyjs_batch_minify_file_operation',
            [
              $fid,
            ],
          ];
          break;

        // Minify files that have not yet been minified.
        case 'minify_skip':
          $file = $files[$fid];
          if (!$file->minified_uri) {
            $operations[] = [
              'minifyjs_batch_minify_file_operation',
              [
                $fid,
              ],
            ];
          }
          break;

        // Restore un-minified version of a file.
        case 'restore':
          $operations[] = [
            'minifyjs_batch_remove_minified_file_operation',
            [
              $fid,
            ],
          ];
          break;
      }
    }

    // Build the batch.
    $batch = [
      'operations' => $operations,
      'file' => drupal_get_path('module', 'minifyjs') . '/minifyjs.module',
      'error_message' => t('There was an unexpected error while processing the batch.'),
      'finished' => 'minifyjs_batch_finished',
    ];
    switch ($form_state
      ->getValue('action')) {
      case 'minify':
        $batch['title'] = t('Minifying Javascript Files.');
        $batch['init_message'] = t('Initializing minify javascript files batch.');
        break;
      case 'restore':
        $batch['title'] = t('Restoring Un-Minified Javascript Files.');
        $batch['init_message'] = t('Initializing restore un-minified javascript files batch.');
        break;
    }

    // Start the batch.
    batch_set($batch);
  }
}