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.

  1. Log in to Drupal as an author or administrator
  2. Go to the homepage of the website
  3. Edit the homepage (node)
  4. Set the homepage (node) to Unpublished and Save
  5. Log out of Drupal
  6. 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

Comments

zerolab created an issue. See original summary.

chop’s picture

Issue summary: View changes
chop’s picture

chop’s picture

Status: Active » Needs review
solotandem’s picture

Category: Bug report » Task

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

partyka’s picture

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

  • solotandem committed a33fa0a on 8.x-1.x
    Issue #2978157 Retain the slash on front page path.
    
    The path service...
solotandem’s picture

Assigned: Unassigned » solotandem
Status: Needs review » Fixed

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

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.