MDL-73273 navigation: Add checks to enforce action param types

* Added checks in navigationlib to enforce the data type of the passed in action
* Add nodes with non moodleurls to the end of the secondary nav
This commit is contained in:
Peter Dias
2022-02-15 11:30:07 +08:00
parent d24a4ab56f
commit bdf4f8a651
3 changed files with 141 additions and 7 deletions
+28
View File
@@ -328,6 +328,11 @@ class navigation_node implements renderable {
*/
public static function create($text, $action=null, $type=self::TYPE_CUSTOM,
$shorttext=null, $key=null, pix_icon $icon=null) {
if ($action && !($action instanceof moodle_url || $action instanceof action_link)) {
debugging(
"It is required that the action provided be either an action_url|moodle_url." .
" Please update your definition.", E_NOTICE);
}
// Properties array used when creating the new navigation node
$itemarray = array(
'text' => $text,
@@ -363,6 +368,9 @@ class navigation_node implements renderable {
* @return navigation_node
*/
public function add($text, $action=null, $type=self::TYPE_CUSTOM, $shorttext=null, $key=null, pix_icon $icon=null) {
if ($action && is_string($action)) {
$action = new moodle_url($action);
}
// Create child node
$childnode = self::create($text, $action, $type, $shorttext, $key, $icon);
@@ -698,6 +706,26 @@ class navigation_node implements renderable {
return !empty($this->action);
}
/**
* Used to easily determine if the action is an internal link.
*
* @return bool
*/
public function has_internal_action(): bool {
global $CFG;
if ($this->has_action()) {
$url = $this->action();
if ($this->action() instanceof \action_link) {
$url = $this->action()->url;
}
if (($url->out() === $CFG->wwwroot) || (strpos($url->out(), $CFG->wwwroot.'/') === 0)) {
return true;
}
}
return false;
}
/**
* Used to easily determine if this link in the breadcrumbs is hidden.
*