From 18f46755bf81a9510f99d39249be1bacad1ca395 Mon Sep 17 00:00:00 2001 From: Wehr Mario Date: Tue, 18 Jul 2023 21:07:02 +0200 Subject: [PATCH] #155 Add support of SCSS in flavor raw scss --- classes/form/flavour_edit_form.php | 6 +----- flavours/flavourslib.php | 27 +++++++++++++-------------- lang/en/theme_boost_union.php | 4 ++-- lib.php | 11 +++++++---- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/classes/form/flavour_edit_form.php b/classes/form/flavour_edit_form.php index 915349651e2..a0f6e0d7300 100644 --- a/classes/form/flavour_edit_form.php +++ b/classes/form/flavour_edit_form.php @@ -213,11 +213,7 @@ public function definition() { $mform->addRule('bootstrapcolordanger', get_string('validateerror', 'admin'), 'theme_boost_union_colourpicker_rule'); // Add custom css as textarea element. - // Note: In the current state of implementation, this setting only allows the usage of custom CSS, not SCSS. - // It will be appended to the stack of CSS code which is shipped to the browser. - // There is a follow-up issue on Github to add SCSS support. - // When this is realized, the widget's title string should be changed to 'theme_boost/rawscss'. - $mform->addElement('textarea', 'look_rawscss', get_string('flavourscustomcss', 'theme_boost_union'), array('rows' => 15)); + $mform->addElement('textarea', 'look_rawscss', get_string('rawscss', 'theme_boost'), array('rows' => 15)); $mform->setType('title', PARAM_TEXT); $mform->addHelpButton('look_rawscss', 'flavourscustomcss', 'theme_boost_union'); diff --git a/flavours/flavourslib.php b/flavours/flavourslib.php index e9fd2c4a357..00ba572a0e7 100644 --- a/flavours/flavourslib.php +++ b/flavours/flavourslib.php @@ -317,30 +317,29 @@ function theme_boost_union_flavour_exists_for_cohort($cohortid) { /** * Helper function do get a config key from flavour item. * - * @param $flavourid - * @param $configkey + * @param string $flavourid + * @param string $configkey * - * @return false + * @return string/bool */ -function theme_boost_union_get_flavour_config_item_for_id($flavourid, $configkey) { +function theme_boost_union_get_flavour_config_item_for_id(string $flavourid, string $configkey) { global $DB; $cache = cache::make('theme_boost_union', 'flavours'); $flavouridkey = 'flavour_' . $flavourid; - // Get the cached flavour for the current user flavour id. - $flavour = $cache->get($flavouridkey); + // Get the cached flavour config for the current user flavour id. + $flavourconfig = $cache->get($flavouridkey); - // If we got a cached flavour. - if ($flavour == false) { - $flavour = $DB->get_record('theme_boost_union_flavours', ['id' => $flavourid]); + // If we got a cached flavour config. + if ($flavourconfig == false) { + $flavourconfig = $DB->get_record('theme_boost_union_flavours', ['id' => $flavourid]); + $cache->set($flavouridkey, $flavourconfig); } - if ($flavour !== false ) { - $cache->set($flavouridkey, $flavour); - if (isset($flavour->{$configkey})) { // ...isset returns true only if property exits and value != null;. - return $flavour->{$configkey}; - } + if (isset($flavourconfig->{$configkey})) { // ...isset returns true only if property exits and value != null;. + return $flavourconfig->{$configkey}; } + return false; } diff --git a/lang/en/theme_boost_union.php b/lang/en/theme_boost_union.php index fb46ee79cff..d43dfdb54da 100644 --- a/lang/en/theme_boost_union.php +++ b/lang/en/theme_boost_union.php @@ -645,8 +645,8 @@ $string['flavoursbrandcolour'] = 'Brand colour '; $string['flavoursbrandcolour_help'] = 'With this setting, the flavour will override the brand colour which is configured in Boost Union\'s look settings.'; $string['flavourscreateflavour'] = 'Create flavour'; -$string['flavourscustomcss'] = 'Custom CSS'; -$string['flavourscustomcss_help'] = 'With this setting, you can write custom CSS for the flavour. It will be appended to the stack of CSS code which is shipped to the browser as soon as the flavour applies. Please note that in the current state of implementation, this setting only allows the usage of custom CSS, not SCSS.'; +$string['flavourscustomcss'] = 'Custom SCSS'; +$string['flavourscustomcss_help'] = 'With this setting, you can write custom SCSS for the flavour. It will be appended to the stack of CSS code which is shipped to the browser as soon as the flavour applies.'; $string['flavoursdelete'] = 'Delete'; $string['flavoursdeleteflavour'] = 'Delete flavour'; $string['flavoursdeleteconfirmation'] = 'Do you really want to delete the flavour {$a}?'; diff --git a/lib.php b/lib.php index c3ab75f7109..0c3f15ceff3 100644 --- a/lib.php +++ b/lib.php @@ -231,11 +231,14 @@ function theme_boost_union_get_pre_scss($theme) { } $scss .= '$blockregionoutsiderightwidth: '.$blockregionoutsiderightwidth.";\n"; - // Prepend pre-scss. - if (!empty($theme->settings->scsspre)) { - $scss .= $theme->settings->scsspre; + if (isset($flavourid)) { + $rawscsspre = theme_boost_union_get_flavour_config_item_for_id($flavourid, 'look_rawscss'); + // Append pre-scss. + if ($rawscsspre !== false) { + $scss .= "\n/** RAW-SCSS from theme_boost_union_get_pre_scss **/\n" . $rawscsspre; + } } - + // Since setting "precss" is originally from parent boost it is added in theme_boost_get_pre_scss. return $scss; }