diff --git a/lib/navigationlib.php b/lib/navigationlib.php
index <HASH>..<HASH> 100644
--- a/lib/navigationlib.php
+++ b/lib/navigationlib.php
@@ -672,6 +672,20 @@ class navigation_node implements renderable {
             $this->parent->make_inactive();
         }
     }
+
+    /**
+     * Hides the node and any children it has.
+     *
+     * @since 2.5
+     */
+    public function hide() {
+        $this->display = false;
+        if ($this->has_children()) {
+            foreach ($this->children as $child) {
+                $child->hide();
+            }
+        }
+    }
 }
 
 /**
@@ -2492,18 +2506,18 @@ class global_navigation extends navigation_node {
     public function set_expansion_limit($type) {
         global $SITE;
         $nodes = $this->find_all_of_type($type);
-        foreach ($nodes as &$node) {
+        foreach ($nodes as $node) {
             // We need to generate the full site node
             if ($type == self::TYPE_COURSE && $node->key == $SITE->id) {
                 continue;
             }
-            foreach ($node->children as &$child) {
+            foreach ($node->children as $child) {
                 // We still want to show course reports and participants containers
                 // or there will be navigation missing.
                 if ($type == self::TYPE_COURSE && $child->type === self::TYPE_CONTAINER) {
                     continue;
                 }
-                $child->display = false;
+                $child->hide();
             }
         }
         return true;
MDL-<I> navigation: fixed expansion limit issue hiding nodes