1 color.module | color_form_system_theme_settings_alter(&$form, &$form_state) |
Implements hook_form_FORM_ID_alter().
File
- core/
modules/ color/ color.module, line 22 - Allows users to change the color scheme of themes.
Code
function color_form_system_theme_settings_alter(&$form, &$form_state) {
if (isset($form_state['build_info']['args'][0])
&& ($theme_name = $form_state['build_info']['args'][0])
&& ($theme_info = color_get_info($theme_name))
&& isset($theme_info['fields'])) {
$form['color'] = array(
'#type' => 'fieldset',
'#title' => t('Color scheme'),
'#weight' => -1,
'#attributes' => array(
'id' => 'color_scheme_form',
'class' => array('color-form'),
),
);
$theme_info['schemes'][''] = array('title' => t('Custom'), 'colors' => array());
$color_sets = array();
$schemes = array();
foreach ($theme_info['schemes'] as $key => $scheme) {
$color_sets[$key] = $scheme['title'];
$schemes[$key] = $scheme['colors'];
$schemes[$key] += $theme_info['schemes']['default']['colors'];
}
// See if we're using a predefined scheme.
// Note: we use the original theme when the default scheme is chosen.
$current_scheme = theme_get_setting('color.palette', $theme_name);
foreach ($schemes as $key => $scheme) {
if ($current_scheme == $scheme) {
$scheme_name = $key;
break;
}
}
if (empty($scheme_name)) {
if (empty($current_scheme)) {
$scheme_name = 'default';
}
else {
$scheme_name = '';
}
}
// Generate the preview markup.
$preview_url = url('<front>', array(
'query' => array(
'preview' => $theme_name
),
'absolute' => TRUE,
));
$preview_markup = theme('color_preview', array('preview_url' => $preview_url));
$base = backdrop_get_path('module', 'color');
// Add scheme selector.
$form['color']['scheme'] = array(
'#type' => 'select',
'#title' => t('Color set'),
'#options' => $color_sets,
'#default_value' => $scheme_name,
'#attributes' => array(
'data-color-schemes' => backdrop_json_encode($schemes),
'data-color-theme-name' => $theme_name,
'data-color-preview-markup' => $preview_markup,
'data-color-preview-token' => backdrop_get_token('color_token'),
),
'#attached' => array(
// Add custom CSS.
'css' => array(
$base . '/css/color.admin.css' => array(),
),
// Add custom JavaScript.
'js' => array(
$base . '/js/color.js'
),
),
);
$form['color']['palette'] = array(
'#type' => 'container',
'#attributes' => array('id' => 'palette'),
'#tree' => TRUE,
);
// Add palette fields.
foreach ($theme_info['fields'] as $name => $label) {
// Avoid placing them if they have already been placed by the theme.
if (!isset($form['#color_elements']) || !in_array($name, $form['#color_elements'])) {
$form['color']['palette'][$name] = color_get_color_element($theme_name, $name, $form);
}
}
$form['color']['theme'] = array('#type' => 'value', '#value' => $theme_name);
$form['color']['info'] = array('#type' => 'value', '#value' => $theme_info);
$form['#validate'][] = 'color_scheme_form_validate';
array_unshift($form['#submit'], 'color_scheme_form_submit');
// Check for legacy Bartik colors.
// @todo Remove in Backdrop 2.0.
if ($theme_name === 'bartik') {
module_load_include('inc', 'color', 'color.legacy');
bartik_form_system_theme_settings_alter($form, $form_state);
}
}
}