function public static function SystemThemeSettings::submitFormElement

8.x-3.x SystemThemeSettings.php public static SystemThemeSettings::submitFormElement(Element $form, FormStateInterface $form_state)

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

Class

SystemThemeSettings
Implements hook_form_system_theme_settings_alter().

Namespace

Drupal\bootstrap\Plugin\Form

Source src/Plugin/Form/SystemThemeSettings.php (line 176)

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();
  $rebuild_cdn_assets = FALSE;

  // 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) {
    // Skip saving deprecated settings.
    if ($setting instanceof DeprecatedSettingInterface) {
      $form_state->unsetValue($name);
      continue;
    }

    // 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);

    // Trim any new lines and convert to simple new line breaks.
    $definition = $setting->getPluginDefinition();
    if (isset($definition['type']) && $definition['type'] === 'textarea' && is_string($value)) {
      $value = implode("\n", array_filter(array_map('trim', preg_split("/\r\n|\n/", $value))));
    }

    // 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()));

      // A CDN setting has changed, flag that CDN assets should be rebuilt.
      if (strpos($name, 'cdn') === 0) {
        $rebuild_cdn_assets = TRUE;
      }

      // 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) {
    // Remove any cached CDN assets so they can be rebuilt.
    if ($rebuild_cdn_assets) {
      $settings->clear('cdn_cache');
    }

    $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();
  }
}