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;
} | Comment | File | Size | Author |
|---|---|---|---|
| Screenshot_4.png | 69.31 KB | fromme |
Issue fork mobile_native_share-3554278
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
Comment #2
fromme commentedComment #4
fromme commentedOpened MR, send to review
Comment #5
fromme commentedComment #6
fromme commentedPreviously, 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.
Comment #8
fromme commentedTaken to 1.1.x