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
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
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 3394691-2.patch | 902 bytes | _utsavsharma |
| #2 | interdiff_2.txt | 902 bytes | _utsavsharma |
Comments
Comment #2
_utsavsharma commentedAdded `if (!$path) $path = '';` in `core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php`.
Comment #3
cilefen commentedThis seems like something the path module was not intended to do and it should perhaps reject a single slash on validation.
Nevertheless for this to be considered we need to have test coverage of this proposed behavior change.
Comment #4
raffaeljIf this not intended, than setting a node as home/front page in system settings should change the canonical/alternate links automatically.
Comment #5
raffaeljRelated issues:
* url() should return / when asked for the URL of the frontpage
* Unable to save '/' root path alias (duplicate)
* Front Page canonical is Incorrect (duplicate)
This issue still persists, because PHP8 deprecated null values in string functions, which will become errors instead of notices sooner or later.
To "fix" my initial motivation to set the path alias of my front page to
/, I might install the Metatag module. I'm not happy with adding 1,9 MiB/522 files/231 sub-folders just to set the correct canonical and shortlink tags, but it might be a workaround to get started.Comment #6
andypostComment #8
drupalfan2 commentedPatch #2 (3394691-2.patch) works for me.
Comment #10
prashant.cFacing the same issue on Drupal 11.2.12, I think a patch/MR is needed with a simple check
Line 54 in
docroot/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php