Problem/Motivation
Inside \Drupal\image\Plugin\Field\FieldType\ImageItem::generateSampleValue() we currently have:
(...)
$destination_dir = static::doGetUploadLocation($settings);
file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
$destination = $destination_dir . '/' . basename($path);
$file = file_move($image, $destination, FILE_CREATE_DIRECTORY);
(...)
However, the constants accepted for file_move() are:
* @param int $replace
* (optional) The replace behavior when the destination file already exists.
* Possible values include:
* - FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with
* the destination name exists then its database entry will be updated and
* $source->delete() called after invoking hook_file_move(). If no database
* entry is found, then the source files record will be updated.
* - FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the
* filename is unique.
* - FILE_EXISTS_ERROR: Do nothing and return FALSE.
Proposed resolution
Fix the wrong constant.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
marcoscanoComment #3
claudiu.cristeaGood catch, but why FILE_EXISTS_REPLACE? I think we don't want to replace an (unlikely) existing file. Let's go with the default, which is FILE_EXISTS_RENAME.
Also, I see the same bug few lines above:
Let's fix that too.
Comment #4
marcoscanoCurrent constant values are:
So by (wrongly) using
FILE_CREATE_DIRECTORY, we are currently already usingFILE_EXISTS_REPLACE(value: 1). I was assuming the safest was not to change the actual behavior of the code, but it's true that renaming makes more sense than replacing here.Thanks!
Comment #5
claudiu.cristeaThank you. As FILE_EXISTS_RENAME is the default value for that parameter, we don't need to pass it explicitly, right? :)
Comment #6
marcoscano🤦
Comment #7
claudiu.cristeaRTBC if turns green.
Comment #9
claudiu.cristeaAPCU failure.
Comment #10
larowlanAdding review credits for @claudiu.cristea
Comment #12
larowlanCommitted 6585630 and pushed to 8.5.x.
This can be backported to 8.4.x but doesn't apply
Comment #13
larowlanComment #14
marcoscanoRe-rolled against 8.4.x
Comment #15
claudiu.cristeaBack to RTBC for 8.4.x
Comment #17
xjmCommitted the backport to 8.4.x as well. Thanks!
Comment #19
xjmSorry, I spaced out and forgot that we're past the last normal patch release of 8.4.x, so marking this fixed against 8.5.x instead. :)