Problem/Motivation
The methods FileItem::generateSampleValue() and ImageItem::generateSampleValue()
create URIs with unnecessary slashes if the file_directory setting is not configured.
For example:
I'm using devel_generate to generate nodes which have file attachments.
Drupal\file\Plugin\Field\FieldType\FileItem::generateSampleValue()
generates uris with 3 slashes when files are not configured to be in a subdirectory. so if $settings['file_directory'] is configured it's great
private://my_dir/blah.txt
otherwise
private:///blah.txt
Steps to reproduce
From the description
1. Use devel_generate
2. generate some content for something that has file attached
3. Don't set file_directory in settings.php
Proposed resolution
Suggested fix. Instead of
$destination = $dirname . '/' . $random->name(10, TRUE) . '.txt';
this
Instead of updating doGetUploadLocation() to add a trailing slash (which could break existing usage), we now add the trailing slash inline within the same function where it's needed:
FileItem
$dirname .= str_ends_with($dirname, '/') ? '' : '/';
$destination = $dirname . $random->name(10, TRUE) . '.txt';
ImageItem
destination_dir .= str_ends_with($destination_dir, '/') ? '' : '/';
$destination = $destination_dir . basename($path); Remaining tasks
Review
User interface changes
N/A
Introduced terminology
N/A
API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
Issue fork drupal-3471194
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.
Comment #5
kim.pepperComment #6
smustgrave commentedCleaned up the issue summary some.
I see test-only has already been ran https://git.drupalcode.org/issue/drupal-3471194/-/jobs/3468388
I didn't see any existing file that the test coverage could be moved to so standalone file seem fine.
LGTM
Comment #7
quietone commentedI made some suggestions in the MR for coding standards and to add some comments.
Comment #8
Anonymous (not verified) commentedakulsaxena made their first commit to this issue’s fork.
Comment #9
Anonymous (not verified) commented@quietone
I applied the suggested changes
Please take a look
Comment #10
Anonymous (not verified) commentedRan tests and the Pipeline is green
Comment #11
smustgrave commentedLeft comments on MR.
Comment #12
Anonymous (not verified) commentedHi @smustgrave
I made the suggested changes
Please give it a look.
Comment #13
Anonymous (not verified) commentedComment #14
smustgrave commentedFeedback appears to be addressed. Thanks
Comment #15
larowlanLeft some questions on the MR
Comment #17
shalini_jha commentedI have replicated the issue and reviewed the feedback on the MR, which makes sense. I have added the comments based on my findings. Additionally, I noticed that the assertion was passing every time, so I updated it and tested the change. Moving this to NR for confirmation or any further guidance.
Comment #18
shalini_jha commentedComment #19
smustgrave commentedResponded to open thread.
Comment #20
shalini_jha commentedComment #21
shalini_jha commentedThank you for the review.
I have implemented the changes in the doGetUploadLocation method and reviewed all occurrences of this method. While we already addressed the issue in the FileItem class, I found that the ImageItem class was handling file paths in a similar way appending static slash, so I have applied the same fix there as well. To ensure the changes work as expected, I have added test coverage for scenarios involving a blank file directory in the ImageItem class.
Please review the changes and let me know if any further adjustments are needed.
Comment #22
smustgrave commentedLast thing but if we are expanding scope believe title/summary need to reflect please
Comment #23
shalini_jha commentedComment #24
shalini_jha commentedComment #25
shalini_jha commentedComment #26
shalini_jha commentedComment #27
shalini_jha commentedComment #28
shalini_jha commentedThank you for the review. You are right; I have updated the issue summary and title to reflect the changes we have made so far.
Comment #29
smustgrave commentedThanks for following this one through
Comment #30
quietone commentedYes, thanks @shalini_jha,
On a second look here I found a few things, see the comment in the MR.
Comment #31
shalini_jha commentedThank you for the review @quietone, I will look into the feedback.
Comment #32
shalini_jha commentedAssigning this me for sometime , as i am working on it now to complete the feedback.
Comment #33
shalini_jha commentedI have addressed all the feedback and made some improvements. I refactored the code to create a more generic method for the test. Additionally, I updated the comments, After re-running the tests, I verified that they are working correctly for both FileItem and ImageItem, handling both empty and custom file directories.
I am moving this to NR. Kindly review and let me know if any further updates are required.
Comment #34
smustgrave commentedBelieve feedback has been addressed here.
Comment #35
longwaveSo if I understand the MR correctly this changes
doGetUploadLocation()so it now always includes a trailing slash.Unfortunately this is a public API change:
Existing callers might not expect the trailing slash, so this could break other usages of this method in contrib or custom code?
Is there another way of fixing this without changing this method's return value?
Comment #36
smustgrave commentedSounds like NW for more workshop.
Comment #37
shalini_jha commentedThanks for the feedback!
Based on your suggestion, I haven’t modified the doGetUploadLocation() method. Instead, I added a trailing slash check directly in the same function where the destination path is being built for both FileItem and ImageItem.
Could you please review and let me know if this approach works, or if further changes are needed?
I have also update the IS based on my current approach for more clarity. pipeline is green so moving this for your review.
Kindly review, Thanks
Comment #38
kim.pepperLeft some suggestions.
Comment #39
shalini_jha commentedThank you for the review and feedback. i am working on it.
Comment #40
shalini_jha commentedThank you for your feedback!
I’ve addressed all the points as suggested, and rerun the pipeline — it's now passing.
I’ve also updated the issue summary to reflect the changes made based on the MR feedback.
Please have a look and let me know if anything further needs to be adjusted. Thank you
Comment #41
shalini_jha commentedComment #42
smustgrave commentedBelieve feedback has been addressed
Comment #44
alexpottWhoops we cannot use rtrim here because of
public://- nice tests! Reverted my noise. Sorry.Comment #45
alexpottComment #46
alexpottBackporting to 11.2.x,10.6.x and 10.5.x as a bugfix.
Committed and pushed ec48de10e88 to 11.x and 0808066939c to 11.2.x and 3f31fab7c51 to 10.6.x and ff046030205 to 10.5.x. Thanks!