TypeError: strpos(): Argument #1 ($haystack) must be of type string, Drupal\Core\Url given in strpos() (line 358 of core/lib/Drupal/Component/Utility/UrlHelper)

Created on 19 March 2023, over 1 year ago
Updated 25 March 2023, over 1 year ago

Problem/Motivation

I have been unable to resolve the following issue: TypeError: strpos(): Argument #1 ($haystack) must be of type string, Drupal\Core\Url given in strpos() (line 358 of /core/lib/Drupal/Component/Utility/UrlHelper). I am using PHP Version 8.1.17. I have searched and tried resolutions outlined in " https://www.drupal.org/project/commerce_square/issues/3273155 β†’ " and " https://www.drupal.org/project/drupal/issues/3294760#comment-14597948 β†’ ", nothing has worked for me

Steps to reproduce

I simply performed a composer update with all dependencies

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Closed: works as designed

Version

9.5

Component
BaseΒ  β†’

Last updated about 13 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @robertmartin1961
  • Status changed to Postponed: needs info over 1 year ago
  • Please post a stack trace. There isn’t much to go on to solve this bug without that.

  • πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

    This primarily occurs when attempting to access the status reports page. My url is: "http://sunraz-dev.localdomain/admin/reports/status"

  • πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

    The website encountered an unexpected error. Please try again later.
    TypeError: strpos(): Argument #1 ($haystack) must be of type string, Drupal\Core\Url given in strpos() (line 358 of core/lib/Drupal/Component/Utility/UrlHelper.php).
    strpos() (Line: 358)
    Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() (Line: 221)
    Drupal\Component\Render\FormattableMarkup::placeholderFormat() (Line: 195)
    Drupal\Core\StringTranslation\TranslatableMarkup->render() (Line: 15)
    Drupal\Core\StringTranslation\TranslatableMarkup->__toString() (Line: 100)
    __TwigTemplate_0baf146124ee9012bf3f6c074b132752->doDisplay() (Line: 405)
    Twig\Template->displayWithErrorHandling() (Line: 378)
    Twig\Template->display() (Line: 390)
    Twig\Template->render() (Line: 55)
    twig_render_template() (Line: 384)
    Drupal\Core\Theme\ThemeManager->render() (Line: 433)
    Drupal\Core\Render\Renderer->doRender() (Line: 204)
    Drupal\Core\Render\Renderer->render() (Line: 479)
    Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 79)
    __TwigTemplate_d2a832c6fea2e2169c051ae83f95850b->doDisplay() (Line: 405)
    Twig\Template->displayWithErrorHandling() (Line: 378)
    Twig\Template->display() (Line: 390)
    Twig\Template->render() (Line: 55)
    twig_render_template() (Line: 384)
    Drupal\Core\Theme\ThemeManager->render() (Line: 433)
    Drupal\Core\Render\Renderer->doRender() (Line: 204)
    Drupal\Core\Render\Renderer->render() (Line: 242)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 580)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 243)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 132)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
    call_user_func() (Line: 142)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 174)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 81)
    Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 68)
    Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle() (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 44)
    Drupal\services\StackMiddleware\FormatSetter->handle() (Line: 57)
    Drupal\advban\AdvbanMiddleware->handle() (Line: 50)
    Drupal\ban\BanMiddleware->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
    Stack\StackedHttpKernel->handle() (Line: 718)
    Drupal\Core\DrupalKernel->handle() (Line: 19)

  • Knowing from where the $uri value comes from will go a way toward solving this. According to the stack trace this is in a template, and it looks like something passed into the |t Twig filter is a Url object instead of a string.

    Do you have the capability of doing some debugging?

    One way to start would be to put this code above line 358 of core/lib/Drupal/Component/Utility/UrlHelper.php:

          if ($uri instanceof \Drupal\Core\Url) {
            echo $uri->toUriString();
            exit();
          }
    

    That is what I would do to just figure out what is going on.

  • πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

    Based on your suggestion, I did the following; which resulted in a blank page and not backtrace information.

    Before:

    public static function stripDangerousProtocols($uri) {
    $allowed_protocols = array_flip(static::$allowedProtocols);

    // Iteratively remove any invalid protocol found.
    do {
    $before = $uri;
    $colonpos = strpos($uri, ':');
    if ($colonpos > 0) {
    // We found a colon, possibly a protocol. Verify.
    $protocol = substr($uri, 0, $colonpos);
    // If a colon is preceded by a slash, question mark or hash, it cannot
    // possibly be part of the URL scheme. This must be a relative URL, which
    // inherits the (safe) protocol of the base document.
    if (preg_match('![/?#]!', $protocol)) {
    break;
    }
    // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3
    // (URI Comparison) scheme comparison must be case-insensitive.
    if (!isset($allowed_protocols[strtolower($protocol)])) {
    $uri = substr($uri, $colonpos + 1);
    }
    }
    } while ($before != $uri);

    return $uri;
    }

    After:

    public static function stripDangerousProtocols($uri) {
    $allowed_protocols = array_flip(static::$allowedProtocols);

    if ($uri instanceof \Drupal\Core\Url) {
    echo $uri->toUriString();
    exit();
    }

    // Iteratively remove any invalid protocol found.
    do {
    $before = $uri;
    $colonpos = strpos($uri, ':');
    if ($colonpos > 0) {
    // We found a colon, possibly a protocol. Verify.
    $protocol = substr($uri, 0, $colonpos);
    // If a colon is preceded by a slash, question mark or hash, it cannot
    // possibly be part of the URL scheme. This must be a relative URL, which
    // inherits the (safe) protocol of the base document.
    if (preg_match('![/?#]!', $protocol)) {
    break;
    }
    // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3
    // (URI Comparison) scheme comparison must be case-insensitive.
    if (!isset($allowed_protocols[strtolower($protocol)])) {
    $uri = substr($uri, $colonpos + 1);
    }
    }
    } while ($before != $uri);

    return $uri;
    }

  • It would have printed a URL.

    Do you have the capability of running a debugger? That is the only way forward unless you can list out steps to reproduce.

  • πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

    I do have the ability to run debugger. However, it appears that debug output is only generated in the browser. After enabling the debugger, all pages are displaying debug output, with the exception of "http://sunraz-dev.localdomain/admin/reports/status". This is the only page that is completely blank, not even an error message, all other pages render perfectly, both other admin and frontend pages. If there is a method to output debug in a text or log file, please direct me to the information.

  • Which PHP debugger are you using and where did you set breakpoints?

  • πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

    See Line #13

    [23-Mar-2023 00:58:08 America/Chicago] TypeError: strpos(): Argument #1 ($haystack) must be of type string, Drupal\Core\Url given in /var/www/html/sunraz-dev/web/core/lib/Drupal/Component/Utility/UrlHelper.php on line 358 #0 /var/www/html/sunraz-dev/web/core/lib/Drupal/Component/Utility/UrlHelper.php(358): strpos()
    #1 /var/www/html/sunraz-dev/web/core/lib/Drupal/Component/Render/FormattableMarkup.php(221): Drupal\Component\Utility\UrlHelper::stripDangerousProtocols()
    #2 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php(195): Drupal\Component\Render\FormattableMarkup::placeholderFormat()
    #3 /var/www/html/sunraz-dev/web/core/lib/Drupal/Component/Utility/ToStringTrait.php(15): Drupal\Core\StringTranslation\TranslatableMarkup->render()
    #4 /var/www/html/sunraz-dev/web/sites/default/files/php/twig/641b3342ebbc4_status-report-grouped.htm_zwpHoCyqOlyEJqLoZHXnDB1d_/9BiuP_EkAe1RSnIeyaJ2u_-ci3B1gl1ZUkQfodv6uEc.php(100): Drupal\Core\StringTranslation\TranslatableMarkup->__toString()
    #5 /var/www/html/sunraz-dev/vendor/twig/twig/src/Template.php(405): __TwigTemplate_0baf146124ee9012bf3f6c074b132752->doDisplay()
    #6 /var/www/html/sunraz-dev/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling()
    #7 /var/www/html/sunraz-dev/vendor/twig/twig/src/Template.php(390): Twig\Template->display()
    #8 /var/www/html/sunraz-dev/web/core/themes/engines/twig/twig.engine(55): Twig\Template->render()
    #9 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template()
    #10 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\Core\Theme\ThemeManager->render()
    #11 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender()
    #12 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Template/TwigExtension.php(479): Drupal\Core\Render\Renderer->render()

    #13 /var/www/html/sunraz-dev/web/sites/default/files/php/twig/641b3342ebbc4_status-report-page.html.t_Lw5HvqhiyBvHRlJ6FMZBzDTOM/XDKeUKmBqCt8_-i-X15lvWXQymD9gsxEIDFVoe8KrzA.php(79): Drupal\Core\Template\TwigExtension->escapeFilter()

    #14 /var/www/html/sunraz-dev/vendor/twig/twig/src/Template.php(405): __TwigTemplate_d2a832c6fea2e2169c051ae83f95850b->doDisplay()
    #15 /var/www/html/sunraz-dev/vendor/twig/twig/src/Template.php(378): Twig\Template->displayWithErrorHandling()
    #16 /var/www/html/sunraz-dev/vendor/twig/twig/src/Template.php(390): Twig\Template->display()
    #17 /var/www/html/sunraz-dev/web/core/themes/engines/twig/twig.engine(55): Twig\Template->render()
    #18 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template()
    #19 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/Renderer.php(433): Drupal\Core\Theme\ThemeManager->render()
    #20 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRender()
    #21 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(242): Drupal\Core\Render\Renderer->render()
    #22 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}()
    #23 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(243): Drupal\Core\Render\Renderer->executeInRenderContext()
    #24 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(132): Drupal\Core\Render\MainContent\HtmlRenderer->prepare()
    #25 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse()
    #26 [internal function]: Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
    #27 /var/www/html/sunraz-dev/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func()
    #28 /var/www/html/sunraz-dev/vendor/symfony/http-kernel/HttpKernel.php(174): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
    #29 /var/www/html/sunraz-dev/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
    #30 /var/www/html/sunraz-dev/web/modules/contrib/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(68): Symfony\Component\HttpKernel\HttpKernel->handle()
    #31 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle()
    #32 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
    #33 /var/www/html/sunraz-dev/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
    #34 /var/www/html/sunraz-dev/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
    #35 /var/www/html/sunraz-dev/web/modules/contrib/services/src/StackMiddleware/FormatSetter.php(44): Drupal\page_cache\StackMiddleware\PageCache->handle()
    #36 /var/www/html/sunraz-dev/web/modules/contrib/advban/src/AdvbanMiddleware.php(57): Drupal\services\StackMiddleware\FormatSetter->handle()
    #37 /var/www/html/sunraz-dev/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\advban\AdvbanMiddleware->handle()
    #38 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle()
    #39 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    #40 /var/www/html/sunraz-dev/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    #41 /var/www/html/sunraz-dev/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle()
    #42 /var/www/html/sunraz-dev/web/index.php(19): Drupal\Core\DrupalKernel->handle()

  • What I surmised in comment #6 is correct. And now you know it is in the status-report-grouped template. Examine it. Also try var_dump($uri) instead of echo as a variation of my suggestion in #6.

  • πŸ‡ΊπŸ‡ΈUnited States robertmartin1961

    Found The Problem

    Following days of frustration with trying to identify the root cause of issue that was preventing the rendering of the status page, I was hoping the find a quick and straight forward solution. The answer circled back to the fundamental task of isolating a module that may be the cause. I discovered that the problem was caused by the Translation Management Module Translation Message entity type. Once those were removed, the status page rendered. Problem solved.

  • Status changed to Closed: works as designed over 1 year ago
  • I am glad you isolated the problem. Thank you for following up.

Production build 0.71.5 2024