Invalid token replacement when using layout builder

Created on 10 November 2020, about 4 years ago
Updated 27 January 2023, almost 2 years ago

Problem/Motivation

When using core Layout Builder, links added with Linked Field that use tokens are rendered with a bad url like https://example.com/Array.

Steps to reproduce

Enable layout builder for one of your content types the proceed with the following for the layout.

  1. Select configure from the contextual menu on one of your fields,
  2. check the box labeled "Link this field"
  3. Select the "Custom" radio button
  4. In the "Destination" field, insert a token such as [site:url]
  5. Click the "Update" button
  6. Click the "Save layout" button
  7. Clear caches
  8. View one of your nodes in the display mode that you enabled the layout for

Proposed resolution

The code block starting at line 385 of linked_field.module handles the case of a 'custom' link with the following block:

if ($destination_type == 'custom') {
  $destination = $manager->replaceToken($destination, $replace_tokens, ['clear' => TRUE]);
  // Try to grab the href attribute if the replaced token is a link.
  preg_match('/<a.* href="([^"]+)".*>/', $destination, $match);
  $destination = isset($match[1]) ? $match[1] : $destination;
}

At this point, the $destination is an array with 'field' and 'custom' elements therefore passing that to have tokens replaced. This returns an empty string. The simple fix is to check if $destination is an array and has the 'custom' element. If so, pass the $destination['custom'] element to the linked field manager's replaceToken().

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States r0nn1ef

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024