Score:0

Gracefully handle URL/URI/Link exceptions when using Link field formatter?

in flag

Occasionally I am encountering an error along these lines:

The website encountered an unexpected error. Please try again later.InvalidArgumentException: The URI 'XYZ' is invalid. You must use a valid URI scheme. in Drupal\Core\Url::fromUri() (line 290 of core/lib/Drupal/Core/Url.php).

We are migrating data frequently and one thing that can happen is link field URIs can come in like that. The core LinkFormatter class does not seem to handle exceptions, although there is this issue:

https://www.drupal.org/project/drupal/issues/2745179

If this patch does not apply/work, what other resolution is there? Should I point the LinkFormatter class definition to a custom class that catches exceptions?

Jaypan avatar
de flag
I'd add a check to ensure the URLs are valid before importing them. But if you want a proper answer, you'll need to provide a lot more detail, as there isn't enough there to give any kind of meaningful answer.
Score:1
in flag

Turns out that the patch for this core LinkFormatter class (#50) works against Drupal 9.2.6 currently.

There are a few ways this data can get into the application outside of user-entry in Drupal forms where we cannot always validate that a link URI is good or whats intended from external API sources. This patch catches exceptions and allows the site to function normally for the public.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.