Problem/Motivation
On #2393339: [META] Make sure Views base fields are using Field API for formatting, and do not lose functionality, we are updating all base entity fields in entity views data so that they are using Field API for formatting rather than using generic Views handlers.
This issue is about various custom handlers from the File module:
- 'file', used in file_managed.fid, file_managed.filename
- 'file_uri', used in file_managed.uri
- 'file_filemime', used in file_managed.filemime
- 'file_extension', used in file_managed.extension
- 'file_size', used in file_managed.filesize
- 'file_status', used in file_managed.status
Proposed resolution
Change these fields to use the Field API formatter 'field' instead of the custom formatters. Should also be able to remove the custom formatters from the code base completely.
If there is missing functionality in the Field API formatters, file separate issue(s) to add that back in (especially if it would delay this patch), and add them to the meta-parent issue: #2393339: [META] Make sure Views base fields are using Field API for formatting, and do not lose functionality
Remaining tasks
Make a patch.
User interface changes
None.
API changes
Not really.
| Comment | File | Size | Author |
|---|---|---|---|
| #28 | interdiff.txt | 482 bytes | rteijeiro |
| #28 | 2456709-28.patch | 40.19 KB | rteijeiro |
| #26 | interdiff.txt | 1.64 KB | dawehner |
| #26 | 2456709-26.patch | 40.18 KB | dawehner |
| #25 | interdiff.txt | 1.28 KB | dawehner |
Comments
Comment #1
cosmicdreams commentedThis sounds very interesting, and a lot of work. Is #2456713: Custom taxonomy field views handler needs to be replaced with generic Field API handler a good example of what needs to be done for File to get it into compliance?
Comment #2
dawehnerI'll give it a try.
Comment #3
dawehnerLet's see, how much fails.
Comment #4
jibranI think we need @yched input here.
Comment #5
jibranWe also need tests for all the new formatters.
Comment #7
dawehner@jibran
Sure, no question.
Here is some work on config schema first.
Comment #9
dawehnerFixing just the test for now.
Comment #11
amateescu commentedThe name of this class is a bit unfortunate, I don't think it's tied only to base fields, is it?
Wouldn't it be easier to just provide something like "FileInformationFormatter" with settings to display all of these things?
I think the test are failing because of this test class with no
test*()methods.Comment #12
dawehnerThank you for your review!!
Did you had a look at the code and see what they actually provide, its so dramatically different.
Yeah right, I was not able to motivate to write some, if we aren't even sure whether we want the formatter structure like that.
Comment #13
dawehnerAlright, we have tests now.
Comment #14
jhodgdonTook a look at the latest patch... some thoughts:
a) It's modifying core/modules/file/config/optional/views.view.files.yml -- not sure how well the pages/blocks that view handles are tested, so we should probably look at them in a manual test before marking this patch RTBC. Didn't do that yet...
b) Similarly, we should probably manually test the code changes by making a File-based view with the patch, manually, and make sure the fields all work the way we'd expect, in the Add dialogs, Settings, and output. Didn't do that yet...
c) core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php -- class has no class doc block.
d) In the settings form on that class:
Can we fix the field title/description? These don't make much sense to me. How about:
- Link this field to the file download URL
- I think get rid of the description. Our UI text says only to put in a description if absolutely necessary, and I don't think this adds anything comprehensible/useful to the title? If it does it needs to be reworded because I have no idea what it means: what links are being overridden?
e) In the viewElements on that same class:
Um... is this right? $items is an array of field items, right? So ... how is there only one entity URL? You have a multi-valued field and you're making the same link for all of them??!? That cannot possibly be right?
f)
This is not an OK class description... What is this? "render a field to its file itself"?!? No idea what it is supposed to be. Also needs to end in .
g)
Oh, so it's actually the "File link" formatter? Does it need a better name? Anyway if the label here is correct, probably the class docs should say:
Provides a formatter for a text field on a file entity that links the field to the file.
If that's too long, take out the "Provides a" part.
h) Settings form in FileExtensionFormatter:
I don't think this is clear... Let's change this to:
- Include tar in extension
- If the part of the filename just before the extension is '.tar', include this in the extension output.
i) Code in FileExtensionFormatter... kind of messy and unclear, why not use pathinfo() PHP function instead of custom regular expressions? Also the code comment "If there is an extension." is not a sentence.
j) FileSize formatter -- class description line needs to end in . and I think it would be better as something more like:
"Formatter for the filesize field on the File entity." Come to think of it, back to FileExtensionFormatter, maybe it should be "Formatter that shows the extension on the filename field for a File entity." Similar for the other formatters.
k) Shouldn't all these formatters do a check in isApplicable() to make sure the fields are on a File entity?
l) File URI formatter:
Needs to be "File URI".
m) Same class, settingsForm is missing the inheritdoc block
n) In that class again:
What does that description mean? Would anyone not a Drupal developer have any idea what the "internal filestream address" is?
o) On the Mime type formatter:
That is kind of long, maybe split into title/description?
p) Same formatter:
Hm. So if you set up to use the mime image, you are deriving that image from the entire File entity, not the mime type field? That's interesting...
q) core/modules/file/src/Tests/Formatter/FileEntityFormatterTest.php - most methods lack docblocks
Whew!
Comment #15
dawehnerThank you for your intensive review!
Made it to exactly 80 chars.
Sure ... to be clear, I just copied them over from where, where especially the description might make more sense in the first place.
That is absolutely an improvement.
... Field formatters are always written against FieldItemList ... as this is what $entity->bar always contains, no matter whether its a base field or its a configured field, with actually multiple entries.
Please have a look at any other formatter in core. ... $items is always coming from one $entity.
Well, its all existing code ... too bad
Alright
Well, I guess we have to ... Its just a bit sad that we can't make the formatters easily reusable.
Well, I just removed it, we seem to not want to explain users details, too bad.
Did that, thank you.
Well, it should be derived though from the filemime at the end ...
Right, because I think adding docblocks here would not add value, but rather discourage people from finding good test method names ...
Seriously, use dreditor, it makes life so much easier ...
Comment #17
dawehnerSo we do have test coverage for that view, yeah!
Comment #18
effulgentsia commentedPer #2393339-57: [META] Make sure Views base fields are using Field API for formatting, and do not lose functionality, tagging for critical office hours, but if there's a reason to not have this particular child issue in that list, please untag it.
Comment #19
larowlanThis is close too, just some minor cleanup and some questions around optimisations
This method can go?
Is it worth moving the first half of this to the parent method? Seems to be common to all of the child classes. Then these lines would be:
c/p error - should say something about file size
Seems that rendering image is optional? Perhaps
Formatter to render the file mime type, with an optional icon?Any reason for the two file->save() calls (here and in all cases)
Missing docblocks?
Comment #20
jibranNW cuz of #19
Comment #21
dawehnerWell, if you think its really easier to read :)
impatience
Meh
Comment #22
larowlanManual testing - ended up with an Exception - see screenshot.

