You are here

public function SettingsForm::submitForm in Font Awesome Icons 8.2

Same name and namespace in other branches
  1. 8 src/Form/SettingsForm.php \Drupal\fontawesome\Form\SettingsForm::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 ConfigFormBase::submitForm

File

src/Form/SettingsForm.php, line 266

Class

SettingsForm
Defines a form that configures fontawesome settings.

Namespace

Drupal\fontawesome\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // Load the fontawesome libraries so we can use its definitions here.
  $fontawesome_library = $this->libraryDiscovery
    ->getLibraryByName('fontawesome', 'fontawesome.svg');

  // Clear the library cache so we use the updated information.
  $this->libraryDiscovery
    ->clearCachedDefinitions();

  // Set external file defaults.
  $default_location = 'https://use.fontawesome.com/releases/v' . $fontawesome_library['version'] . '/';
  $default_svg_location = $default_location . 'js/all.js';
  $default_webfonts_location = $default_location . 'css/all.css';
  $default_svg_shimfile_location = $default_location . 'js/v4-shims.js';
  $default_webfonts_shimfile_location = $default_location . 'css/v4-shims.css';

  // Use default values if CDN is checked and the locations are blank.
  if ($values['use_cdn']) {
    if (empty($values['external_svg_location']) || $values['external_svg_location'] == $default_webfonts_location || $values['external_svg_location'] == $default_svg_location) {

      // Choose the default depending on method.
      $values['external_svg_location'] = $values['method'] == 'webfonts' ? $default_webfonts_location : $default_svg_location;
    }
    if ($values['use_shim'] && (empty($values['external_shim_location']) || $values['external_shim_location'] == $default_webfonts_shimfile_location || $values['external_shim_location'] == $default_svg_shimfile_location)) {

      // Choose the default depending on method.
      $values['external_shim_location'] = $values['method'] == 'webfonts' ? $default_webfonts_shimfile_location : $default_svg_shimfile_location;
    }
  }

  // Save the updated settings.
  $this
    ->config('fontawesome.settings')
    ->set('tag', $values['tag'])
    ->set('method', $values['method'])
    ->set('load_assets', $values['load_assets'])
    ->set('use_cdn', $values['use_cdn'])
    ->set('external_svg_location', (string) $values['external_svg_location'])
    ->set('external_svg_integrity', (string) $values['external_svg_integrity'])
    ->set('use_shim', $values['use_shim'])
    ->set('external_shim_location', (string) $values['external_shim_location'])
    ->set('allow_pseudo_elements', $values['allow_pseudo_elements'])
    ->set('use_solid_file', $values['use_solid_file'])
    ->set('use_regular_file', $values['use_regular_file'])
    ->set('use_light_file', $values['use_light_file'])
    ->set('use_brands_file', $values['use_brands_file'])
    ->set('use_duotone_file', $values['use_duotone_file'])
    ->set('bypass_validation', $values['bypass_validation'])
    ->save();
  parent::submitForm($form, $form_state);
}