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.

Comments

taitai created an issue. See original summary.

socketwench’s picture

I can replicate this issue locally.

  1. Download, enable paragraphs
  2. Create a new paragraph type with at least one field
  3. Attach a paragraph field to an existing node type (like article)
  4. Create a Paragraph Flag.
  5. Attempt to use the flag link on the paragraph.
joachim’s picture

Status: Active » Postponed (maintainer needs more info)

Can 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.

socketwench’s picture

Status: Postponed (maintainer needs more info) » Active
StatusFileSize
new1.05 KB
new1.05 KB

The 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.

socketwench’s picture

socketwench’s picture

Maybe we can fall back to the HTTP_REFERRER? Especially if that falls within the Drupal site path:

https://gist.github.com/morsok/5db2fb9dfb09757b9fdeb36ed5ad2540

joachim’s picture

      // Redirect back to the entity. A passed in destination query parameter
      // will automatically override this.

That'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?

berdir’s picture

The 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.

joachim’s picture

> 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?

berdir’s picture

It does, but the controller still generates the fallback redirect response, enforcing the ?destination is done automatically by core.

joachim’s picture

Ah I get it now. Thanks!

But:

      // Redirect back to the entity. A passed in destination query parameter
      // will automatically override this.
      $url_info = $entity->toUrl();
      return $this->redirect($url_info->getRouteName(), $url_info->getRouteParameters());

if the flag links, which we output, always contain a destination query, then why do we need this at all?

berdir’s picture

There 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

taitai’s picture

Just to clarify. The paragraphs are displayed in a view. Views reference the paragraphs directly.

joachim’s picture

Status: Active » Needs work
  1. +++ b/src/Controller/ActionLinkController.php
    @@ -13,6 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
    +use Zend\Stdlib\Exception\LogicException;
    

    That doesn't look right...

  2. +++ b/src/Controller/ActionLinkController.php
    @@ -150,12 +151,15 @@ class ActionLinkController extends ControllerBase implements ContainerInjectionI
    +    elseif ($entity->hasLinkTemplate('canonical')){
    

    Should be a space before the {

  3. +++ b/src/Controller/ActionLinkController.php
    @@ -150,12 +151,15 @@ class ActionLinkController extends ControllerBase implements ContainerInjectionI
           // Redirect back to the entity. A passed in destination query parameter
           // will automatically override this.
    

    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?

  4. +++ b/src/Controller/ActionLinkController.php
    @@ -150,12 +151,15 @@ class ActionLinkController extends ControllerBase implements ContainerInjectionI
    +      throw new \LogicException('IDK!');
    

    Let's redirect to the front page.

joachim’s picture

   * Every entity type should, at minimum, define "canonical", which is the
   * pattern for URIs to that entity. Even if the entity will have no HTML page
   * exposed to users it should still have a canonical URI in order to be
   * compatible with web services. Entities that will be user-editable via an
   * HTML page must also define an "edit-form" relationship.

Hmmm... sounds like either Paragraphs is wrong, or that core documentation needs a rethink.

berdir’s picture

I 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.

socketwench’s picture

Status: Needs work » Needs review
StatusFileSize
new1.37 KB

Removed bizarre use statement.
Added space.
Added redirect to front page if all else fails.

taitai’s picture

Patch works. No further exceptions.

  • socketwench committed f57fa8d on 8.x-4.x
    Issue #2873567 by socketwench: Turning on a flag for a paragraph results...
socketwench’s picture

Status: Needs review » Fixed

Patch works. No further exceptions.

I'll consider that an RTBC. ^_^

And committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.