Wrap $item->getUrl()->access() in a catch, like the LinkWidget in core does

Created on 8 July 2025, 5 days ago

Problem/motivation

We ran into a problem when a link field was being rendered with ajax, such as a content type having a multi value paragraph field containing a link field. The site also needs a role where the permission "Link to any page" from the core system module is NOT set.

Steps to reproduce

  1. Install Drupal with Linkit 7.x and Paragraphs 1.19
  2. Create a user with a role that doesn't have the permission "Link to any page" set.
  3. Create a new paragraph type Test containing a Link field
  4. In the paragraph form display for test, set the Link field to use the Linkit widget, you can just use the default settings
  5. Create a content type Test with only one field; a paragraph field and allow the field to contain Test paragraphs
  6. Add a Test node
  7. Enter "hello broken world" or another link in the Link field that causes validation to fail
  8. Click "Add Link"

Result

The steps above causes Drupal to crash with a 500 error, returning invalid code back to the AJAX form request. Further editing in the form might also be broken.

Proposed resolution

The issue is caused by getUrl in $item->getUrl()->access() not returning a valid URL and then the access for that object failing to get the access, since getUrl throws an InvalidArgumentException when it reaches the fromInternalUri function.

In the core LinkWidget.php class, they have resolved the issue by wrapping $item->getUrl()->access() in a try, catching the InvalidArgumentException and handling it gracefully.

The code in LinkWidget.php assigns a $display_uri, but I don't think we need to copy that part, as LinkitWidget.php doesn't seem to have the same assigning that LinkWidget.php has.

πŸ› Bug report
Status

Active

Version

7.0

Component

Code

Created by

πŸ‡³πŸ‡΄Norway svenryen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024