I have created a flag for a paragraph. Left all defaults when defined.
Flog shows "Flag this item" when displaying the node and the included paragraph.
When clicking on "Flag this item" I get the following PHP exception:
Uncaught PHP Exception Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: "No link template 'canonical' found for the 'paragraph' entity type" at /Applications/MAMP/htdocs/core/lib/Drupal/Core/Entity/Entity.php line 214
Can not use flags with paragraphs.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | flagParagraph.2873567.17.patch | 1.37 KB | socketwench |
| #4 | flagParagraph.2873567.3.patch | 1.05 KB | socketwench |
Comments
Comment #2
socketwench commentedI can replicate this issue locally.
Comment #3
joachim commentedCan someone get a backtrace?
My first thought at reading this was that we're doing something stupid in our link plugins like getting the flaggable entity's canonical URI, but I don't see anything like that in the code.
Comment #4
socketwench commentedThe problem appears to be in ActionLinkController::generateResponse(). If the entity does not provide a 'canonical' link template -- the default link template to provide -- an exception is generated in the Entity class.
For paragraphs, a canonical link template is difficult as it's not really meant to be referenced individually. Instead, it should go to the owning entity. I don't want to add a specific exception for the paragraph module, but we should figure out some sort of graceful fallback.
I don't know what that fallback should be yet, but this patch at least shows where it should go.
Comment #5
socketwench commentedComment #6
socketwench commentedMaybe we can fall back to the HTTP_REFERRER? Especially if that falls within the Drupal site path:
https://gist.github.com/morsok/5db2fb9dfb09757b9fdeb36ed5ad2540
Comment #7
joachim commentedThat's the mistake. Why are we assuming we were on the entity in the first place? We might be in a View, or looking at an entity that's being output in a reference field (such as a Paragraph, or the compact user view on a node).
There might already be an issue for this problem in the case of using a flag link in a View.
BTW...
> For paragraphs, a canonical link template is difficult as it's not really meant to be referenced individually. Instead, it should go to the owning entity.
It could be argued that Paragraphs module should override toUrl() to provide the owning entity -- anyone care to file an issue over there?
Comment #8
berdirThe redirect behavior was fixed a long time ago, the canonical link is a fallback, if there is a destination it uses that and the flag link by default includes the destination back to the current page.
We've been discusssing this in paragraphs before. But even if we would override toUrl(), there's no way to detect that, and not having a canonical link is definitely allowed. I'd just fall back to redirecting to the frontpage, as i said, flag links should always have a destination anyway.
Comment #9
joachim commented> the flag link by default includes the destination back to the current page.
So how come the link on the Paragraph entity doesn't have the destination set?
Comment #10
berdirIt does, but the controller still generates the fallback redirect response, enforcing the ?destination is done automatically by core.
Comment #11
joachim commentedAh I get it now. Thanks!
But:
if the flag links, which we output, always contain a destination query, then why do we need this at all?
Comment #12
berdirThere might be cases where there is none, if somoene uses a different way to build the link or on purpose doesn't want it. as I said, I think it is fine to just redirect to as fallback for the fallback. If someone displays that link without destination for paragraphs then its his fault
Comment #13
taitai commentedJust to clarify. The paragraphs are displayed in a view. Views reference the paragraphs directly.
Comment #14
joachim commentedThat doesn't look right...
Should be a space before the {
While we're at it, let's rewrite this to explain more clearly what is going on and why we're doing it:
Our flag link should have a destination query parameter on it, which will override what we do here. Which we do just in case wacky people create a flag link without a destination?
Let's redirect to the front page.
Comment #15
joachim commentedHmmm... sounds like either Paragraphs is wrong, or that core documentation needs a rethink.
Comment #16
berdirI think that documentation is new and paragraphs doesn't need to be compatible with web services in that way, at least not anymore soon. #2848878: Embed Paragraph Content in Normalized Parent Entities (REST).
But yes, it's a bit tricky.
Comment #17
socketwench commentedRemoved bizarre use statement.
Added space.
Added redirect to front page if all else fails.
Comment #18
taitai commentedPatch works. No further exceptions.
Comment #20
socketwench commentedI'll consider that an RTBC. ^_^
And committed.