From c1a07ff96d0579a62a39677a32e70ceab58651c1 Mon Sep 17 00:00:00 2001
From: Sergiu Rosu <sr2168@cam.ac.uk>
Date: Tue, 18 Mar 2025 14:17:22 +0000
Subject: [PATCH] fix: add extra checks to avoid running foreach on null

---
 template.php | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/template.php b/template.php
index 0aa16eb..fce5136 100644
--- a/template.php
+++ b/template.php
@@ -694,19 +694,26 @@ function cambridge_theme_block_view_alter(&$data, $block) {
  * Cycle through a tree and make items marked as faux active as actually active.
  */
 function _cambridge_theme_add_active_item($items) {
+  if (!is_array($items)) {
+    return $items; // Return as-is if not an array
+  }
+
   foreach ($items as $key => $item) {
-    if (FALSE === is_int($key)) {
+    if (!is_int($key)) {
       continue;
     }
 
     if (
-      isset($item['#original_link']['cambridge_theme_faux_active'])
-      &&
-      FALSE === in_array('active', $item['#attributes']['class'])
+      isset($item['#original_link']['cambridge_theme_faux_active']) &&
+      !in_array('active', $item['#attributes']['class'] ?? [])
     ) {
       $items[$key]['#attributes']['class'][] = 'active';
     }
-    $items[$key]['#below'] = _cambridge_theme_add_active_item($items[$key]['#below']);
+
+    // Ensure '#below" exists and is an array
+    if (isset($items[$key]['#below']) && is_array($items[$key]['#below'])) {
+      $items[$key]['#below'] = _cambridge_theme_add_active_item($items[$key]['#below']);
+    }
   }
 
   return $items;
-- 
GitLab