Skip to content

Commit

Permalink
Upgrade: Migrate the before_standard_html_head() function to the new …
Browse files Browse the repository at this point in the history
…hook callback on Moodle 4.4, resolves #604.
  • Loading branch information
abias committed Mar 23, 2024
1 parent 98aba70 commit dc0e362
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ moodle-theme_boost_union
Changes
-------

### Unreleased

* 2024-03-22 - Upgrade: Migrate the before_standard_html_head() function to the new hook callback on Moodle 4.4, resolves #604.

### v4.3-r10

* 2024-03-18 - Improvement: Add prefixes to the sessionStorage keys in the scrollspy implementation, resolves #598.
Expand Down
41 changes: 41 additions & 0 deletions classes/local/hook/output/before_standard_head_html_generation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace theme_boost_union\local\hook\output;

/**
* Theme Boost Union - Hook: Allows plugins to add any elements to the page <head> html tag.
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class before_standard_head_html_generation {
/**
* Callback to add head elements.
*
* @param \core\hook\output\before_standard_head_html_generation $hook
*/
public static function callback(\core\hook\output\before_standard_head_html_generation $hook): void {
global $CFG;

// Require local library.
require_once($CFG->dirroot.'/theme/boost_union/locallib.php');

// Call callback implementation.
theme_boost_union_callbackimpl_before_standard_html($hook);
}
}
33 changes: 33 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Theme Boost Union - Hook callbacks.
*
* @package theme_boost_union
* @copyright 2024 Alexander Bias <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$callbacks = [
[
'hook' => \core\hook\output\before_standard_head_html_generation::class,
'callback' => [\theme_boost_union\local\hook\output\before_standard_head_html_generation::class, 'callback'],
'priority' => 0,
],
];
27 changes: 3 additions & 24 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,35 +499,14 @@ function theme_boost_union_pluginfile($course, $cm, $context, $filearea, $args,
}

/**
* Callback to add head elements.
*
* We use this callback to inject the flavour's CSS code to the page.
* Callback to add head elements (for releases up to Moodle 4.3).
*
* @return string
*/
function theme_boost_union_before_standard_html_head() {
global $CFG, $PAGE;

// Initialize HTML.
$html = '';

// If a theme other than Boost Union or a child theme of it is active, return directly.
// This is necessary as the before_standard_html_head() callback is called regardless of the active theme.
if ($PAGE->theme->name != 'boost_union' && !in_array('boost_union', $PAGE->theme->parents)) {
return $html;
}

// Require local library.
require_once($CFG->dirroot . '/theme/boost_union/locallib.php');

// Add the flavour CSS to the page.
theme_boost_union_add_flavourcss_to_page();

// Add the touch icons to the page.
$html .= theme_boost_union_get_touchicons_html_for_page();

// Return the HTML code.
return $html;
// Call and return callback implementation.
return theme_boost_union_callbackimpl_before_standard_html();
}

/**
Expand Down
52 changes: 52 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1961,3 +1961,55 @@ function($favourite) {

return $html;
}

/**
* Callback to add head elements.
* This function is implemented here and used from two locations:
* -> function theme_boost_union_before_standard_html_head in lib.php (for releases up to Moodle 4.3)
* -> class theme_boost_union\local\hook\output\before_standard_head_html_generation (for releases from Moodle 4.4 on).
*
* We use this callback
* -> to inject the flavour's CSS code to the page
* -> to add the touch icons to the page
*
* @param \core\hook\output\before_standard_head_html_generation $hook If the hook is passed, the hook implementation will
* be used. If not, the legacy implementation will
* be used.
* @return string|void The legacy implementation will return a string, the hook implementation will return nothing.
*/
function theme_boost_union_callbackimpl_before_standard_html(&$hook = null) {
global $CFG, $PAGE;

// Require local library.
require_once($CFG->dirroot.'/theme/boost_union/locallib.php');

// Initialize HTML.
$html = '';

// If a theme other than Boost Union or a child theme of it is active, return directly.
// This is necessary as the callback is called regardless of the active theme.
if ($PAGE->theme->name != 'boost_union' && !in_array('boost_union', $PAGE->theme->parents)) {
if ($hook != null) {
return;
} else {
return $html;
}
}

// Require local library.
require_once($CFG->dirroot . '/theme/boost_union/locallib.php');

// Add the flavour CSS to the page.
theme_boost_union_add_flavourcss_to_page();

// Add the touch icons to the page.
$html .= theme_boost_union_get_touchicons_html_for_page();

if ($hook != null) {
// Add the HTML code to the hook.
$hook->add_html($html);
} else {
// Return the HTML code.
return $html;
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'theme_boost_union';
$plugin->version = 2023102031;
$plugin->version = 2023102032;
$plugin->release = 'v4.3-r10';
$plugin->requires = 2023100900;
$plugin->supported = [403, 403];
Expand Down

0 comments on commit dc0e362

Please sign in to comment.