Also noticed that the boolean formatter doesn't seem to have #states support, although could be pre-existing issue there.

Attached is my test view from which the exception was thrown - created the view, added an article with a file attached.
Comment #23
dawehnerThank you for your manual test! I think we should
Long story short.
Fatal error, due to passing "0" as $settings. This is caused by the code in
core/lib/Drupal/Core/Field/FormatterPluginManager.php:161doing basically array_intersect_key($settings, NULL) + NULL === NULL. This is caused by a missing return value in
\Drupal\file\Plugin\Field\FieldFormatter\FileUriFormatter::defaultSettingsAnother problem was that the wrong field type was supported.
Let's expand the test coverage.
Yeah, that is not a problem of that issue, but I was sure #1985406: #states not supported for elements in formatter settings being displayed on Views field handler form had fixed that.
Comment #25
dawehnerAlso tests has to be written in the right ways.
Comment #26
dawehnerAdded the access test coverage
Comment #27
larowlanThanks
Comment #28
rteijeiro commentedJust fixed a nitpick.
Comment #29
alexpottThis issue addresses a critical bug and is allowed per https://www.drupal.org/core/beta-changes. Committed d36e3de and pushed to 8.0.x. Thanks!
Comment #30
alexpottThis issue addresses a critical bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 7f90ee6 and pushed to 8.0.x. Thanks!
Fixed some unused uses on commit.
Comment #33
amateescu commentedOpened a small follow-up to remove some forgotten entries in the file module's config schema: #2479607: Remove obsolete schema entries from file.views.schema.yml