PHP 8 compatibility: Warning: Trying to access array offset on value of type null

Created on 20 October 2023, 8 months ago

Problem/Motivation

The following warning shows frequently in site logs when using PHP 8:

Warning: Trying to access array offset on value of type null in Drupal\feed_block\Plugin\Field\FieldFormatter\RSSFeedFormatter->viewElements() (line 61 of /code/web/modules/contrib/feed_block/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php)

Steps to reproduce

1. Create a feed_block with a no-longer-valid XML source.
2. View the feed_block and check the logs.
3. The above warning will be present.

Proposed resolution

diff --git a/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php b/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php
index 9097296..e44802c 100644
--- a/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/RSSFeedFormatter.php
@@ -57,6 +57,9 @@ class RSSFeedFormatter extends FormatterBase {
           $feed_object = $responseXml->channel->item;
         }
         $inc = 0;
+        if (!isset($feed_object[$inc])) {
+          continue;
+        }
         while ($inc < $item->count) {
           $instance = $feed_object[$inc];
           if (!isset($instance->link)) {
πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024