Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion css_editor.module
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ function css_editor_form_system_theme_settings_alter(&$form, FormStateInterface
'#type' => 'checkbox',
'#default_value' => $config->get('enabled'),
);
// Text box for custom file name
$form['css_editor']['custom_filename'] = array(
'#type' => 'textfield',
'#title' => t('Custom Filename (Optional)'),
'#default_value' => $config->get('custom_filename'),
'#size' => 60,
'#maxlength' => 128,
'#pattern' => '[A-Za-z0-9\-_]+',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to consider non-English characters here.

'#required' => FALSE,
'#description' => t("If you want to save this CSS file with a custom filename, enter that filename here, omitting the file extension.<br/>The filename must include only letters, numbers, dashes, and underscores."),
);
// Editor box.
$form['css_editor']['css'] = array(
'#type' => 'textarea',
Expand Down Expand Up @@ -96,6 +107,7 @@ function css_editor_form_system_theme_settings_alter(&$form, FormStateInterface
'url' => $preview_url,
),
);

// Attach CSS and Javascript libraries.
$form['#attached']['library'][] = 'css_editor/codemirror';
$form['#attached']['library'][] = 'css_editor/css_editor';
Expand All @@ -112,14 +124,29 @@ function _css_editor_theme_settings_form_submit($form, FormStateInterface $form_
if ($theme) {
// Save file.
$path = 'public://css_editor';
$file = $path . DIRECTORY_SEPARATOR . $theme . '.css';
$oldPath = \Drupal::config('css_editor.theme.' . $theme)->get('path');

if (!empty($form_state->getValue(['css_editor', 'custom_filename']))) {
// custom path definition
$file = $path . DIRECTORY_SEPARATOR . $form_state->getValue(['css_editor', 'custom_filename']) . '.css';
} else {
$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);
}
// delete obsolete file
if ($oldPath!=$file) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will get flagged by the linter for no spaces between the two sides of the inequality check.

\Drupal::logger('css_editor')->notice("Attempting to delete an obsolete file: ".$oldPath);
\Drupal::service('file_system')->delete($oldPath);
}
\Drupal::logger('css_editor')->notice("Saving path: ".$oldPath);

// Save settings.
\Drupal::configFactory()->getEditable('css_editor.theme.' . $theme)
->set('enabled', $form_state->getValue(['css_editor', 'enabled']))
->set('custom_filename', $form_state->getValue(['css_editor', 'custom_filename']))
->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']))
Expand Down