Skip to content

Commit

Permalink
MDL-80037 theme: Indicate when $CFG->theme is defined in config.php
Browse files Browse the repository at this point in the history
* And prevent selection of theme if $CFG->theme is defined in config.php
  • Loading branch information
junpataleta committed Nov 17, 2023
1 parent b58d1fd commit 1819028
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion admin/amd/build/themeselector/preview_modal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion admin/amd/build/themeselector/preview_modal.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion admin/amd/src/themeselector/preview_modal.js
Expand Up @@ -29,6 +29,7 @@ import Templates from 'core/templates';
import {getString} from 'core/str';

const SELECTORS = {
THEMES_CONTAINER: 'themelist',
PREVIEW: '[data-action="preview"]',
};

Expand Down Expand Up @@ -66,6 +67,8 @@ const buildModal = async(element) => {
// This string can be long. We will fetch it with JS as opposed to passing it as an attribute.
let description = await getString('choosereadme', 'theme_' + element.getAttribute('data-choose'));

const themesContainer = document.getElementById(SELECTORS.THEMES_CONTAINER);
const definedInConfig = parseInt(themesContainer.dataset.definedinconfig);
// Prepare data for modal.
const data = {
name: element.getAttribute('data-name'),
Expand All @@ -75,11 +78,12 @@ const buildModal = async(element) => {
actionurl: element.getAttribute('data-actionurl'),
choose: element.getAttribute('data-choose'),
sesskey: element.getAttribute('data-sesskey'),
definedinconfig: definedInConfig,
};

// Determine which modal template we should use.
let modalTemplate = ModalSaveCancel;
if (data.current) {
if (data.current || data.definedinconfig) {
modalTemplate = ModalCancel;
}

Expand Down
8 changes: 7 additions & 1 deletion admin/classes/output/theme_selector.php
Expand Up @@ -34,13 +34,18 @@ class theme_selector implements renderable, templatable {
/** @var array $themedata Theme data to pass to the template. */
private $themedata = null;

/** @var bool Whether $CFG->theme is defined in config.php. */
private $definedinconfig;

/**
* Constructor.
*
* @param array $themedata Theme data used for template.
* @param bool $definedinconfig Whether $CFG->theme is defined in config.php.
*/
public function __construct(array $themedata) {
public function __construct(array $themedata, bool $definedinconfig = false) {
$this->themedata = $themedata;
$this->definedinconfig = $definedinconfig;
}

/**
Expand All @@ -59,6 +64,7 @@ public function export_for_template(renderer_base $output): stdClass {
$resetbutton = new \single_button($reseturl, get_string('themeresetcaches', 'admin'), 'post',
\single_button::BUTTON_SECONDARY);
$data->resetbutton = $resetbutton->export_for_template($output);
$data->definedinconfig = $this->definedinconfig;

return $data;
}
Expand Down
3 changes: 3 additions & 0 deletions admin/templates/themeselector/theme_card.mustache
Expand Up @@ -58,6 +58,9 @@
<div class="d-flex align-items-end flex-column p-3">
{{#current}}
<strong><span class="text-success" id="current-theme-{{choose}}">{{#str}}currenttheme, moodle{{/str}}</span></strong>
{{#definedinconfig}}
<div class="alert alert-info p-1">{{#str}}configoverride, admin{{/str}}</div>
{{/definedinconfig}}
{{/current}}
{{^current}}
{{#actionurl}}
Expand Down
5 changes: 4 additions & 1 deletion admin/templates/themeselector/theme_preview_modal.mustache
Expand Up @@ -39,9 +39,12 @@
<div class="flex-grow-1">
<p><strong>{{#str}}themepreviewdescription, moodle, {{name}}{{/str}}</strong></p>
</div>
<div>
<div class="d-flex align-items-end flex-column">
{{#current}}
<strong><span class="text-success">{{#str}}currenttheme, moodle{{/str}}</span></strong>
{{#definedinconfig}}
<div class="alert alert-info p-1">{{#str}}configoverride, admin{{/str}}</div>
{{/definedinconfig}}
{{/current}}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion admin/templates/themeselector/theme_selector.mustache
Expand Up @@ -62,7 +62,7 @@
{{>core/single_button}}
</div>
{{/resetbutton}}
<div class="card-deck dashboard-card-deck" id="themelist" data-region="card-deck" role="list">
<div class="card-deck dashboard-card-deck" id="themelist" data-region="card-deck" role="list" data-definedinconfig="{{definedinconfig}}">
{{#themes}}
{{>core_admin/themeselector/theme_card}}
{{/themes}}
Expand Down
13 changes: 10 additions & 3 deletions admin/themeselector.php
Expand Up @@ -42,8 +42,15 @@
theme_reset_all_caches();
}

$definedinconfig = array_key_exists('theme', $CFG->config_php_settings);
if ($definedinconfig) {
$forcedthemename = get_string('pluginname', 'theme_'.$CFG->theme);
// Show a notification that the theme is defined in config.php.
\core\notification::info(get_string('themedefinedinconfigphp', 'admin', $forcedthemename));
}

// Change theme.
if (!empty($choose) && confirm_sesskey()) {
if (!$definedinconfig && !empty($choose) && confirm_sesskey()) {

// Load the theme to make sure it is valid.
$theme = theme_config::load($choose);
Expand Down Expand Up @@ -105,7 +112,7 @@
if ($themename === $CFG->theme) {
$themedata['current'] = true;
$currentthemeindex = $index;
} else {
} else if (!$definedinconfig) {
// Form params.
$actionurl = new moodle_url('/admin/themeselector.php');
$themedata['actionurl'] = $actionurl;
Expand All @@ -124,7 +131,7 @@
}

// Show theme selector.
$renderable = new \core_admin\output\theme_selector($data);
$renderable = new \core_admin\output\theme_selector($data, $definedinconfig);
$renderer = $PAGE->get_renderer('core', 'admin');
echo $renderer->theme_selector_list($renderable);

Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -1452,6 +1452,7 @@
$string['testoutgoingmailconf_subject'] = '{$a->site}: test message. {$a->additional} Sent: {$a->time}';
$string['testoutgoingmailconf_subjectadditional'] = 'Additional subject';
$string['testoutgoingmailconf_toemail'] = 'To email address';
$string['themedefinedinconfigphp'] = 'The theme cannot be changed because it is set to {$a} in config.php.';
$string['themedesignermode'] = 'Theme designer mode';
$string['themedesignermodewarning'] = 'Theme designer mode is enabled. This should not be enabled on production sites as it can significantly reduce performance.';
$string['themelist'] = 'Theme list';
Expand Down

0 comments on commit 1819028

Please sign in to comment.