Problem/Motivation
\Drupal\image\Plugin\Field\FieldType\ImageItem::generateSampleValue
is having a mechanism to prevent generation of too many sample files. Creates up to 5 in a single request using the static local variable $images
.
However, after the 5 generated files, it does not anymore check the validity of the files when trying to pick one of the available ones.
This might lead in the following error:
getimagesize(public://...): Failed to open stream: "Drupal\Core\StreamWrapper\PublicStream::stream_open" call failed
This is a rare occasion of course, because the chances of another process or function or hook to delete the files in the same request is minimal, however, there are other scenarios that might lead to this.
For example, in our project, we are using behat to run some tests. Then, in an afterScenario scope, we clean up the created entities. Behat runs the entire feature (group of tests) in a single request, so in this case, some other mechanism is deleting the file entities mid-request.
Proposed resolution
Check if the files exist before trying to appoint one of the created ones.
Remaining tasks
None.