Skip to content

Commit

Permalink
Issue #6122: Allow themes to have config folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
docwilmot committed Sep 5, 2023
1 parent 582eab6 commit d186fd1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
48 changes: 35 additions & 13 deletions core/includes/config.inc
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,27 @@ function config_load_multiple($names, $type = 'active') {
}

/**
* Moves the default config supplied by a module to the live config directory.
* Moves the default config supplied by a project to the live config directory.
*
* @param string $module
* The name of the module we are installing.
* @param string $project
* The name of the project we are installing.
* @param string|NULL $config_name
* (optional) If wanting to copy just a single configuration file from the
* module, specify the configuration file name without the extension.
* project, specify the configuration file name without the extension.
*
* @since 1.26.0 First parameter changed from $module to $project.
*/
function config_install_default_config($module, $config_name = NULL) {
$module_config_dir = backdrop_get_path('module', $module) . '/config';
if (is_dir($module_config_dir)) {
$storage = new ConfigFileStorage($module_config_dir);
$files = glob($module_config_dir . '/*.json');
function config_install_default_config($project, $config_name = NULL) {
$project_path = NULL;
foreach (array('module', 'theme') as $project_type) {
if ($project_path = backdrop_get_path($project_type, $project)) {
break;
}
}
$project_config_dir = $project_path . '/config';
if (is_dir($project_config_dir)) {
$storage = new ConfigFileStorage($project_config_dir);
$files = glob($project_config_dir . '/*.json');
foreach ($files as $file) {
// Load config data into the active store and write it out to the
// file system in the Backdrop config directory. Note the config name
Expand All @@ -305,11 +313,25 @@ function config_install_default_config($module, $config_name = NULL) {
}

/**
* Uninstall all the configuration provided by a module.
* Uninstall all the configuration provided by a project.
*
* @param string $project
* The name of the project we are uninstalling.
* @param string|NULL $config_name
* (optional) If wanting to remove just a single configuration file from the
* project, specify the configuration file name without the extension.
*
* @since 1.26.0 First parameter changed from $module to $project.
*/
function config_uninstall_config($module, $config_name = NULL) {
backdrop_load('module', $module);
if ($configs = module_invoke($module, 'config_info')) {
function config_uninstall_config($project, $config_name = NULL) {
// If this is a theme key, load the matching template.php file.
if (!backdrop_load('module', $project) && $theme_path = backdrop_get_path('theme', $project)) {
if (file_exists($theme_path . '/template.php')) {
include_once $theme_path . '/template.php';
}
}

if ($configs = module_invoke($project, 'config_info')) {
foreach ($configs as $config_name => $config_info) {
if (isset($config_info['name_key'])) {
$sub_names = config_get_names_with_prefix($config_name . '.');
Expand Down
8 changes: 8 additions & 0 deletions core/includes/theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,9 @@ function theme_get_setting($setting_name, $theme = NULL) {
* TRUE if the theme has settings, FALSE otherwise.
*/
function theme_has_settings($theme) {
if (config_get_names_with_prefix($theme . '.settings')) {
return TRUE;
}
$themes = list_themes();
$theme_info = $themes[$theme];
if (!empty($theme_info->info['settings'])) {
Expand Down Expand Up @@ -1643,6 +1646,9 @@ function theme_enable($theme_list) {
->condition('type', 'theme')
->condition('name', $key)
->execute();

// Copy any default configuration data to the system config directory.
config_install_default_config($key);
}

list_themes(TRUE);
Expand Down Expand Up @@ -1676,6 +1682,8 @@ function theme_disable($theme_list) {
->condition('type', 'theme')
->condition('name', $key)
->execute();

config_uninstall_config($key);
}

list_themes(TRUE);
Expand Down
1 change: 1 addition & 0 deletions core/modules/simpletest/tests/theme_test.module
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function theme_test_system_theme_info() {
$themes['test_theme'] = backdrop_get_path('module', 'theme_test') . '/themes/test_theme/test_theme.info';
$themes['test_basetheme'] = backdrop_get_path('module', 'theme_test') . '/themes/test_basetheme/test_basetheme.info';
$themes['test_subtheme'] = backdrop_get_path('module', 'theme_test') . '/themes/test_subtheme/test_subtheme.info';
$themes['test_theme_config'] = backdrop_get_path('module', 'theme_test') . '/themes/test_theme_config/test_theme_config.info';
return $themes;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_config_name": "test_theme_config.settings",
"test_theme_checkbox_one": true,
"test_theme_checkbox_two": false,
"test_theme_textfield": "Default field value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name = Test theme config
description = Theme for testing the theme default configuration
version = BACKDROP_VERSION
backdrop = 1.x
type = theme
hidden = TRUE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @file
* Theme settings file for the test_theme_config theme.
*/

$form['test_theme_checkbox_one'] = array(
'#type' => 'checkbox',
'#title' => 'Test theme checkbox one',
'#default_value' => theme_get_setting('test_theme_checkbox_one', 'test_theme_config'),
);
$form['test_theme_checkbox_two'] = array(
'#type' => 'checkbox',
'#title' => 'Test theme checkbox two',
'#default_value' => theme_get_setting('test_theme_checkbox_two', 'test_theme_config'),
);
$form['test_theme_textfield'] = array(
'#type' => 'textfield',
'#title' => 'Test theme textfield',
'#default_value' => theme_get_setting('test_theme_textfield', 'test_theme_config'),
);
15 changes: 14 additions & 1 deletion core/modules/system/tests/system.test
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,8 @@ class SystemThemeFunctionalTest extends BackdropWebTestCase {
*/
function testThemeSettings() {
theme_enable(array('basis'));
module_enable(array('color'));
module_enable(array('color', 'theme_test'));
backdrop_flush_all_caches();
// Change Basis settings so config file is created.
$edit = array();
$this->backdropPost('admin/appearance/settings/basis', $edit, t('Save theme settings'));
Expand All @@ -1870,6 +1871,18 @@ class SystemThemeFunctionalTest extends BackdropWebTestCase {
// Check that config file no longer exists.
$config_after = config('basis.settings');
$this->assertTrue($config_after->isNew(), 'Basis config file does not exist.');

// Test that the theme-specific settings form appears and has correct
// values pulled from a config file, without a settings array in the info
// file.
module_enable(array('theme_test'));
backdrop_flush_all_caches();
theme_enable(array('test_theme_config'));

$this->backdropGet('admin/appearance/settings/test_theme_config');
$this->assertFieldChecked('edit-test-theme-checkbox-one', 'The checkbox one setting is checked.');
$this->assertNoFieldChecked('edit-test-theme-checkbox-two', 'The checkbox two setting is unchecked.');
$this->assertFieldById('edit-test-theme-textfield', 'Default field value');
}
}

Expand Down

1 comment on commit d186fd1

@backdrop-ci
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.