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

CommentFileSizeAuthor
#2 3394691-2.patch902 bytes_utsavsharma
#2 interdiff_2.txt902 bytes_utsavsharma

Comments

raffaelj created an issue. See original summary.

_utsavsharma’s picture

Status: Active » Needs review
StatusFileSize
new902 bytes
new902 bytes

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

cilefen’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This 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.

raffaelj’s picture

If this not intended, than setting a node as home/front page in system settings should change the canonical/alternate links automatically.

raffaelj’s picture

Related 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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

drupalfan2’s picture

Patch #2 (3394691-2.patch) works for me.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

prashant.c’s picture

Facing 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

if (!empty($path) && str_starts_with($path, '//')) {
        $path = '/' . ltrim($path, '/');
      }