You are here

public function ManageFilesForm::submitForm in Minify JS 8

Same name and namespace in other branches
  1. 8.2 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 188

Class

ManageFilesForm
Displays a list of detected javascript files and allows actions to be performed on them

Namespace

Drupal\minifyjs\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if (count($form_state
    ->getValue('files'))) {
    $files = minifyjs_load_all_files();

    // 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 = array();
    foreach ($selected_files as $fid) {
      switch ($form_state
        ->getValue('action')) {

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

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

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

    // Build the batch.
    $batch = array(
      '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);
  }
}