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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

m.ioannidis created an issue. See original summary.

m.ioannidis’s picture

Issue tags: -PHP7.2 8.5.x file.module Image field repair +PHP 7.2 (duplicate), +8.5.x, +file.module, +Image field repair
cilefen’s picture

Title: PHP7.2 compatibility issue with ManagedFile and Image Field Repair module » [PHP 7.2] count() called on an uncountable in ManagedFile
Version: 8.5.4 » 8.5.x-dev
Status: Active » Needs review
Issue tags: -8.5.x, -file.module
borisson_’s picture

Version: 8.5.x-dev » 8.7.x-dev
Status: Needs review » Reviewed & tested by the community

This makes sense, good change!

alexpott’s picture

Title: [PHP 7.2] count() called on an uncountable in ManagedFile » Make $element['#files'] always an array in ManagedFile
Category: Bug report » Task
Status: Reviewed & tested by the community » Needs review

Spent 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; the File::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 like if ($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.

alexpott’s picture

Committed 2e71607 and pushed to 8.7.x. Thanks!

  • alexpott committed 2e71607 on 8.7.x
    Issue #2981853 by m.ioannidis: Make $element['#files'] always an array...
alexpott’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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