Skip to content

Instantly share code, notes, and snippets.

@PeloNZ
Created October 30, 2014 12:18
Show Gist options
  • Save PeloNZ/d258a296d1fcc33eaef5 to your computer and use it in GitHub Desktop.
Save PeloNZ/d258a296d1fcc33eaef5 to your computer and use it in GitHub Desktop.
example for moodle & totara add custom css to theme settings page
commit 5472efadcbe6fb1b2e45fdd06c80fbf97254245b (HEAD, refs/heads/theme-2.5-custom-dev)
Author: Chris Wharton <[email protected]>
Date: Thu Oct 30 12:08:53 2014 +0000
theme/custom: Add customcss setting
This is mainly for rapid fixes in between deployments.
diff --git a/config.php b/config.php
index 771b853..193a882 100644
--- a/config.php
+++ b/config.php
@@ -44,7 +44,8 @@ $THEME->sheets = array(
'pagelayout',
'css3', /** Sets up CSS 3 + browser specific styles **/
'filemanager',
- 'print'
+ 'print',
+ 'settings',
);
$THEME->editor_sheets = array('editor');
@@ -191,3 +192,4 @@ $THEME->rarrow = '&#8250;';
$THEME->enable_dock = true;
$THEME->rendererfactory = 'theme_overridden_renderer_factory';
+$THEME->csspostprocess = 'theme_custom_process_css';
diff --git a/lang/en/theme_custom.php b/lang/en/theme_custom.php
index a771ba6..941c968 100644
--- a/lang/en/theme_custom.php
+++ b/lang/en/theme_custom.php
@@ -126,3 +126,6 @@ $string['facetoface:discountcost'] = 'Discount Cost';
$string['ourlatestnews'] = 'Our latest news';
$string['followcustomupdates'] = 'Follow updates about the development of the custom portal';
+// theme settings - custom css
+$string['customcss'] = 'Custom CSS';
+$string['customcssdesc'] = 'Any CSS you enter here will be added to every page allowing your to easily customise this theme.';
diff --git a/lib.php b/lib.php
index 1847866..f56a994 100644
--- a/lib.php
+++ b/lib.php
@@ -1,5 +1,7 @@
<?php
+defined('MOODLE_INTERNAL') || die();
+
+
+/**
+ * Makes our changes to the CSS
+ *
+ * @param string $css
+ * @param theme_config $theme
+ * @return string
+ */
+function theme_custom_process_css($css, $theme) {
+ // Set the custom CSS
+ if (!empty($theme->settings->customcss)) {
+ $customcss = $theme->settings->customcss;
+ } else {
+ $customcss = null;
+ }
+ $css = theme_custom_set_customcss($css, $customcss);
+
+ return $css;
+}
+
+/**
+ * Sets the custom css variable in CSS
+ *
+ * @param string $css
+ * @param mixed $customcss
+ * @return string
+ */
+function theme_custom_set_customcss($css, $customcss) {
+ $tag = '[[setting:customcss]]';
+ $replacement = $customcss;
+ if (is_null($replacement)) {
+ $replacement = '';
+ }
+ $css = str_replace($tag, $replacement, $css);
+ return $css;
+}
diff --git a/settings.php b/settings.php
index 3c7baff..d732d4a 100644
--- a/settings.php
+++ b/settings.php
@@ -190,4 +190,12 @@ if ($ADMIN->fulltree) {
$default = '';
$setting = new admin_setting_confightmleditor($name, $title, $description, $default);
$settings->add($setting);
+
+ // Custom CSS file.
+ $name = 'theme_custom/customcss';
+ $title = new lang_string('customcss','theme_custom');
+ $description = new lang_string('customcssdesc', 'theme_custom');
+ $setting = new admin_setting_configtextarea($name, $title, $description, '');
+ $setting->set_updatedcallback('theme_reset_all_caches');
+ $settings->add($setting);
}
diff --git a/style/settings.css b/style/settings.css
new file mode 100644
index 0000000..d464bb2
--- /dev/null
+++ b/style/settings.css
@@ -0,0 +1,2 @@
+/** Custom CSS defined via theme settings **/
+[[setting:customcss]]
diff --git a/version.php b/version.php
index 3f3df71..71ba567 100644
--- a/version.php
+++ b/version.php
@@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die;
-$plugin->version = 2011082400; // The current module version (Date: YYYYMMDDXX)
+$plugin->version = 2014103000; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2011081700; // Requires this Moodle version
$plugin->component = 'theme_custom'; // Full name of the plugin (used for diagnostics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment