Problem/Motivation

I couldn't find an existing issue for this exception.

exception: [Notice] Line 926 of modules/forum/forum.module: Trying to access array offset on value of type null

Here's the relevant code (including line numbers initially):

 914 function forum_get_topics($tid, $sortby, $forum_per_page) {                     
 915   global $user, $forum_topic_list_header;                                       
 916                                                                                 
 917   $forum_topic_list_header = array(                                             
 918     NULL,                                                                       
 919     array('data' => t('Topic'), 'field' => 'f.title'),                          
 920     array('data' => t('Replies'), 'field' => 'f.comment_count'),                
 921     array('data' => t('Last reply'), 'field' => 'f.last_comment_timestamp'),    
 922   );                                                                            
 923                                                                                 
 924   $order = _forum_get_topic_order($sortby);                                     
 925   for ($i = 0; $i < count($forum_topic_list_header); $i++) {                    
 926     if ($forum_topic_list_header[$i]['field'] == $order['field']) {             
 927       $forum_topic_list_header[$i]['sort'] = $order['sort'];                    
 928     }                                                                           
 929   } 

Proposed resolution

Fix the bug; avoid the PHP exception / notice in PHP 7.4.

Remaining tasks

Patch, review, commit etc..

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

See parent issue.

CommentFileSizeAuthor
#8 3125824-8.patch566 bytestaran2l
#5 3125824-5.patch565 bytestaran2l
#2 3125824-2.patch614 bytesmcdruid

Comments

mcdruid created an issue. See original summary.

mcdruid’s picture

Title: PHP 7.4 exception: [Notice] Line 926 of modules/forum/forum.module: Trying to access array offset on value of type null » PHP 7.4 notce in forum.module forum_get_topic() Trying to access array offset on value of type null
Status: Active » Needs review
StatusFileSize
new614 bytes
mcdruid’s picture

Confirmation that this patch prevents the forum module exceptions in the current baseline test over in #3081386-70: [META] Fully support PHP 7.4 in Drupal 7:

$ curl -s https://www.drupal.org/pift-ci-job/1640782 | grep -o 'exception: .* Line [0-9]* of .*:' | perl -pe 's#<.*?>##g' | sort | uniq -c | sort -rn | diff - <(curl -s https://www.drupal.org/pift-ci-job/1641983 | grep -o 'exception: .* Line [0-9]* of .*:' | perl -pe 's#<.*?>##g' | sort | uniq -c | sort -rn)
2d1
<      16 exception: [Notice] Line 926 of modules/forum/forum.module:
taran2l’s picture

My idea was to fix it in a slightly different way:

925 -  for ($i = 0; $i < count($forum_topic_list_header); $i++) {
925 +  for ($i = 1; $i < count($forum_topic_list_header); $i++) {

As 0 indexed element is NULL, so just skip it

taran2l’s picture

StatusFileSize
new565 bytes
avpaderno’s picture

Title: PHP 7.4 notce in forum.module forum_get_topic() Trying to access array offset on value of type null » PHP 7.4 notice in forum.module forum_get_topic() Trying to access array offset on value of type null
Status: Needs review » Needs work
avpaderno’s picture

Title: PHP 7.4 notice in forum.module forum_get_topic() Trying to access array offset on value of type null » PHP 7.4 notice in forum.module: forum_get_topic() tries to access an array offset on value of type null
taran2l’s picture

Status: Needs work » Needs review
StatusFileSize
new566 bytes
taran2l’s picture

Confirmation that patch in #8 has the same effect as #2:

curl -s https://www.drupal.org/pift-ci-job/1640782 | grep -o 'exception: .* Line [0-9]* of .*:' | perl -pe 's#<.*?>##g' | sort | uniq -c | sort -rn | diff - <(curl -s https://www.drupal.org/pift-ci-job/1643137 | grep -o 'exception: .* Line [0-9]* of .*:' | perl -pe 's#<.*?>##g' | sort | uniq -c | sort -rn)
2d1
<   16 exception: [Notice] Line 926 of modules/forum/forum.module:
mcdruid’s picture

Title: PHP 7.4 notice in forum.module: forum_get_topic() tries to access an array offset on value of type null » Trying to access an array offset on value of type null in forum_get_topics()
Status: Needs review » Reviewed & tested by the community
Issue tags: +Pending Drupal 7 commit

Thanks @Taran2L, I like the approach of starting at index 1.

I wonder if it's worth a comment to point out that we're deliberately skipping the first item? Probably not necessary.

In D8 this code is in \Drupal\forum\ForumManager::getTopics() and there is no NULL first item.

I've tweaked the issue title to match recent commit messages.

taran2l’s picture

@mcdruid, I think a comment will add some clarity

mcdruid’s picture

Suggested comment to add on commit, before the for loop:

// Skip element with index 0 which is NULL.
fabianx’s picture

RTBM - thanks all!

And I agree the comment to add looks good.

  • mcdruid committed d647c02 on 7.x
    Issue #3125824 by Taran2L, mcdruid: Trying to access array offset on...
mcdruid’s picture

Title: Trying to access an array offset on value of type null in forum_get_topics() » Trying to access array offset on value of type null in forum_get_topics()
Status: Reviewed & tested by the community » Fixed
Issue tags: -Pending Drupal 7 commit

Thanks everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.