[PHP 8.1] Gracefully handle malformed token - get rid of deprecation messages

Created on 19 July 2022, over 2 years ago
Updated 24 April 2023, over 1 year ago

Problem/Motivation

After updating to php8.1 we're seeing these errors for metatags that have malformed token values. It is actually good to see that you have wrong tokens but still it is not nice to see errors popping up.

Deprecated function: preg_match_all(): Passing null to parameter #2 ($subject) of type string is deprecated in Drupal\Core\Utility\Token->scan() (line 293 of /app/web/core/lib/Drupal/Core/Utility/Token.php)

#0 /app/web/core/includes/bootstrap.inc(347): _drupal_error_handler_real(8192, 'preg_match_all(...', '/app/web/core/l...', 293)
#1 [internal function]: _drupal_error_handler(8192, 'preg_match_all(...', '/app/web/core/l...', 293)
#2 /app/web/core/lib/Drupal/Core/Utility/Token.php(293): preg_match_all('/\n      \\[     ...', NULL, NULL)
#3 /app/web/modules/contrib/token_default/token_default.module(66): Drupal\Core\Utility\Token->scan(NULL)
#4 /app/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(562): token_default_tokens_alter(Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#5 /app/web/core/lib/Drupal/Core/Utility/Token.php(366): Drupal\Core\Extension\ModuleHandler->alter('tokens', Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#6 /app/web/core/lib/Drupal/Core/Utility/Token.php(239): Drupal\Core\Utility\Token->generate('node', Array, Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#7 /app/web/core/lib/Drupal/Core/Utility/Token.php(189): Drupal\Core\Utility\Token->doReplace(true, '[node:field_tea...', Array, Array, Object(Drupal\Core\Render\BubbleableMetadata))
#8 /app/web/modules/contrib/metatag/src/MetatagToken.php(66): Drupal\Core\Utility\Token->replace('[node:field_tea...', Array, Array, NULL)
#9 /app/web/modules/contrib/metatag/src/MetatagManager.php(773): Drupal\metatag\MetatagToken->replace('[node:field_tea...', Array, Array)
#10 /app/web/modules/contrib/metatag/src/MetatagManager.php(622): Drupal\metatag\MetatagManager->processTagValue(Object(Drupal\metatag_open_graph\Plugin\metatag\Tag\OgImageType), Array, Array, false, 'en')
#11 /app/web/modules/contrib/metatag/src/MetatagManager.php(544): Drupal\metatag\MetatagManager->generateRawElements(Array, Object(Drupal\ldp_topic_page\Entity\Bundle\NodeTopicPage))
#12 /app/web/modules/contrib/metatag/metatag.module(508): Drupal\metatag\MetatagManager->generateElements(Array, Object(Drupal\ldp_topic_page\Entity\Bundle\NodeTopicPage))
#13 /app/web/modules/contrib/metatag/metatag.module(666): metatag_get_tags_from_route(Object(Drupal\ldp_topic_page\Entity\Bundle\NodeTopicPage))
#14 /app/web/modules/contrib/lupus_ce_renderer/src/CustomElementsRenderer.php(366): metatag_generate_entity_metatags(Object(Drupal\ldp_topic_page\Entity\Bundle\NodeTopicPage))
#15 /app/web/modules/contrib/lupus_ce_renderer/src/CustomElementsRenderer.php(195): Drupal\lupus_ce_renderer\CustomElementsRenderer->getMetatags(Object(Drupal\Core\Routing\CurrentRouteMatch))
#16 /app/web/modules/contrib/lupus_ce_renderer/src/EventSubscriber/CustomElementsViewSubscriber.php(67): Drupal\lupus_ce_renderer\CustomElementsRenderer->renderResponse(Object(Drupal\custom_elements\CustomElement), 'json', NULL)
#17 [internal function]: Drupal\lupus_ce_renderer\EventSubscriber\CustomElementsViewSubscriber->onKernelView(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#18 /app/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#19 /app/vendor/symfony/http-kernel/HttpKernel.php(163): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Symfony\Component\HttpKernel\Event\ViewEvent), 'kernel.view')
#20 /app/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#21 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 /app/web/modules/drunomics/ldp-core/modules/ldp_ce_api/src/BackendApiRequest.php(56): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 /app/web/modules/drunomics/ldp-core/modules/ldp_ce_api/src/MenuApiRequestMiddleware.php(43): Drupal\ldp_ce_api\BackendApiRequest->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\ldp_ce_api\MenuApiRequestMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#27 /app/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(49): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#28 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#29 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /app/web/core/lib/Drupal/Core/DrupalKernel.php(709): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#33 {main}

Steps to reproduce

Use malformed token ([node:field_teaser_media:entity:field_image:height] - image style is missing) and reload the page.

Proposed resolution

I wonder whether metatag should handle this error more gracefully and notify site administrator somewhere else (perhaps hook requirements).

Feature request
Status

Closed: duplicate

Version

1.0

Component

Code

Created by

🇸🇮Slovenia useernamee Ljubljana

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024