Add a trait to get file upload location from a field definition

Created on 26 September 2023, 9 months ago
Updated 18 May 2024, about 1 month ago

Problem/Motivation

Part of ๐Ÿ“Œ [META] Modernise file upload logic Active we identified that we are duplicating the code to find the file upload location in the following places:

  • \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadLocation()
  • \Drupal\file\Plugin\rest\resource\FileUploadResource::getUploadLocation()
  • \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::getUploadLocation()

We can't really create a base class and use inheritance for this, as they use different methods of uploading files (ie. UploadedFile vs input stream).

Steps to reproduce

Proposed resolution

Add a trait that gets the upload destination from the field definition, and deprecate the duplication.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ“Œ Task
Status

Fixed

Version

10.3 โœจ

Component
File moduleย  โ†’

Last updated 3 days ago

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @kim.pepper
  • last update 9 months ago
    30,363 pass
  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    Looks like it's going to be problematic replacing the code in method \Drupal\file\Plugin\Field\FieldType\FileItem::doGetUploadLocation() with a trait because it is static.

  • Pipeline finished with Failed
    9 months ago
    Total: 965s
    #23594
  • Status changed to Needs work 9 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    For the new trait could we get a CR

  • Pipeline finished with Success
    7 months ago
    Total: 669s
    #59098
  • Status changed to Needs review 7 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    Fixed merge conflicts and added a change record.

  • @kim.pepper Need to rebase the branch.
    Thanks!

  • Status changed to Needs work 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    For the rebase.

  • Status changed to Needs review 7 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    Merged with 11.x

  • Pipeline finished with Success
    7 months ago
    Total: 652s
    #59711
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala

    The trait seem to be functioning well for the file fields. I was considering whether it might be sensible to maintain consistency in the function names within the trait as well. For instance, changing getFileUploadLocation to getUploadLocation(). Just a suggestion.

  • Status changed to RTBC 7 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Thanks for fixing that @kim.pepper!

  • last update 7 months ago
    30,696 pass
  • last update 7 months ago
    30,697 pass
  • last update 7 months ago
    30,698 pass
  • last update 6 months ago
    30,712 pass
  • last update 6 months ago
    30,726 pass
  • last update 6 months ago
    30,766 pass
  • last update 6 months ago
    25,900 pass, 1,816 fail
  • last update 6 months ago
    25,882 pass, 1,823 fail
  • last update 6 months ago
    CI aborted
  • last update 6 months ago
    25,905 pass, 1,832 fail
  • 34:21
    29:42
    Running
  • Status changed to Needs work 6 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand

    I'm triaging RTBC issues โ†’ . I read the IS, the comments, the change record and the MR. I do like a bit of tidying up!

    The question raised in #9 has not been answered. Why the name change? And how will this effect any contrib usages of getUploadLocation?

    The change record also needs to be updated, it is referring to changes that have not been made. Also, the first sentence is difficult to read, and English is my first language. Also, instead of mentioning a usage in a contrib project it would be more helpful to explain what must be done in contrib to update modules for this change, such as a before and after example.

    Setting to needs work for the above.

  • Pipeline finished with Canceled
    6 months ago
    Total: 107s
    #69325
  • Status changed to Needs review 6 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia
    • Rebased on 11.x
    • Renamed to getUploadLocation() as per the suggestion in #9
    • Updated the IS and CR
  • Pipeline finished with Success
    6 months ago
    Total: 642s
    #69326
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    I really wanted to replace \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadLocation() with the trait but not sure how to work around deprecating a static method with a class method.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    thoughts on handling that replacement in a follow up?

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Oh cool. Then in that case rest seems fine to me.

  • Status changed to RTBC 6 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
  • last update 6 months ago
    25,909 pass, 1,828 fail
  • last update 6 months ago
    25,910 pass, 1,829 fail
  • last update 6 months ago
    25,993 pass, 1,821 fail
  • last update 6 months ago
    25,963 pass, 1,817 fail
  • Pipeline finished with Success
    5 months ago
    Total: 512s
    #77722
  • Status changed to Needs review 5 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    I thought I would have a go at replacing FileItem::doGetUploadLocation() with the trait method.

    Unfortunately, generateSampleValue() calls it and is a static method, so we have to call the trait method statically too. I added a @phpstan-ignore-next-line but I'm not sure what the alternative is?

  • Pipeline finished with Failed
    5 months ago
    Total: 486s
    #77743
  • Pipeline finished with Failed
    5 months ago
    Total: 241s
    #77749
  • Pipeline finished with Success
    5 months ago
    Total: 564s
    #77753
  • Pipeline finished with Success
    5 months ago
    Total: 470s
    #77755
  • Pipeline finished with Success
    5 months ago
    Total: 586s
    #77757
  • Pipeline finished with Failed
    5 months ago
    Total: 174s
    #78964
  • Pipeline finished with Failed
    5 months ago
    Total: 291s
    #78977
  • Status changed to Needs work 5 months ago
  • The Needs Review Queue Bot โ†’ tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    5 months ago
    Total: 4508s
    #78987
  • Pipeline finished with Failed
    5 months ago
    Total: 768s
    #79006
  • Pipeline finished with Failed
    5 months ago
    Total: 713s
    #80146
  • Pipeline finished with Success
    5 months ago
    Total: 670s
    #80511
  • Pipeline finished with Success
    5 months ago
    Total: 476s
    #80515
  • Status changed to Needs review 5 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    Updated IS

  • Status changed to RTBC 5 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Reviewed the deprecation and link to CR appears correct https://www.drupal.org/node/3406099 โ†’

    Reviewing actual CR and all the details are there.

    Believe this is good.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone New Zealand

    This is my second visit here for triage. I didn't find any unanswered questions or other work to do.

    Leaving at RTBC;

  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    I wonder if we don't have to add the service. Can we use the same approach as \Drupal\media_library\Form\FileUploadForm::createMediaFromValue and create a FileItem to do this.

    Otherwise if we're going to go with the current approach I think we should be accepting a field definition object instead of settings and validating that it is a fiie item that we're generating a location for. Otherwise it's all a bit mushing random arrays together.

  • Status changed to Needs work 4 months ago
  • The Needs Review Queue Bot โ†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • Pipeline finished with Success
    4 months ago
    Total: 570s
    #110998
  • Pipeline finished with Success
    4 months ago
    Total: 485s
    #111079
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    #23 @alexpott I'd prefer to use a service, as we depend on 'token' and that has about 5 other dependencies.

    I've changed \Drupal\file\Upload\UploadDestinationResolver::getUploadDestination() from accepting array $settings to FieldDefinitionInterface and I think that works better.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    I've looked at both this and \Drupal\file\Validation\FileValidatorSettingsTrait::getFileUploadValidators() and I think that they both should be / have been part of the file field API. If we put them on \Drupal\file\Plugin\Field\FieldType\FileFieldItemList then we'd be able to call this methods from the field definition we already have.

    I don't think our API should be decided by the fact that our field system struggles with container injection (only because it was updated before the container was truly available in the D8 cycle). Both getFileUploadValidators() and getUploadDestination() are extremely intimate to the internals of the file field type.

  • Pipeline finished with Failed
    4 months ago
    Total: 615s
    #111161
  • Status changed to Needs work 4 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    #26 hmm, not sure exactly how that would work ๐Ÿค”

  • Pipeline finished with Failed
    2 months ago
    Total: 703s
    #142314
  • Pipeline finished with Canceled
    2 months ago
    Total: 255s
    #142328
  • Pipeline finished with Failed
    2 months ago
    Total: 968s
    #142333
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    @alexpott re: #26 I added the method to\Drupal\file\Plugin\Field\FieldType\FileFieldItemList::getUploadLocation() but not sure how to access that from a $field_definition.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    @alexpott thanks for the review, but we still need to work out how to do this. See #28

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Yeah having this on the field item list doesn't work out :( I can't see how to get back to the list item either...

    $field_definition->getClass() returns \Drupal\file\Plugin\Field\FieldType\FileFieldItemList but $field_defintion is actually a FieldItemDataDefinition.

    How about not deprecating the stuff on the FileItem and use that - as suggested in #23.

  • Pipeline finished with Failed
    2 months ago
    Total: 1109s
    #147534
  • Pipeline finished with Success
    2 months ago
    Total: 1079s
    #147539
  • Status changed to Needs review 2 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    I took the approach suggested in #30 of creating a file item from the field definition. I put it in a trait so it could get used by both REST and JSON API.

    @alexpott Do we still want to postpone the deprecation to 12.x? It seems much less disruptive now.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia kim.pepper ๐Ÿ„โ€โ™‚๏ธ๐Ÿ‡ฆ๐Ÿ‡บSydney, Australia

    Updated title and IS

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Given they are both controllers I think 11.x is fine

  • Pipeline finished with Canceled
    2 months ago
    Total: 232s
    #147817
  • Pipeline finished with Success
    2 months ago
    Total: 959s
    #147826
  • Status changed to RTBC 2 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Appears feedback has been addressed.

    CR still appears fine, not sure if it was updated or mention removed in 12 before.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom alexpott ๐Ÿ‡ช๐Ÿ‡บ๐ŸŒ

    Committed and pushed 7a12710863 to 11.x and e9c7c01bc8 to 10.3.x. Thanks!

  • Status changed to Fixed 2 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024