Issue Bug Closed

#101925Check if index exists in stdWrap.listNum to avoid PHP warnings

Michael Stopp 09/15/23, 08:33 updated 07/09/24, 15:19

Description

Let’s assume we want to use the 2nd image stored in a page’s media field as a background image. We can do something like this in TypoScript to generate some inline CSS:

10 = IMG_RESOURCE
10 {
  file.import.data = levelmedia:-1, slide
  file.import.listNum = 1
  file.treatIdAsReference = 1
  stdWrap.wrap = body {background: url(|) no-repeat; background-size: cover; background-attachment: fixed;}
}

But if there is no 2nd image, a PHP warning is generated, which can flood your sys_log over time:

Core: Error handler (FE): PHP Warning: Undefined array key 1 in /typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php line 2592 

The listNum() method in ContentObjectRenderer currently just blindly returns the specified/calculated index:

return $temp[$index];

I think it would be cleaner and better to do something like this (same behaviour as if the exploded content was empty):

return $temp[$index] ?? '';

Reviews 3

History 47

  1. Michael Stopp changed subject Check if index existis in stdWrap.listNum to avoid PHP warnings Check if index exists in stdWrap.listNum to avoid PHP warnings 09/15/23, 08:33

  2. Gerrit Code Review pushed patch set 1 for main to change 81027 status New Under Review 09/15/23, 10:32

  3. Christian Kuhn changed Related to #101934 09/18/23, 14:00

  4. Gerrit Code Review pushed patch set 1 for 12.4 to change 81044 09/18/23, 14:05

  5. Gerrit Code Review pushed patch set 1 for 11.5 to change 81045 09/18/23, 14:05

  6. Georg Ringer commented and changed status Under Review Resolved done 0 % 100 % 09/18/23, 14:25

    Applied in changeset commit:90d885c7782f255028148e90509e4686685ef02f.

  7. Benni Mack changed status Resolved Closed 07/09/24, 15:19