Problem/Motivation

I get error Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: No link template 'canonical' found for the 'paragraph' entity type in Drupal\Core\Entity\EntityBase->toUrl() (line 211 of core/lib/Drupal/Core/Entity/EntityBase.php). when add Mobile Native Share button to paragraph.

The problem caused by this line in mobile_native_share_entity_view():
$url = !empty($entity) ? $entity->toUrl('canonical', ['absolute' => TRUE])->toString() : NULL;
Not all entity types has 'canonical' url.

Proposed resolution

Wrap getting url in "try/catch". And if entity with Mobile Native Share button does not have 'canonical' url - use the current page URL, where this entity rendered.

    try {
      $url = $entity->toUrl('canonical', ['absolute' => TRUE])->toString();
    }
    catch (\Exception $e) {
      // If no link template 'canonical' found for the entity type,
      // set NULL to use the current page URL.
      $url = NULL;
    } 
CommentFileSizeAuthor
Screenshot_4.png69.31 KBfromme
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

fromme created an issue. See original summary.

fromme’s picture

Issue summary: View changes

fromme’s picture

Assigned: fromme » Unassigned
Status: Active » Needs review

Opened MR, send to review

fromme’s picture

Version: 1.0.x-dev » 1.1.x-dev
fromme’s picture

Previously, in mobile_native_share_entity_view, when an entity did not have a canonical link template, the code set $url = NULL. This caused problems in the JavaScript layer:
- navigator.share tolerates a missing url (it falls back to the current page automatically).
- But for fallback paths (navigator.clipboard and prompt), a NULL value resulted in broken behavior - users would see nothing or an invalid link (accordings updates from #3554442).
To ensure consistent behavior across all browsers, I replaced NULL with the current page URL (\Drupal::request()->getUri()). This guarantees that:
- The share button always has a valid data-url.
- Clipboard and prompt fallbacks work reliably.
- The logic is predictable and does not depend on JS having to check for NULL.

  • fromme committed 8941fc1b on 1.1.x
    Resolve #3554278 "No link template"
    
fromme’s picture

Status: Needs review » Fixed

Taken to 1.1.x

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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