From c6ca05a47c5da1d1490dde57edafc3f3d6aab1e6 Mon Sep 17 00:00:00 2001
From: Lily Yan <l26yan@uwaterloo.ca>
Date: Wed, 5 Mar 2025 14:56:50 -0500
Subject: [PATCH] ISTWCMS-7265 Make aggregator remove items from the Web
 Resources feed that don't exist

---
 uw_custom_blocks.module | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/uw_custom_blocks.module b/uw_custom_blocks.module
index 88a01a3c..f7758917 100644
--- a/uw_custom_blocks.module
+++ b/uw_custom_blocks.module
@@ -304,6 +304,44 @@ function uw_custom_blocks_cron() {
     \Drupal::state()->set('uw_custom_blocks.block_cache_clear_last_run', $request_time);
     \Drupal::logger('uw_custom_blocks')->notice('Invalidate custom blocks cache tags.');
   }
+
+  // Load all aggregator feeds.
+  $feeds = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->loadMultiple();
+  foreach ($feeds as $feed) {
+    // Check if the feed title matches 'Web-resources news'.
+    if ($feed->label() == 'Web-resources news') {
+      // Fetch the current feed items.
+      $current_items = \Drupal::entityTypeManager()->getStorage('aggregator_item')->loadByProperties(['fid' => $feed->id()]);
+
+      // Fetch the latest feed data from the feed URL from
+      // the Aggregator source using the Guzzle HTTP client.
+      $client = \Drupal::httpClient();
+      $response = $client->get($feed->get('url')->value);
+
+      // Parse the XML response to get the latest feed data.
+      $latest_feed_data = simplexml_load_string($response->getBody()->getContents());
+
+      // Process the latest feed data as needed.
+      // Compare and remove items no longer present in the current feed.
+      foreach ($current_items as $item) {
+        // Set the flag to FALSE.
+        $item_found = FALSE;
+        // Loop through all latest feed data.
+        foreach ($latest_feed_data->channel->item as $latest_item) {
+          // If the current item is in the latest feed data.
+          if ($item->getTitle() == (string) $latest_item->title) {
+            // Set the flag to TRUE.
+            $item_found = TRUE;
+            break;
+          }
+        }
+        // If the flag is false, remove the current feed item.
+        if (!$item_found) {
+          $item->delete();
+        }
+      }
+    }
+  }
 }
 
 /**
-- 
GitLab