Problem/Motivation
If you have a file field on an entity without a canonical link template, visiting file/ID/usage throws an exception.
The reason is that the EntityLabel plugin unconditionally calls toUrl().
To reproduce, install contact_storage, add a file filed to the contact form, fill out the contact form and then visit the file usage page for that file.
Proposed resolution
Check if the canonical link template exists before trying to display a link.
Other places and views plugins might be affected by this too.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
xjmComment #3
alexpottDiscussed with @xjm and @catch. We agree that this is major because if an entity don't have a canonical link template and you attach file fields a core provided view will break.
Comment #4
gaborpeter commentedComment #5
gaborpeter commentedI've been able to reproduce the bug, patch attached.
Thanks for the example module, it helped to speed up the process.
After patch screenshot attached as well.
Comment #6
mariancalinro commentedThe code looks good.
I tested on simplytest.me, and can confirm that it works.
Comment #7
joshi.rohit100Instead to get all templates first and then check, we also have hasLinkTemplate() for entity.
Comment #8
mariancalinro commentedComment #9
gaborpeter commentedYou're totally right, thanks for the tip.
Here is the tested patch.
Comment #10
mariancalinro commentedComment #11
gaborpeter commentedPatch #10 works for me, you're right getEntityType() is not needed as well.
Comment #12
berdirFix looks good but we need tests.
I assume we have existing tests for the file usage page. In that test, also enable entity_test and pick one of the entity types tere that don't have a canonical link template. create one and add a usage to a file, then make sure the page doesn't break and doesn't try to create a link for that.
Comment #14
justachris commentedAdded WebTest pretty much following the directions outlined by @berdir in #12
Interdiff is the test-only patch
Comment #16
swentel commentedI'm wondering whether we should add a positive assert as well, something like assertText($entity_name) ? I always like those more a little more. Shouldn't hold up commit though, rest looks good to me.
Comment #17
justachris commentedGood idea @swentel
Added the positive assertion for the entity name. Had to replace the test entity type used with another to allow the entity name to display.
Comment #19
mbovan commentedStumbled upon this... It looks like we need to explicitly set
make_linkto FALSE if there is no "canonical" template. Otherwise, in case of multiple usages, no-canonical entity's name is linked with a URL from a canonical-entity.Uploaded a patch with 2 usages (canonical/non-canonical) as test-only example.
Comment #21
mbovan commentedCorrect patch.
Comment #22
justachris commented@mbovan Good catch on output with multiple usages on the file. Can you upload an updated test-only patch to show a test failure in that scenario?
Setting to Needs Work since I think that updating the tests to show that failure is relevant to the issue.
Comment #23
mbovan commentedActually, I got an exception while working on a client project where the fix is a patch from #21.
On a clear 8.1.x branch, I cannot reproduce it... Also, seems like node usage #19 patch is not failing on testbot, so I'm removing my patches and pointing to #17 as the latest valid patch, for now...
Comment #24
dawehnerThis solves the problem, seemed to be quite reasonable.
Comment #25
alexpottIs #2402533: Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation an alternate solution?
Comment #26
alexpottAlso if we #2402533: Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation then this issue is fixed for files - but not for other entities without a canonical template.
Comment #27
joshi.rohit100Yes thats right. If I remember correctly, I faced this issue in paragraph module as well as there was no canonical for that.
Comment #28
alexpottI don't think we should be checking for a link template here. I think we need to be calling $entity->toUrl() and catching UndefinedLinkTemplateException and EntityMalformedException and in this instance setting
make_linkto FALSE.This is because it is possible to provide URLs without link templates - see #2402533: Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation and also the
uri_callbackannotation.Comment #29
chr.fritschI adjusted the patch with alex hints
Comment #30
mtodor commentedPatch looks good.
Comment #31
cyberschorschLooks good for me as well.
Marking as RTBC
Comment #32
xjmOne small thing we could clean up here is that this does not follow our coding standards for control structures -- see: https://www.drupal.org/coding-standards#controlstruct
This could be fixed on commit (so leaving RTBC), but also would be easy to clean up in a patch. :)
Comment #33
chr.fritschJust fixing the coding style issues
Comment #34
alexpottThis comment is not quite right - it is if Entity::toUrl throws an exception.
This test will break once #2402533: Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation lands... which indicates we should have a test that is not tied to file entities. Maybe using the entity_test entity and not having a canonical link.
Comment #35
chr.fritsch1. Fixed
2. We discussed that with alexpott and came to the point that #2402533: Provide File::createFileUrl() as a replacement for the deprecated File:url() implementation will not break the test, because the test covers the relation from a file to an entity. Not the other way round
So IMHO we can move this fast to RTBC
Comment #36
alexpott@chr.fritsch yep #34.2 was wrong - sorry everyone.
Comment #37
cyberschorschThen we are done here :)
Comment #38
alexpottCommitted bbcd3bd and pushed to 8.1.x and 8.2.x. Thanks!
Fixed coding standards on commit.
Comment #42
gábor hojtsyComment #43
gábor hojtsyFix for use of current tag. Sorry for using the wrong one.
Comment #44
rosinegrean commented