function color_scheme_form_submit
Same name in other branches
- 7.x modules/color/color.module \color_scheme_form_submit()
- 9 core/modules/color/color.module \color_scheme_form_submit()
Form submission handler for color_scheme_form().
See also
1 string reference to 'color_scheme_form_submit'
- color_form_system_theme_settings_alter in core/
modules/ color/ color.module - Implements hook_form_FORM_ID_alter().
File
-
core/
modules/ color/ color.module, line 372
Code
function color_scheme_form_submit($form, FormStateInterface $form_state) {
// Avoid color settings spilling over to theme settings.
$color_settings = [
'theme',
'palette',
'scheme',
];
if ($form_state->hasValue('info')) {
$color_settings[] = 'info';
}
foreach ($color_settings as $setting_name) {
${$setting_name} = $form_state->getValue($setting_name);
$form_state->unsetValue($setting_name);
}
if (!isset($info)) {
return;
}
$config = \Drupal::configFactory()->getEditable('color.theme.' . $theme);
// Resolve palette.
if ($scheme != '') {
foreach ($palette as $key => $color) {
if (isset($info['schemes'][$scheme]['colors'][$key])) {
$palette[$key] = $info['schemes'][$scheme]['colors'][$key];
}
}
$palette += $info['schemes']['default']['colors'];
}
// Make sure enough memory is available.
if (isset($info['base_image'])) {
// Fetch source image dimensions.
$source = drupal_get_path('theme', $theme) . '/' . $info['base_image'];
list($width, $height) = getimagesize($source);
// We need at least a copy of the source and a target buffer of the same
// size (both at 32bpp).
$required = $width * $height * 8;
// We intend to prevent color scheme changes if there isn't enough memory
// available. memory_get_usage(TRUE) returns a more accurate number than
// memory_get_usage(), therefore we won't inadvertently reject a color
// scheme change based on a faulty memory calculation.
$usage = memory_get_usage(TRUE);
$memory_limit = ini_get('memory_limit');
$size = Bytes::toInt($memory_limit);
if (!Environment::checkMemoryLimit($usage + $required, $memory_limit)) {
\Drupal::messenger()->addError(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="http://php.net/manual/ini.core.php#ini.sect.resource-limits">PHP documentation</a> for more information.', [
'%size' => format_size($usage + $required - $size),
]));
return;
}
}
$file_system = \Drupal::service('file_system');
// Delete old files.
$files = $config->get('files');
if (isset($files)) {
foreach ($files as $file) {
@$file_system->unlink($file);
}
}
if (isset($file) && ($file = dirname($file))) {
@\Drupal::service('file_system')->rmdir($file);
}
// No change in color config, use the standard theme from color.inc.
if (implode(',', color_get_palette($theme, TRUE)) == implode(',', $palette)) {
$config->delete();
return;
}
// Prepare target locations for generated files.
$id = $theme . '-' . substr(hash('sha256', serialize($palette) . microtime()), 0, 8);
$paths['color'] = 'public://color';
$paths['target'] = $paths['color'] . '/' . $id;
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
foreach ($paths as $path) {
$file_system->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
}
$paths['target'] = $paths['target'] . '/';
$paths['id'] = $id;
$paths['source'] = drupal_get_path('theme', $theme) . '/';
$paths['files'] = $paths['map'] = [];
// Save palette and logo location.
$config->set('palette', $palette)
->set('logo', $paths['target'] . 'logo.svg')
->save();
// Copy over neutral images.
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
foreach ($info['copy'] as $file) {
$base = $file_system->basename($file);
$source = $paths['source'] . $file;
try {
$filepath = $file_system->copy($source, $paths['target'] . $base);
} catch (FileException $e) {
$filepath = FALSE;
}
$paths['map'][$file] = $base;
$paths['files'][] = $filepath;
}
// Render new images, if image has been provided.
if (isset($info['base_image'])) {
_color_render_images($theme, $info, $paths, $palette);
}
// Rewrite theme stylesheets.
$css = [];
foreach ($info['css'] as $stylesheet) {
// Build a temporary array with CSS files.
$files = [];
if (file_exists($paths['source'] . $stylesheet)) {
$files[] = $stylesheet;
}
foreach ($files as $file) {
$css_optimizer = new CssOptimizer();
// Aggregate @imports recursively for each configured top level CSS file
// without optimization. Aggregation and optimization will be
// handled by drupal_build_css_cache() only.
$style = $css_optimizer->loadFile($paths['source'] . $file, FALSE);
// Return the path to where this CSS file originated from, stripping
// off the name of the file at the end of the path.
$css_optimizer->rewriteFileURIBasePath = base_path() . dirname($paths['source'] . $file) . '/';
// Prefix all paths within this CSS file, ignoring absolute paths.
$style = preg_replace_callback('/url\\([\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\)/i', [
$css_optimizer,
'rewriteFileURI',
], $style);
// Rewrite stylesheet with new colors.
$style = _color_rewrite_stylesheet($theme, $info, $paths, $palette, $style);
$base_file = $file_system->basename($file);
$css[] = $paths['target'] . $base_file;
_color_save_stylesheet($paths['target'] . $base_file, $style, $paths);
}
}
// Maintain list of files.
$config->set('stylesheets', $css)
->set('files', $paths['files'])
->save();
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.