Problem/Motivation
On Drupal 8.5 it is possible for the Google Tag Manager module to throw an uncaught exception on the home page.
Steps to Reproduce
On a Drupal 8.5 website, with Google Tag Manager in use, we have been able to reliably reproduce this error by un-publishing the homepage, then viewing it as an unauthenticated visitor.
- Log in to Drupal as an author or administrator
- Go to the homepage of the website
- Edit the homepage (node)
- Set the homepage (node) to Unpublished and Save
- Log out of Drupal
- Navigate back to the homepage
Error Log
The following stack trace is produced that shows Google Tag Manager as the source of the error.
Uncaught PHP Exception InvalidArgumentException: "Source path has to start with a slash." at ./docroot/core/lib/Drupal/Core/Path/AliasManager.php line 186
#0 ./docroot/core/includes/bootstrap.inc(582): _drupal_error_handler_real(8, 'Uninitialized s...', '/mnt/www/html/m...', 185, Array)
#1 ./docroot/core/lib/Drupal/Core/Path/AliasManager.php(185): _drupal_error_handler(8, 'Uninitialized s...', '/mnt/www/html/m...', 185, Array)
#2 ./docroot/modules/contrib/google_tag/google_tag.module(264): Drupal\Core\Path\AliasManager->getAliasByPath('')
#3 ./docroot/modules/contrib/google_tag/google_tag.module(192): _google_tag_path_check()
#4 ./docroot/modules/contrib/google_tag/google_tag.module(60): google_tag_insert_snippet()
#5 ./docroot/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(297): google_tag_page_attachments(Array)
#6 ./docroot/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(273): Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
Proposed resolution
Fix path handling for homepage paths.
Remaining tasks
Write some tests.
Original report
We keep getting
InvalidArgumentException: Source path has to start with a slash. in Drupal\Core\Path\AliasManager->getAliasByPath() (line 186 of web/core/lib/Drupal/Core/Path/AliasManager.php).
in our builds.
Notice: Uninitialized string offset: 0 in Drupal\Core\Path\AliasManager->getAliasByPath() (line 185 of web/core/lib/Drupal/Core/Path/AliasManager.php)
#0 web/core/includes/bootstrap.inc(566): _drupal_error_handler_real(8, 'Uninitialized s...', 'web/...', 185, Array)
#1 web/core/lib/Drupal/Core/Path/AliasManager.php(185): _drupal_error_handler(8, 'Uninitialized s...', 'web/...', 185, Array)
#2 web/modules/contrib/google_tag/google_tag.module(264): Drupal\Core\Path\AliasManager->getAliasByPath('')
#3 web/modules/contrib/google_tag/google_tag.module(192): _google_tag_path_check()
#4 web/modules/contrib/google_tag/google_tag.module(60): google_tag_insert_snippet()
#5 web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(297): google_tag_page_attachments(Array)
#6 ...
This comes from:
<?php
// Compare the lowercase path alias (if any) and internal path.
$path = rtrim($current_path->getPath($request), '/');
$path_alias = Unicode::strtolower($alias_manager->getAliasByPath($path));
the current path returns / for the frontpage, so that gets stripped out
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | google_tag-homepage_uncaught_exception-2978157-3.patch | 855 bytes | chop |
Comments
Comment #2
chop commentedComment #3
chop commentedComment #4
chop commentedComment #5
solotandem commentedThanks for reporting this.
This has not been a problem before. What changed in core? If core API changed, then this is a 'task' or such to update this module for the core API change.
Comment #6
partyka commentedWe just encountered this in our CI environment.
Our CI runs the tests in a 'clean' database as opposed to using the production database. This means that the settings for the front page, 404, and 403 contain invalid NIDs, and once I cleared out these invalid NIDs and left them blank, the error went away. This is OK because we're using config split and our tests run with a differnet split enabled.
Hope this helps.
Comment #8
solotandem commentedGranted the
getAliasByPath()function expects a path with a leading slash and the absence thereof would constitute a bug, but the manner in which you arrive at this error seems a bit odd. You fabricated an invalid state and then expected the code to operate. [As it is the code works fine on a valid front page path of blank or '/'.] The essential ingredient to your test failure is that you are requesting the snippet on a 404 page response. This is the only time the error would occur. I find it rather unusual to be concerned with tracking on such a request, but to each his own.Again, thanks for reporting this.