MIGX getImageList PHP 8 issue?

Hi all,

I’ve been using MIGX 3.0.0-alpha5 on various sites running PHP 8.x without problems.

I think they’ve all been MODX 3.x sites.

I have an issue with a MODX 2.8.4-pl site on PHP 8.1 where, when I enable a certain TV [which uses getImageList to populate a listbox] on a template - editing a resource which uses that template gives a 500 error — in the error log:

[05-Dec-2022 12:17:05 Europe/London] PHP Fatal error:  Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /home/lcc/public_html/STAGE1/core/cache/includes/elements/modsnippet/1.include.cache.php:183
Stack trace:
#0 /home/lcc/public_html/STAGE1/core/model/modx/modscript.class.php(76): include()
#1 /home/lcc/public_html/STAGE1/core/model/modx/modparser.class.php(537): modScript->process()
#2 /home/lcc/public_html/STAGE1/core/components/pdotools/model/pdotools/pdoparser.class.php(273): modParser->processTag()
#3 /home/lcc/public_html/STAGE1/core/model/modx/modparser.class.php(251): pdoParser->processTag()
#4 /home/lcc/public_html/STAGE1/core/components/pdotools/model/pdotools/pdoparser.class.php(64): modParser->processElementTags()
#5 /home/lcc/public_html/STAGE1/core/model/modx/modchunk.class.php(116): pdoParser->processElementTags()
#6 /home/lcc/public_html/STAGE1/core/model/modx/modx.class.php(1885): modChunk->process()
#7 /home/lcc/public_html/STAGE1/core/model/modx/modtemplatevar.class.php(793): modX->getChunk()
#8 /home/lcc/public_html/STAGE1/core/model/modx/modtemplatevar.class.php(1223): modTemplateVar->processBindings()
#9 /home/lcc/public_html/STAGE1/core/model/modx/processors/element/tv/renders/mgr/input/listbox-multiple.class.php(22): modTemplateVarInputRender->getInputOptions()
#10 /home/lcc/public_html/STAGE1/core/model/modx/modtemplatevar.class.php(1155): modTemplateVarInputRenderListboxMultiple->process()
#11 /home/lcc/public_html/STAGE1/core/model/modx/modtemplatevar.class.php(1195): modTemplateVarRender->render()
#12 /home/lcc/public_html/STAGE1/core/model/modx/modtemplatevar.class.php(452): modTemplateVarInputRender->render()
#13 /home/lcc/public_html/STAGE1/core/model/modx/modtemplatevar.class.php(396): modTemplateVar->getRender()
#14 /home/lcc/public_html/STAGE1/manager/controllers/default/resource/resource.class.php(335): modTemplateVar->renderInput()
#15 /home/lcc/public_html/STAGE1/manager/controllers/default/resource/update.class.php(168): ResourceManagerController->loadTVs()
#16 /home/lcc/public_html/STAGE1/core/model/modx/modmanagercontroller.class.php(155): ResourceUpdateManagerController->process()
#17 /home/lcc/public_html/STAGE1/core/model/modx/modmanagerresponse.class.php(79): modManagerController->render()
#18 /home/lcc/public_html/STAGE1/core/model/modx/modmanagerrequest.class.php(187): modManagerResponse->outputContent()
#19 /home/lcc/public_html/STAGE1/core/model/modx/modmanagerrequest.class.php(135): modManagerRequest->prepareResponse()
#20 /home/lcc/public_html/STAGE1/manager/index.php(59): modManagerRequest->handleRequest()
#21 {main}
  thrown in /home/lcc/public_html/STAGE1/core/cache/includes/elements/modsnippet/1.include.cache.php on line 183

Line 183:

$modx->setPlaceholder($totalVar, count($items));

The same error happens on lines 197 and 218 as well.

Line 197:

$count = count($items);

Line 218:

$count = count($items);

I can work around these errors by wrapping the ($items) as (array($items)) in the getImageList snippet on those lines.

At least that seems to fix the problem - someone may have a better workaround.

I guess my questions are:

a) has anyone else come across this issue?
b) is that fix OK? and
c) can anyone confirm that this code is not PHP8 compliant? - if so I could open an issue on github.

I’m a bit confused as to why I’ve not seen this error come up before as I’ve had very similar setups in other sites - albeit they’ve been MODX3.

Many thanks as always!


The problem is that $items is null and not an array.
The right solution would be to use an empty array ([]) only if $items is null and not changing $items at all, if it is already an array.

Can you find out where in the code $items is set to null?
Or maybe provide the “getImageList” call (with all the properties) and the value of the TV so that the error can be reproduced?

Really confused now - I just went to get lunch - came back and the error no longer occurs.

Everything seems to work as it should with the standard shipped getImageList snippet.

The only thing I might have done between the two states was to comment out the [[getImageList]] call and then reverse that process.

I’d been clearing that cache regularly while troubleshooting this - but does that sound cache-related?

Thanks for your reply @halftrainedharry