You are here

function _css_editor_theme_settings_form_submit in CSS Editor 8

Form submission handler for hook_form_system_theme_settings_alter().

1 string reference to '_css_editor_theme_settings_form_submit'
css_editor_form_system_theme_settings_alter in ./css_editor.module
Implements hook_form_FORM_ID_alter() for `system_theme_settings`.

File

./css_editor.module, line 101
Allows users to apply customized CSS to themes.

Code

function _css_editor_theme_settings_form_submit($form, FormStateInterface $form_state) {
  $theme = _css_editor_get_edited_theme($form_state);
  if ($theme) {

    // Save file.
    $path = 'public://css_editor';
    $file = $path . DIRECTORY_SEPARATOR . $theme . '.css';
    if (\Drupal::service('file_system')
      ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
      \Drupal::service('file_system')
        ->saveData($form_state
        ->getValue([
        'css_editor',
        'css',
      ]), $file, FileSystemInterface::EXISTS_REPLACE);
      \Drupal::service('file_system')
        ->chmod($file);
    }

    // Save settings.
    \Drupal::configFactory()
      ->getEditable('css_editor.theme.' . $theme)
      ->set('enabled', $form_state
      ->getValue([
      'css_editor',
      'enabled',
    ]))
      ->set('plaintext_enabled', $form_state
      ->getValue([
      'css_editor',
      'plaintext_enabled',
    ]))
      ->set('autopreview_enabled', $form_state
      ->getValue([
      'css_editor',
      'autopreview_enabled',
    ]))
      ->set('css', $form_state
      ->getValue([
      'css_editor',
      'css',
    ]))
      ->set('path', $file)
      ->save();

    // Clear cache.
    drupal_flush_all_caches();
  }

  // Remove the settings from the form state so the values are not saved in the
  // theme settings.
  $form_state
    ->unsetValue('css_editor');
}