Problem
When using Image Field Repair, any form with image field throws count() incompatibility warnings.
Warning: count(): Parameter must be an array or an object that implements Countable in Drupal\image\Plugin\Field\FieldWidget\ImageWidget::process() (line 197 of core/modules/image/src/Plugin/Field/FieldWidget/ImageWidget.php).
Warning: count(): Parameter must be an array or an object that implements Countable in Drupal\image_field_repair\Plugin\Field\FieldWidget\ImageFieldRepairWidget::process() (line 20 of modules/contrib/image_field_repair/src/Plugin/Field/FieldWidget/ImageFieldRepairWidget.php).
Possible cause
core/modules/file/src/Element/ManagedFile.php line 220.
Solution
core/modules/file/src/Element/ManagedFile.php should return empty array and NOT FALSE.
Comments
Comment #2
m.ioannidis CreditAttribution: m.ioannidis commentedComment #3
cilefen CreditAttribution: cilefen as a volunteer commentedComment #4
borisson_This makes sense, good change!
Comment #5
alexpottSpent some time trying to work out if this can and should be tested and also the likelihood of this change cause regressions for other contrib or custom code. For example if the code did
!== FALSE
. The thing is though that the code still needs to handle empty arrays because it is possible (though unlikely) that in the code$element['#files'] = !empty($fids) ? File::loadMultiple($fids) : FALSE;
theFile::loadMultiple($fids)
could return an empty array - for example - maybe if a file was deleted in another process whilst the form widget was being rendered. Therefore, I think this change is okay. Also testing this is going to be very very tricky - you'd need to extend the image field widget in the same was as https://cgit.drupalcode.org/image_field_repair/tree/src/Plugin/Field/Fie... and we've already noted that plugin implementations are not API.Therefore I think this change is safe to make and makes the code better as the type of
$element['#files']
is now consistently an array and an empty array evaluates to false when you do things likeif ($element['#files'])
. However that makes this fix only really a task as a core bug is not being fixed. Therefore this is eligible for 8.7.x only. Which also will give us time to ensure that my thoughts on regressions and other contrib and custom are correct.Comment #6
alexpottCommitted 2e71607 and pushed to 8.7.x. Thanks!
Comment #8
alexpott