Enabling Tokens Breaks Token functionality.
Problem/Motivation
On my Drupal 9.4.5 system on top of PHP 8.1.8 tokens gets totally hosed. Certain Tokens for example the Node Summary will not show up, also when I try to access the Nodes Debug Tab and select tokens I get the following error:
Exception: Object of type Drupal\media\Entity\MediaType cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter() (line 454 of core/lib/Drupal/Core/Template/TwigExtension.php).
__TwigTemplate_b6fb0f06f4012eb00481b6d8ec510b00->doDisplay(Array, Array) (Line: 405) Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378) Twig\Template->display(Array) (Line: 390) Twig\Template->render(Array) (Line: 55) twig_render_template('themes/contrib/gin/templates/dataset/table.html.twig', Array) (Line: 384) Drupal\Core\Theme\ThemeManager->render('table', Array) (Line: 422) Drupal\Core\Render\Renderer->doRender(Array) (Line: 435) Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201) Drupal\Core\Render\Renderer->render(Array, ) (Line: 241) Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent{closure}() (Line: 564) Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242) Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132) Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90) Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) call_user_func(Array, Object, 'kernel.view', Object) (Line: 142) Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163) Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80) Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58) Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48) Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106) Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85) Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 50) Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48) Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51) Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23) Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709) Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
I have tried numerous Admin themes and it wasn't until I disabled the Devel module did the tokens respond correctly and the above error went away.
Proposed resolution
Even though it is poor form to have Devel enabled on a Production Site, there are times when it is needed such as on a new launch or debugging. With this issue not being resolved it makes it more difficult to do so.