Problem/Motivation

Issue reproduces after updating to the module version(8.x-1.14) and Drupal version(8.8.5).

Step to reproduce:

Create node with paragraph that contains 'add to any' field.

After saving the node you'll get this error:
Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: No link template 'canonical' found for the 'paragraph' entity type in Drupal\Core\Entity\EntityBase->toUrl() (line 227 of core/lib/Drupal/Core/Entity/EntityBase.php).

Comments

Haddaway created an issue. See original summary.

Haddaway’s picture

StatusFileSize
new928 bytes

In my case problem here:

function addtoany_create_entity_data($node, $config = NULL) {
  $url = isset($node) ? $node->toUrl('canonical', ['absolute' => TRUE])->toString() : NULL;
  $title = isset($node) ? $node->label() : NULL;
  return addtoany_create_data($url, $title, $config);
}

The $node parameter is an instance of Paragraph so we need to go to the host entity.

Here's a quick fix use it at your own risk.

Haddaway’s picture

StatusFileSize
new627 bytes

Avoids paragraphs module dependencies.

avpaderno’s picture

Version: 8.x-1.14 » 8.x-1.x-dev
Status: Active » Needs review
omarlopesino’s picture

StatusFileSize
new1.39 KB

Updating the patch to cover this use cases:

  • There isn't a node in the root parent.
  • We don't get a node from parents.
samar.alazzeh’s picture

#5 worked fine, Thank you

harshkundaliya’s picture

Status: Needs review » Reviewed & tested by the community

#5 reviewed and tested on my system. Works fine. Marking it as RTBC

nikolaat’s picture

StatusFileSize
new1.47 KB

Extend #5 with check for node->id because, toUrl method is throwing error if node is not created and doesn't have id. Case scenario when user want to preview node.

Syntapse’s picture

I saw this same error adding an image to a accordion section. Im just adding <img/> element to text body as a workaround.

kumar rakesh’s picture

same error i'm getting in drupal 9.3 version.
i am using paragraph module , i have created an view of paragraph and using fields image and text when i try to link image with content then the module has throw an exception that is ....Below -

Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: No link template 'canonical' found for the 'paragraph' entity type in Drupal\Core\Entity\EntityBase->toUrl()

issue described by me above is related to paragraph module. if any patch implemented for this issue please tell about that.

Thank You.

JennBina’s picture

This is a newbie question, but where do we insert the #5 patch?

eugen zerr’s picture

@JennBina you'll need a composer based project.
See: https://devdocs.magento.com/guides/v2.4/comp-mgr/patching/composer.html

Bmuskan’s picture

StatusFileSize
new17.24 KB

Replace the code of addtoany.module file below code :

use Drupal\node\NodeInterface;
 
 /**
  * Implements hook_theme().
@@ -192,8 +193,12 @@ function addtoany_page_attachments(&$page) {
  *   The array with url, title, additional_html that will be send to Twig.
  */
 function addtoany_create_entity_data($node, $config = NULL) {
  while ($node instanceof EntityInterface && $node->getEntityTypeId() == 'paragraph') {
    $node = $node->getParentEntity();
  }

  $url = $node instanceof NodeInterface ? $node->toUrl('canonical', ['absolute' => TRUE])->toString() : NULL;
 $title = $node instanceof NodeInterface ? $node->label() : NULL;
   return addtoany_create_data($url, $title, $config);
 }
dpi’s picture

Status: Reviewed & tested by the community » Needs work

Theres something critically wrong with the patch. How is it the case that this statement is ever true?

while ($node instanceof EntityInterface && $node->getEntityTypeId() == 'paragraph') {

$node should only ever be a NodeInterface.

Unless addtoany_create_entity_data is truely supposed to be able to handle any entity type, then the entire function signature and documentation needs to change to take any arbitrary entity type.

So function addtoany_create_entity_data($node, $config = NULL) { changes to function addtoany_create_entity_data(ContentEntityInterface $entity, $config = NULL) { After which the entire function should be guarded/early-return with a link template check:

if (!$entity instanceof ContentEntityInterface || !$entity->hasLinkTemplate('canonical') {
  return;
}
ghuygens’s picture

StatusFileSize
new1.54 KB
avpaderno’s picture

Status: Needs work » Needs review
jnettik’s picture

StatusFileSize
new1.65 KB

I've updated the patch in #15. Mostly by adding some type casting to the $entity object that gets passed in and adding some commenting around the changes. That should simplify some of the conditional logic happening in the function.

martin nikolov’s picture

The paragraphs can be nested inside another paragraphs. Here is patch version that handle this case as well.
https://www.drupal.org/files/issues/2023-06-06/addtoany-no-link-template...

  • micropat committed 7f1459d6 on 8.x-1.x
    Issue #3138220 by Haddaway, NikolaAt, omarlopesino, Martin Nikolov,...

  • micropat committed ed245c84 on 2.0.x
    Issue #3138220 by Haddaway, NikolaAt, omarlopesino, Martin Nikolov,...
micropat’s picture

Status: Needs review » Fixed

Fixes in the latest releases, including the 2.0.x branch.

Status: Fixed » Closed (fixed)

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