You are here

public static function SystemThemeSettings::submitFormElement in Express 8

Form submission handler.

Parameters

\Drupal\bootstrap\Utility\Element $form: The Element object that comprises the form.

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

Overrides FormBase::submitFormElement

File

themes/contrib/bootstrap/src/Plugin/Form/SystemThemeSettings.php, line 117
Contains \Drupal\bootstrap\Plugin\Form\SystemThemeSettings.

Class

SystemThemeSettings
Implements hook_form_system_theme_settings_alter().

Namespace

Drupal\bootstrap\Plugin\Form

Code

public static function submitFormElement(Element $form, FormStateInterface $form_state) {
  $theme = self::getTheme($form, $form_state);
  if (!$theme) {
    return;
  }
  $cache_tags = [];
  $save = FALSE;
  $settings = $theme
    ->settings();

  // Iterate over all setting plugins and manually save them since core's
  // process is severely limiting and somewhat broken.
  foreach ($theme
    ->getSettingPlugin() as $name => $setting) {

    // Allow the setting to participate in the form submission process.
    // Must call the "submitForm" method in case any setting actually uses it.
    // It should, in turn, invoke "submitFormElement", if the setting that
    // overrides it is implemented properly.
    $setting
      ->submitForm($form
      ->getArray(), $form_state);

    // Retrieve the submitted value.
    $value = $form_state
      ->getValue($name);

    // Determine if the setting has a new value that overrides the original.
    // Ignore the schemas "setting" because it's handled by UpdateManager.
    if ($name !== 'schemas' && $settings
      ->overridesValue($name, $value)) {

      // Set the new value.
      $settings
        ->set($name, $value);

      // Retrieve the cache tags for the setting.
      $cache_tags = array_unique(array_merge($setting
        ->getCacheTags()));

      // Flag the save.
      $save = TRUE;
    }

    // Remove value from the form state object so core doesn't re-save it.
    $form_state
      ->unsetValue($name);
  }

  // Save the settings, if needed.
  if ($save) {
    $settings
      ->save();

    // Invalidate necessary cache tags.
    if ($cache_tags) {
      \Drupal::service('cache_tags.invalidator')
        ->invalidateTags($cache_tags);
    }

    // Clear our internal theme cache so it can be rebuilt properly.
    $theme
      ->getCache('settings')
      ->deleteAll();
  }
}