setting path alias to `/` causes PHP8.1/str_starts_with/null error

Created on 17 October 2023, 8 months ago
Updated 30 October 2023, 8 months ago

Problem/Motivation

Setting an url alias to `/` causes a deprecated notice in PHP 8.1. Internally it seems to be converted to an empty string or null.

```
Deprecated function: str_starts_with(): Passing null to parameter #1 ($haystack) of type string is deprecated in Drupal\path_alias\PathProcessor\AliasPathProcessor->processOutbound() (line 54 of core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php).
```

Motivation to set url alias to a single slash is to generate proper canonical and alternate links, e. g.:

```html

<link rel="canonical" href="http://localhost:8000/node/1" />
<link rel="shortlink" href="http://localhost:8000/node/1" />

```

should become:

```html

<link rel="canonical" href="http://localhost:8000" />
<link rel="shortlink" href="http://localhost:8000/node/1" />

```

Or in a multilingual setup:

```html

<link rel="shortlink" href="http://localhost:8000/de" />
<link rel="canonical" href="http://localhost:8000/de" />
<link rel="alternate" hreflang="de" href="http://localhost:8000/de" />
<link rel="alternate" hreflang="en" href="http://localhost:8000/en" />

```

Related issue: https://www.drupal.org/project/drupal/issues/3308707 🐛 Deprecated function - Files: UrlGenerator.php and AliasPathProcessor Postponed: needs info

Steps to reproduce

Drupal: 10.1.5
PHP: 8.1

```bash
mkdir test
cd test
composer create-project drupal/recommended-project .
composer require drush/drush
```

* create `README.md`, `docker-compose.yml`, `.env` manually
* `docker-compose up -d`, `docker exec -it drupal bash`
* `drush site:install`, type drupal config
* add `$config['system.logging']['error_level'] = 'verbose';` to `web/sites/default/settings.php`
* open browser, login
* create basic page, set url alias to `/`, save page
* error message pops up on all page loads

Proposed resolution

Add `if (!$path) $path = '';` in `core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php` after line 47

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

10.1

Component
Path 

Last updated 6 days ago

  • Maintained by
  • 🇬🇧United Kingdom @catch
Created by

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024