Double slash when generating the URL

Created on 19 June 2024, 5 months ago

I have a View Mode Page configured for the "teaser" view mode of a node with a pattern of /teaser/%. I create a node with a URL alias of /test/page. The teaser view mode is accessible via /teaser/test/page. But when I generate the URL, it results in a double slash /teaser//test/page.

$url = new Url(
  'view_mode_page.display_entity',
  [
    'view_mode' => 'teaser',
    'entity_type' => 'node',
    'entity_id' => $node_id,
  ]
);
echo $url->toString(); // results in /teaser//test/page

It seems like a potential issue in \Drupal\view_mode_page\PathProcessor\DynamicPathProcessor::processOutbound where this is called:

$path = str_replace('%', $url_alias, $pattern->getPattern());

When our URL alias = /test/page and our pattern = /teaser/%, then the string replace of % with /test/page would result in /teaser//test/page. Since all aliases begin with a leading slash, should we instead string replace "/%" to account for this?

$path = str_replace('/%', $url_alias, $pattern->getPattern());

This works for my specific use-case? The only scenario it doesn't work for is if your % is not immediately preceded by a slash, but I don't think these paths resolve properly anyway. I'm not sure why the error doesn't present itself with patterns where the % appears at the beginning of the path, like /%/teaser but my fix doesn't break those either.

πŸ› Bug report
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States joncjordan Colorado Springs, CO

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

Comments & Activities

Production build 0.71.5 2024