You are here

public function JavascriptLibrariesCustomAddForm::submitForm in JavaScript Libraries Manager 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/JavascriptLibrariesCustomAddForm.php, line 136
Contains \Drupal\javascript_libraries\Form\JavascriptLibrariesCustomAddForm.

Class

JavascriptLibrariesCustomAddForm

Namespace

Drupal\javascript_libraries\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $custom = \Drupal::config('javascript_libraries.settings')
    ->get('javascript_libraries_custom_libraries');
  switch ($form_state
    ->getValue('library_type')) {
    case 'external':
      $id = 'ext-' . db_next_id();
      $lib_id = $form_state
        ->getValue('lib_id');
      if (isset($lib_id) && !empty($lib_id)) {
        $id = $lib_id;
      }
      $custom[$id] = array(
        'id' => $id,
        'type' => 'external',
        'scope' => $form_state
          ->getValue('scope'),
        'name' => $form_state
          ->getValue('name'),
        'weight' => $form_state
          ->getValue('weight'),
        'uri' => $form_state
          ->getValue('external_url'),
        'cache' => $form_state
          ->getValue('cache_external'),
      );
      break;
    case 'file':
      $fid = $form_state
        ->getValue(array(
        'js_file_upload',
        0,
      ));
      $file = \Drupal\file\Entity\File::load($fid);
      $id = 'file-' . $fid;
      $lib_id = $form_state
        ->getValue('lib_id');
      if (isset($lib_id) && !empty($lib_id)) {
        $id = $lib_id;
      }
      $custom[$id] = array(
        'type' => 'file',
        'scope' => $form_state
          ->getValue('scope'),
        'weight' => $form_state
          ->getValue('weight'),
        'id' => $id,
        'name' => $form_state
          ->getValue('name'),
        'fid' => $fid,
        'uri' => $file
          ->getFileUri(),
      );
      break;
  }
  \Drupal::configFactory()
    ->getEditable('javascript_libraries.settings')
    ->set('javascript_libraries_custom_libraries', $custom)
    ->save();
  drupal_set_message('Your library has been saved . Please configure the region and weight.');
  $form_state
    ->setRedirect('javascript_libraries.custom_form');
}