Commit 31513883 authored by catch's avatar catch

Issue #1712250 by alexpott, n3or, cam8001, vijaycs85, Albert Volkman, jibran,...

Issue #1712250 by alexpott, n3or, cam8001, vijaycs85, Albert Volkman, jibran, heyrocker, chx: Convert theme settings to configuration system.
parent cb4782ea
This diff is collapsed.
......@@ -1479,6 +1479,39 @@ function update_config_manifest_add($config_prefix, array $ids) {
$manifest->save();
}
/**
* Installs a default configuration file into the active store.
*
* Provide a generalised method to save a default configuration object for an
* already enabled module or theme as part of an update from Drupal 7 to Drupal
* 8's configuration management system.
*
* @param string $type
* The extension type; e.g., 'module' or 'theme'.
* @param string $config_name
* The configuration object name to retrieve.
*
* @return boolean
* True on success, false if config file does not exist.
*/
function update_7_to_8_install_default_config($type, $config_name) {
// Build the new configuration object.
$config = Drupal::config($config_name);
// Extract the extension namespace/owner from the configuration object name.
$name = strtok($config_name, '.');
// Load and set default configuration values.
$file = new FileStorage(drupal_get_path($type, $name) . '/config');
if (!$file->exists($config_name)) {
return FALSE;
}
$default_data = $file->read($config_name);
// Apply and save the default values.
$config->setData($file->read($config_name))->save();
return TRUE;
}
/**
* Updates 7.x variables to state records.
*
......
......@@ -112,7 +112,7 @@ protected function getAllFolders() {
if (!isset($this->folders)) {
$this->folders = $this->getComponentNames('profile', array(drupal_get_profile()));
$this->folders += $this->getComponentNames('module', array_keys(drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.module$/', 'modules', 'name', 0)));
$this->folders += $this->getComponentNames('theme', array_keys(drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info$/', 'themes')));
$this->folders += $this->getComponentNames('theme', array_keys(drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', 'themes')));
}
return $this->folders;
}
......
<?php
/**
* @file
* Contains \Drupal\Core\Theme\ThemeSettings.
*/
namespace Drupal\Core\Theme;
use Drupal\Component\Utility\NestedArray;
/**
* Defines the default theme settings object.
*/
class ThemeSettings {
/**
* The theme of the theme settings object.
*
* @var string
*/
protected $theme;
/**
* The data of the theme settings object.
*
* @var array
*/
protected $data;
/**
* Constructs a theme settings object.
*
* @param string $name
* The name of the theme settings object being constructed.
*/
public function __construct($theme) {
$this->theme = $theme;
$this->data = array();
}
/**
* Returns the theme of this theme settings object.
*
* @return string
* The theme of this theme settings object.
*/
public function getTheme() {
return $this->theme;
}
/**
* Gets data from this theme settings object.
*
* @param string $key
* A string that maps to a key within the theme settings data.
* For instance in the following theme settings array:
* @code
* array(
* 'foo' => array(
* 'bar' => 'baz',
* ),
* );
* @endcode
* A key of 'foo.bar' would return the string 'baz'. However, a key of 'foo'
* would return array('bar' => 'baz').
* If no key is specified, then the entire data array is returned.
*
*
* @return mixed
* The data that was requested.
*/
public function get($key = '') {
if (empty($key)) {
return $this->data;
}
else {
$parts = explode('.', $key);
if (count($parts) == 1) {
return isset($this->data[$key]) ? $this->data[$key] : NULL;
}
else {
$value = NestedArray::getValue($this->data, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
}
}
/**
* Replaces the data of this theme settings object.
*
* @param array $data
* The new theme settings data.
*
* @return \Drupal\Core\Theme\ThemeSettings
* The theme settings object.
*/
public function setData(array $data) {
$this->data = $data;
return $this;
}
/**
* Sets value in this theme settings object.
*
* @param string $key
* Identifier to store value in theme settings.
* @param string $value
* Value to associate with identifier.
*
* @return \Drupal\Core\Theme\ThemeSettings
* The theme settings object.
*/
public function set($key, $value) {
// The dot/period is a reserved character; it may appear between keys, but
// not within keys.
$parts = explode('.', $key);
if (count($parts) == 1) {
$this->data[$key] = $value;
}
else {
NestedArray::setValue($this->data, $parts, $value);
}
return $this;
}
/**
* Unsets value in this theme settings object.
*
* @param string $key
* Name of the key whose value should be unset.
*
* @return \Drupal\Core\Theme\ThemeSettings
* The theme settings object.
*/
public function clear($key) {
$parts = explode('.', $key);
if (count($parts) == 1) {
unset($this->data[$key]);
}
else {
NestedArray::unsetValue($this->data, $parts);
}
return $this;
}
/**
* Merges the data into this theme settings object.
*
* @param array $data
* Theme settings data to merge.
*
* @return \Drupal\Core\Theme\ThemeSettings
* The theme settings object.
*/
public function mergeData ($data) {
$this->data = NestedArray::mergeDeep($this->data, $data);
return $this;
}
}
......@@ -1576,7 +1576,7 @@ function template_preprocess_comment(&$variables) {
$variables['changed'] = format_date($comment->changed->value);
}
if (theme_get_setting('toggle_comment_user_picture')) {
if (theme_get_setting('features.comment_user_picture')) {
// To change user picture settings (e.g., image style), edit the 'compact'
// view mode on the User entity.
$variables['user_picture'] = user_view($account, 'compact');
......
......@@ -1166,7 +1166,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']));
if (theme_get_setting('toggle_node_user_picture')) {
if (theme_get_setting('features.node_user_picture')) {
// To change user picture settings (e.g. image style), edit the 'compact'
// view mode on the User entity. Note that the 'compact' view mode might
// not be configured, so remember to always check the theme setting first.
......
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'
......@@ -390,20 +390,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(
......@@ -436,7 +440,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;
......@@ -466,7 +470,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(
......@@ -481,7 +485,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',
......@@ -507,7 +511,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',
......@@ -521,7 +525,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',
......@@ -615,12 +619,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);
}
/**
......@@ -715,14 +714,15 @@ function _system_theme_settings_validate_path($path) {
* Process system_theme_settings form submissions.
*/
function system_theme_settings_submit($form, &$form_state) {
$config = Drupal::config($form_state['values']['config_key']);
// Exclude unnecessary elements before saving.
form_state_values_clean($form_state);
$values = $form_state['values'];
$key = $form_state['values']['var'];
unset($form_state['values']['var']);
unset($form_state['values']['config_key']);
// Extract the name of the theme from the submitted form values, then
// remove it from the array so that it is not saved as part of the variable.
$key = $values['var'];
unset($values['var']);
$values = $form_state['values'];
// If the user uploaded a new logo or favicon, save it to a permanent location
// and use it in place of the default theme-provided file.
......@@ -756,8 +756,7 @@ function system_theme_settings_submit($form, &$form_state) {
}
}
variable_set($key, $values);
drupal_set_message(t('The configuration options have been saved.'));
theme_settings_convert_to_config($values, $config)->save();
cache_invalidate_tags(array('content' => TRUE));
}
......
......@@ -2101,6 +2101,46 @@ function system_update_8054() {
db_add_field('menu_router', 'route_name', $spec);
}
/**
* Moves system theme settings from variables to config.
*/
function system_update_8054() {
// Install the global theme settings from the system module.
update_7_to_8_install_default_config('module', 'system.theme.global');
// Add the global settings to a map for variable to configuration conversion.
$theme_settings_to_config_map = array('theme_settings' => 'system.theme.global');
// Can only upgrade core themes since if you follow the instructions in
// UPGRADE.txt list_themes() would only return core themes at this point.
// Therefore limit to a hard coded list to ensure that this update only
// applies to core provided themes.
$core_themes = array(
'bartik',
'seven',
'stark',
);
// Add the core theme to the variable to configuration conversion map and
// install the default configuration.
foreach ($core_themes as $theme) {
$variable = 'theme_' . $theme . '_settings';
$config_name = $theme . '.settings';
$theme_settings_to_config_map[$variable] = $config_name;
update_7_to_8_install_default_config('theme', $config_name);
}
// Convert array of theme settings from Drupal 7's variable system to Drupal
// 8's configuration management system.
foreach ($theme_settings_to_config_map as $variable => $config_name) {
$config = Drupal::config($config_name);
$theme_settings = update_variable_get($variable);
if (!empty($theme_settings)) {
theme_settings_convert_to_config($theme_settings, $config)->save();
}
update_variable_del($variable);
}
}
/**
* @} End of "defgroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.
......
......@@ -89,7 +89,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.'),
);
}
......
......@@ -97,20 +97,32 @@ function testPictureOnNodeComment() {
$node = $this->drupalCreateNode(array('type' => 'article'));
// Enable user pictures on nodes.
variable_set('theme_settings', array('toggle_node_user_picture' => TRUE));
$this->container->get('config.factory')->get('system.theme.global')->set('features.node_user_picture', TRUE)->save();
// Verify that the image is displayed on the user account page.
$this->drupalGet('node/' . $node->nid);
$this->assertRaw(file_uri_target($file->uri), 'User picture found on node page.');
// Enable user pictures on comments, instead of nodes.
variable_set('theme_settings', array('toggle_comment_user_picture' => TRUE));
$this->container->get('config.factory')->get('system.theme.global')
->set('features.node_user_picture', FALSE)
->set('features.comment_user_picture', TRUE)
->save();
$edit = array(
'comment_body[' . LANGUAGE_NOT_SPECIFIED . '][0][value]' => $this->randomString(),
);
$this->drupalPost('comment/reply/' . $node->nid, $edit, t('Save'));
$this->assertRaw(file_uri_target($file->uri), 'User picture found on comment.');
// Disable user pictures on comments and nodes.
$this->container->get('config.factory')->get('system.theme.global')
->set('features.node_user_picture', FALSE)
->set('features.comment_user_picture', FALSE)
->save();
$this->drupalGet('node/' . $node->nid);
$this->assertNoRaw(file_uri_target($file->uri), 'User picture not found on node and comment.');
}
/**
......
......@@ -744,7 +744,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') . ')';
}
}
......
......@@ -13,7 +13,7 @@
*/
function minimal_install() {
// Disable the user pictures on nodes.
variable_set('theme_settings', array('toggle_node_user_picture' => FALSE));
Drupal::config('system.theme.global')->set('features.node_user_picture', FALSE)->save();
// Allow visitor account creation, but with administrative approval.
config('user.settings')->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)->save();
......
......@@ -28,5 +28,6 @@ regions:
footer_thirdcolumn: 'Footer third column'
footer_fourthcolumn: 'Footer fourth column'
footer: Footer
# @todo D8: Remove once themes have to be installed.
settings:
shortcut_module_link: '0'
......@@ -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'));
......
......@@ -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.
......
......@@ -11,6 +11,7 @@ stylesheets-override:
- vertical-tabs-rtl.css
- jquery.ui.theme.css
settings:
# @todo D8: Remove once themes have to be installed.
shortcut_module_link: '1'
regions:
content: Content
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment