View source
<?php
namespace Drupal\admincss\Form;
use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AdminCssEditor extends ConfigFormBase {
protected $fileSystem;
protected $cacheTagsInvalidator;
protected $cssCollectionOptimizer;
protected $jsCollectionOptimizer;
public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system, CacheTagsInvalidatorInterface $cache_tags_invalidator, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
parent::__construct($config_factory);
$this->fileSystem = $file_system;
$this->cacheTagsInvalidator = $cache_tags_invalidator;
$this->cssCollectionOptimizer = $css_collection_optimizer;
$this->jsCollectionOptimizer = $js_collection_optimizer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('file_system'), $container
->get('cache_tags.invalidator'), $container
->get('asset.css.collection_optimizer'), $container
->get('asset.js.collection_optimizer'));
}
public function getFormId() {
return 'admincss_config_form';
}
public function getEditableConfigNames() {
return [
'admincss.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['custom_css'] = [
'#prefix' => '<div class="admincss-ace-editor">',
'#suffix' => '</div>',
'#type' => 'textarea',
'#title' => $this
->t('Custom CSS'),
'#description' => $this
->t('The custom CSS code.'),
'#default_value' => $this
->config('admincss.settings')
->get('custom_css'),
'#rows' => 10,
'#attributes' => [
'class' => [
'admincss__editor',
'admincss__custom-css',
],
'data-ace-mode' => 'css',
],
];
$form['#attached']['library'][] = 'admincss/admincss.editor';
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('admincss.settings');
$custom_css = $form_state
->getValue('custom_css');
$config
->set('custom_css', $custom_css);
$config
->save();
$changed = FALSE;
$destination_uri = 'public://admin-style.css';
if (empty($custom_css)) {
try {
$this->fileSystem
->delete($destination_uri);
$changed = TRUE;
} catch (FileException $e) {
}
}
elseif (file_save_data($custom_css, $destination_uri, FileSystemInterface::EXISTS_REPLACE)) {
$changed = TRUE;
}
if ($changed) {
$this
->flushAssetCache();
}
else {
$this
->messenger()
->addWarning($this
->t('Failed to successfully write the changes to disk.'));
}
parent::submitForm($form, $form_state);
}
protected function flushAssetCache() {
if ($this
->config('system.performance')
->get('css.preprocess')) {
$this->cacheTagsInvalidator
->invalidateTags([
'library_info',
]);
$this->cssCollectionOptimizer
->deleteAll();
$this->jsCollectionOptimizer
->deleteAll();
}
else {
_drupal_flush_css_js();
}
}
}