diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 157dc18..8485b1e 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -9,6 +9,7 @@ */ use Drupal\Core\Utility\ThemeRegistry; +use Drupal\Component\Utility\NestedArray; /** * @defgroup content_flags Content markers @@ -659,7 +660,7 @@ function _theme_build_registry($theme, $base_theme, $theme_engine) { * their base theme), direct sub-themes of sub-themes, etc. The keys are * the themes' machine names, and the values are the themes' human-readable * names. This element is not set if there are no themes on the system that - * declare this theme as their base theme. + * declare this theme as their base theme. */ function list_themes($refresh = FALSE) { $list = &drupal_static(__FUNCTION__, array()); @@ -1296,23 +1297,33 @@ function theme_get_setting($setting_name, $theme = NULL) { } if (empty($cache[$theme])) { - // Set the default values for each global setting. - // To add new global settings, add their default values below, and then - // add form elements to system_theme_settings() in system.admin.inc. - $cache[$theme] = array( - 'default_logo' => 1, - 'logo_path' => '', - 'default_favicon' => 1, - 'favicon_path' => '', - // Use the IANA-registered MIME type for ICO files as default. - 'favicon_mimetype' => 'image/vnd.microsoft.icon', + // Provide defaults as function is used before config installed. + $default_config = array( + 'favicon' => array( + 'path' => '', + // Use the IANA-registered MIME type for ICO files as default. + 'mimetype' => 'image/vnd.microsoft.icon', + 'use_default' => 1, + ), + 'features' => array(), + 'logo' => array( + 'path' => '', + 'use_default' => 1, + ), ); // Turn on all default features. $features = _system_default_theme_features(); foreach ($features as $feature) { - $cache[$theme]['toggle_' . $feature] = 1; + $default_config['features'][$feature] = 1; } + // Create a config object that will merge $default_config, settings from the + // theme's info file, global theme configuration and the theme's + // configuration. This allows the use of CMI to manage the nested array of + // keys. The config object is never be saved. Potentially saving it could + // offer performance improvements. + $cache[$theme] = config('cache.theme.' . $theme)->setData($default_config); + // Get the values for the theme-specific settings from the .info files of // the theme and all its base themes. if ($theme) { @@ -1329,59 +1340,107 @@ function theme_get_setting($setting_name, $theme = NULL) { } foreach ($theme_keys as $theme_key) { if (!empty($themes[$theme_key]->info['settings'])) { - $cache[$theme] = array_merge($cache[$theme], $themes[$theme_key]->info['settings']); + $cache[$theme]->setData(NestedArray::mergeDeep($cache[$theme]->get(), $themes[$theme_key]->info['settings'])); } } } - // Get the saved global settings from the database. - $cache[$theme] = array_merge($cache[$theme], variable_get('theme_settings', array())); + // Get the global settings from configuration. + $cache[$theme]->setData(NestedArray::mergeDeep($cache[$theme]->get(), config('system.theme.global')->get())); if ($theme) { - // Get the saved theme-specific settings from the database. - $cache[$theme] = array_merge($cache[$theme], variable_get('theme_' . $theme . '_settings', array())); + // Get the saved theme-specific settings from the configuration system. + $cache[$theme]->setData(NestedArray::mergeDeep($cache[$theme]->get(), config($theme . '.settings')->get())); // If the theme does not support a particular feature, override the global // setting and set the value to NULL. if (!empty($theme_object->info['features'])) { - foreach ($features as $feature) { + foreach ($default_config['features'] as $feature => $enabled) { if (!in_array($feature, $theme_object->info['features'])) { - $cache[$theme]['toggle_' . $feature] = NULL; + $cache[$theme]->set('features.' . $feature, NULL); } } } // Generate the path to the logo image. - if ($cache[$theme]['toggle_logo']) { - if ($cache[$theme]['default_logo']) { - $cache[$theme]['logo'] = file_create_url(dirname($theme_object->filename) . '/logo.png'); + if ($cache[$theme]->get('features.logo')) { + $logo_path = $cache[$theme]->get('logo.path'); + if ($cache[$theme]->get('logo.use_default')) { + $cache[$theme]->set('logo.url', file_create_url(dirname($theme_object->filename) . '/logo.png')); } - elseif ($cache[$theme]['logo_path']) { - $cache[$theme]['logo'] = file_create_url($cache[$theme]['logo_path']); + elseif ($logo_path) { + $cache[$theme]->set('logo.url', file_create_url($logo_path)); } } // Generate the path to the favicon. - if ($cache[$theme]['toggle_favicon']) { - if ($cache[$theme]['default_favicon']) { + if ($cache[$theme]->get('features.favicon')) { + $favicon_path = $cache[$theme]->get('favicon.path'); + if ($cache[$theme]->get('favicon.use_default')) { if (file_exists($favicon = dirname($theme_object->filename) . '/favicon.ico')) { - $cache[$theme]['favicon'] = file_create_url($favicon); + $cache[$theme]->set('favicon.url', file_create_url($favicon)); } else { - $cache[$theme]['favicon'] = file_create_url('core/misc/favicon.ico'); + $cache[$theme]->set('favicon.url', file_create_url('core/misc/favicon.ico')); } } - elseif ($cache[$theme]['favicon_path']) { - $cache[$theme]['favicon'] = file_create_url($cache[$theme]['favicon_path']); + elseif ($favicon_path) { + $cache[$theme]->set('favicon.url', file_create_url($favicon_path)); } else { - $cache[$theme]['toggle_favicon'] = FALSE; + $cache[$theme]->set('features.favicon', FALSE); } } } } - return isset($cache[$theme][$setting_name]) ? $cache[$theme][$setting_name] : NULL; + return $cache[$theme]->get($setting_name); +} + +/** + * Converts format of old theme-configuration to the format of yml-configuration. + * + * @param $old_config + * The configuration loaded from old theme configuration + * @return + * Configuration array compatible to yml-configuration + * + * @todo Make this function obsolete + */ +function theme_convert_variables_to_config($old_config) { + $new_config = array(); + + foreach ($old_config as $key => $value) { + if ($key == 'logo') { + $new_config['logo']['url'] = $value; + } + else if ($key == 'default_logo') { + $new_config['logo']['use_default'] = $value; + } + else if ($key == 'logo_path') { + $new_config['logo']['path'] = $value; + } + else if ($key == 'favicon') { + $new_config['favicon']['url'] = $value; + } + else if ($key == 'default_favicon') { + $new_config['favicon']['use_default'] = $value; + } + else if ($key == 'favicon_path') { + $new_config['favicon']['path'] = $value; + } + else if ($key == 'favicon_mimetype') { + $new_config['favicon']['mimetype'] = $value; + } + else if (substr($key, 0, 7) == 'toggle_') { + $new_config['features'][substr($key, 7)] = $value; + } + else if (!in_array($key, array('theme', 'logo_upload'))) { + $new_config[$key] = $value; + } + } + + return $new_config; } /** @@ -2448,9 +2507,9 @@ function template_preprocess_html(&$variables) { $variables['html_attributes']['dir'] = $language_interface->direction ? 'rtl' : 'ltr'; // Add favicon. - if (theme_get_setting('toggle_favicon')) { - $favicon = theme_get_setting('favicon'); - $type = theme_get_setting('favicon_mimetype'); + if (theme_get_setting('features.favicon')) { + $favicon = theme_get_setting('favicon.url'); + $type = theme_get_setting('favicon.mimetype'); drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type)); } @@ -2556,12 +2615,12 @@ function template_preprocess_page(&$variables) { $variables['feed_icons'] = drupal_get_feeds(); $variables['language'] = $language_interface; $variables['language']->dir = $language_interface->direction ? 'rtl' : 'ltr'; - $variables['logo'] = theme_get_setting('logo'); - $variables['main_menu'] = theme_get_setting('toggle_main_menu') ? menu_main_menu() : array(); - $variables['secondary_menu'] = theme_get_setting('toggle_secondary_menu') ? menu_secondary_menu() : array(); + $variables['logo'] = theme_get_setting('logo.url'); + $variables['main_menu'] = theme_get_setting('features.main_menu') ? menu_main_menu() : array(); + $variables['secondary_menu'] = theme_get_setting('features.secondary_menu') ? menu_secondary_menu() : array(); $variables['action_links'] = menu_local_actions(); - $variables['site_name'] = (theme_get_setting('toggle_name') ? check_plain($site_config->get('name')) : ''); - $variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? filter_xss_admin($site_config->get('slogan')) : ''); + $variables['site_name'] = (theme_get_setting('features.name') ? check_plain($site_config->get('name')) : ''); + $variables['site_slogan'] = (theme_get_setting('features.slogan') ? filter_xss_admin($site_config->get('slogan')) : ''); $variables['tabs'] = menu_local_tabs(); if ($node = menu_get_object()) { @@ -2717,9 +2776,9 @@ function template_preprocess_maintenance_page(&$variables) { $regions = $theme_data[$theme]->info['regions']; // Add favicon - if (theme_get_setting('toggle_favicon')) { - $favicon = theme_get_setting('favicon'); - $type = theme_get_setting('favicon_mimetype'); + if (theme_get_setting('features.favicon')) { + $favicon = theme_get_setting('favicon.url'); + $type = theme_get_setting('favicon.mimetype'); drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => drupal_strip_dangerous_protocols($favicon), 'type' => $type)); } @@ -2766,12 +2825,12 @@ function template_preprocess_maintenance_page(&$variables) { $variables['help'] = ''; $variables['language'] = $language_interface; $variables['language']->dir = $language_interface->direction ? 'rtl' : 'ltr'; - $variables['logo'] = theme_get_setting('logo'); + $variables['logo'] = theme_get_setting('logo.url'); $variables['messages'] = $variables['show_messages'] ? theme('status_messages') : ''; $variables['main_menu'] = array(); $variables['secondary_menu'] = array(); - $variables['site_name'] = (theme_get_setting('toggle_name') ? check_plain($site_name) : ''); - $variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? filter_xss_admin($site_slogan) : ''); + $variables['site_name'] = (theme_get_setting('features.name') ? check_plain($site_name) : ''); + $variables['site_slogan'] = (theme_get_setting('features.slogan') ? filter_xss_admin($site_slogan) : ''); $variables['tabs'] = ''; $variables['title'] = drupal_get_title(); diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index b46beff..de2b60c 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -2131,7 +2131,7 @@ function template_preprocess_comment(&$variables) { $variables['changed'] = format_date($comment->changed); $variables['new'] = !empty($comment->new) ? t('new') : ''; - $variables['user_picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', array('account' => $comment)) : ''; + $variables['user_picture'] = theme_get_setting('features.comment_user_picture') ? theme('user_picture', array('account' => $comment)) : ''; $variables['signature'] = $comment->signature; $uri = entity_uri('comment', $comment); diff --git a/core/modules/node/node.module b/core/modules/node/node.module index d1dfdc2..587d0a6 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1422,7 +1422,7 @@ function template_preprocess_node(&$variables) { if (variable_get('node_submitted_' . $node->type, TRUE)) { $variables['display_submitted'] = TRUE; $variables['submitted'] = t('Submitted by !username on !datetime', array('!username' => $variables['name'], '!datetime' => $variables['date'])); - $variables['user_picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array('account' => $node)) : ''; + $variables['user_picture'] = theme_get_setting('features.node_user_picture') ? theme('user_picture', array('account' => $node)) : ''; } else { $variables['display_submitted'] = FALSE; diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 27eb8c2..7ebc28c 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -690,7 +690,7 @@ function shortcut_preprocess_page(&$variables) { $link_path = 'admin/config/user-interface/shortcut/link/' . $mlid . '/delete'; } - if (theme_get_setting('shortcut_module_link')) { + if (theme_get_setting('features.shortcut_module_link')) { $variables['title_suffix']['add_or_remove_shortcut'] = array( '#attached' => array( 'css' => array( diff --git a/core/modules/system/config/system.theme.global.yml b/core/modules/system/config/system.theme.global.yml new file mode 100644 index 0000000..fca8fd2 --- /dev/null +++ b/core/modules/system/config/system.theme.global.yml @@ -0,0 +1,19 @@ +favicon: + mimetype: image/vnd.microsoft.icon + path: '' + url: '' + use_default: '1' +features: + comment_user_picture: '1' + comment_user_verification: '1' + favicon: '1' + logo: '1' + name: '1' + node_user_picture: '1' + main_menu: '1' + secondary_menu: '1' + slogan: '1' +logo: + path: '' + url: '' + use_default: '1' diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index 2e424ad..56c4413 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Drupal\Component\Utility\NestedArray; /** * Menu callback; Provide the administration overview page. @@ -372,20 +373,24 @@ function system_theme_default() { * @return * The form structure. * @ingroup forms + * @see system_theme_settings_validate() * @see system_theme_settings_submit() */ function system_theme_settings($form, &$form_state, $key = '') { // Default settings are defined in theme_get_setting() in includes/theme.inc if ($key) { $var = 'theme_' . $key . '_settings'; + $config_key = $key . '.settings'; $themes = list_themes(); $features = $themes[$key]->info['features']; } else { $var = 'theme_settings'; + $config_key = 'system.theme.global'; } $form['var'] = array('#type' => 'hidden', '#value' => $var); + $form['config_key'] = array('#type' => 'hidden', '#value' => $config_key); // Toggle settings $toggles = array( @@ -418,7 +423,7 @@ function system_theme_settings($form, &$form_state, $key = '') { ); foreach ($toggles as $name => $title) { if ((!$key) || in_array($name, $features)) { - $form['theme_settings']['toggle_' . $name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => theme_get_setting('toggle_' . $name, $key)); + $form['theme_settings']['toggle_' . $name] = array('#type' => 'checkbox', '#title' => $title, '#default_value' => theme_get_setting('features.' . $name, $key)); // Disable checkboxes for features not supported in the current configuration. if (isset($disabled['toggle_' . $name])) { $form['theme_settings']['toggle_' . $name]['#disabled'] = TRUE; @@ -442,7 +447,7 @@ function system_theme_settings($form, &$form_state, $key = '') { $form['logo']['default_logo'] = array( '#type' => 'checkbox', '#title' => t('Use the default logo supplied by the theme'), - '#default_value' => theme_get_setting('default_logo', $key), + '#default_value' => theme_get_setting('logo.use_default', $key), '#tree' => FALSE, ); $form['logo']['settings'] = array( @@ -457,7 +462,7 @@ function system_theme_settings($form, &$form_state, $key = '') { $form['logo']['settings']['logo_path'] = array( '#type' => 'textfield', '#title' => t('Path to custom logo'), - '#default_value' => theme_get_setting('logo_path', $key), + '#default_value' => theme_get_setting('logo.path', $key), ); $form['logo']['settings']['logo_upload'] = array( '#type' => 'file', @@ -476,7 +481,7 @@ function system_theme_settings($form, &$form_state, $key = '') { $form['favicon']['default_favicon'] = array( '#type' => 'checkbox', '#title' => t('Use the default shortcut icon supplied by the theme'), - '#default_value' => theme_get_setting('default_favicon', $key), + '#default_value' => theme_get_setting('favicon.use_default', $key), ); $form['favicon']['settings'] = array( '#type' => 'container', @@ -490,7 +495,7 @@ function system_theme_settings($form, &$form_state, $key = '') { $form['favicon']['settings']['favicon_path'] = array( '#type' => 'textfield', '#title' => t('Path to custom icon'), - '#default_value' => theme_get_setting('favicon_path', $key), + '#default_value' => theme_get_setting('favicon.path', $key), ); $form['favicon']['settings']['favicon_upload'] = array( '#type' => 'file', @@ -584,12 +589,7 @@ function system_theme_settings($form, &$form_state, $key = '') { } } - $form = system_settings_form($form); - // We don't want to call system_settings_form_submit(), so change #submit. - array_pop($form['#submit']); - $form['#submit'][] = 'system_theme_settings_submit'; - $form['#validate'][] = 'system_theme_settings_validate'; - return $form; + return system_config_form($form, $form_state); } /** @@ -682,10 +682,13 @@ function _system_theme_settings_validate_path($path) { * Process system_theme_settings form submissions. */ function system_theme_settings_submit($form, &$form_state) { + $config = config($form_state['values']['config_key']); + // Exclude unnecessary elements before saving. form_state_values_clean($form_state); $key = $form_state['values']['var']; unset($form_state['values']['var']); + unset($form_state['values']['config_key']); $values = $form_state['values']; @@ -719,9 +722,10 @@ function system_theme_settings_submit($form, &$form_state) { $values['favicon_mimetype'] = file_get_mimetype($values['favicon_path']); } - variable_set($key, $values); - drupal_set_message(t('The configuration options have been saved.')); - + // Merge form values into theme's configuration data in case there is + // configuration not exposed on system_theme_settings(). + $new_config = theme_convert_variables_to_config($values); + $config->setData(NestedArray::mergeDeep($config->get(), $new_config))->save(); cache_invalidate(array('content' => TRUE)); } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 0e42214..b447d8f 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -2047,6 +2047,26 @@ function system_update_8016() { } /** + * Moves system theme settings from variables to config. + */ +function system_update_8017() { + $variables = array('theme_settings' => 'system.theme.global'); + $themes = list_themes(); + foreach ($themes as $theme) { + $variables['theme_' . $theme->name . '_settings'] = $theme->name . '.settings'; + } + + foreach ($variables as $old_key => $new_key) { + $config = theme_convert_variables_to_config(variable_get($old_key, array())); + $new_config = config($new_key); + foreach ($config as $key => $value) { + $new_config->set($key, $value); + } + $new_config->save(); + } +} + +/** * @} End of "defgroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/system/theme.api.php b/core/modules/system/theme.api.php index b98132e..3693b97 100644 --- a/core/modules/system/theme.api.php +++ b/core/modules/system/theme.api.php @@ -87,7 +87,7 @@ function hook_form_system_theme_settings_alter(&$form, &$form_state) { $form['toggle_breadcrumb'] = array( '#type' => 'checkbox', '#title' => t('Display the breadcrumb'), - '#default_value' => theme_get_setting('toggle_breadcrumb'), + '#default_value' => theme_get_setting('features.breadcrumb'), '#description' => t('Show a trail of links from the homepage to the current page.'), ); } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 4e8df24..25707b6 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1291,7 +1291,7 @@ function template_preprocess_username(&$variables) { $variables['extra'] = ''; if (empty($account->uid)) { $variables['uid'] = 0; - if (theme_get_setting('toggle_comment_user_verification')) { + if (theme_get_setting('features.comment_user_verification')) { $variables['extra'] = ' (' . t('not verified') . ')'; } } diff --git a/core/themes/bartik/bartik.info b/core/themes/bartik/bartik.info index c0c206d..e1652c6 100644 --- a/core/themes/bartik/bartik.info +++ b/core/themes/bartik/bartik.info @@ -30,4 +30,4 @@ regions[footer_thirdcolumn] = Footer third column regions[footer_fourthcolumn] = Footer fourth column regions[footer] = Footer -settings[shortcut_module_link] = 0 +settings[features][shortcut_module_link] = 0 diff --git a/core/themes/bartik/color/color.inc b/core/themes/bartik/color/color.inc index 50ce731..f70450c 100644 --- a/core/themes/bartik/color/color.inc +++ b/core/themes/bartik/color/color.inc @@ -6,7 +6,7 @@ */ // Put the logo path into JavaScript for the live preview. -drupal_add_js(array('color' => array('logo' => theme_get_setting('logo', 'bartik'))), 'setting'); +drupal_add_js(array('color' => array('logo' => theme_get_setting('logo.url', 'bartik'))), 'setting'); $info = array( // Available colors and color labels used in theme. diff --git a/core/themes/bartik/template.php b/core/themes/bartik/template.php index 5373c92..b1dbb51 100644 --- a/core/themes/bartik/template.php +++ b/core/themes/bartik/template.php @@ -50,8 +50,8 @@ function bartik_process_page(&$variables) { } // Always print the site name and slogan, but if they are toggled off, we'll // just hide them visually. - $variables['hide_site_name'] = theme_get_setting('toggle_name') ? FALSE : TRUE; - $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE; + $variables['hide_site_name'] = theme_get_setting('features.name') ? FALSE : TRUE; + $variables['hide_site_slogan'] = theme_get_setting('features.slogan') ? FALSE : TRUE; if ($variables['hide_site_name']) { // If toggle_name is FALSE, the site_name will be empty, so we rebuild it. $variables['site_name'] = check_plain($site_config->get('name')); @@ -98,8 +98,8 @@ function bartik_process_maintenance_page(&$variables) { $site_config = config('system.site'); // Always print the site name and slogan, but if they are toggled off, we'll // just hide them visually. - $variables['hide_site_name'] = theme_get_setting('toggle_name') ? FALSE : TRUE; - $variables['hide_site_slogan'] = theme_get_setting('toggle_slogan') ? FALSE : TRUE; + $variables['hide_site_name'] = theme_get_setting('features.name') ? FALSE : TRUE; + $variables['hide_site_slogan'] = theme_get_setting('features.slogan') ? FALSE : TRUE; if ($variables['hide_site_name']) { // If toggle_name is FALSE, the site_name will be empty, so we rebuild it. $variables['site_name'] = check_plain($site_config->get('name')); diff --git a/core/themes/seven/seven.info b/core/themes/seven/seven.info index 969f749..69d052d 100644 --- a/core/themes/seven/seven.info +++ b/core/themes/seven/seven.info @@ -5,7 +5,7 @@ version = VERSION core = 8.x stylesheets[screen][] = reset.css stylesheets[screen][] = style.css -settings[shortcut_module_link] = 1 +settings[features][shortcut_module_link] = 1 regions[content] = Content regions[help] = Help regions[page_top] = Page top