I have got this error:
The website encountered an unexpected error. Please try again later.
Recoverable fatal error: Object of class Drupal\Core\Url could not be converted to string in Drupal\Component\Render\HtmlEscapedText->__construct() (line 41 of core/lib/Drupal/Component/Render/HtmlEscapedText.php).
Drupal\Component\Render\HtmlEscapedText->__construct(Object) (Line: 204)
Drupal\Core\Utility\Token->replace('[node:field_jissue_cover:entity:url]', Array, Array) (Line: 40)
Drupal\metatag\MetatagToken->replace('[node:field_jissue_cover:entity:url]', Array, Array) (Line: 293)
Drupal\metatag\MetatagManager->generateElements(Array, Object) (Line: 321)
metatag_get_tags_from_route() (Line: 143)
metatag_entity_view_alter(Array, Object, Object) (Line: 501)
Drupal\Core\Extension\ModuleHandler->alter(Array, Array, Object, Object) (Line: 285)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 220)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func(Array, Array) (Line: 376)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 149)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 656)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Comments
Comment #2
brtamas commentedPatch attached.
Comment #3
cilefen commentedHow does this happen?
Comment #4
kenorb commentedComment #5
kenorb commentedComment #8
didebruRan into the same error with [node:field_teaser_media:entity:url] token.
Comment #9
didebruPatch #2 fixed it for me, Thanks!
Comment #13
acke commentedI got this problem using an image field url as a token [node:field_image:entity:field_image:entity:url] with meta tags module. Recoverable fatal error: Object of class Drupal\Core\Url could not be converted to string i Drupal\Component\Render\HtmlEscapedText->__construct(). Patch #2 fixed it for me.
* Drupal core 8.8.2
* Token 8.x-1.6
* Metatag 8.x-1.11
Comment #14
sivaji_ganesh_jojodae commented#8 & #13 are confirming the occurrence of issue & fix by patch #2.
The documentation reads needed as a string. Maybe we can add a type hinting to handle appropriately at the incorrect caller?
Comment #15
sivaji_ganesh_jojodae commentedPatch #2 re-rolled.
Comment #16
longwaveComment #18
pavlosdanThe issue also happens when trying to use a Link field URL through a token. The patch in #15 works for 8.8 as well.
Comment #21
abhinand gokhala k commentedReviewed and tested #15. it's working fine.
RTBC
Comment #22
alexpottWe need some test coverage.
Comment #24
jejddx commentedPatch #15 doesn't work if the object has no 'toString' method.
I did that using Simplenews 8.x-2.0-beta2 (that triggers the error), Drupal 9.3.0, PHP 7.4.26
Sorry, i don't have time to make a patch.
Comment #26
vas-alyfantis commentedComment #27
vas-alyfantis commentedComment #28
vas-alyfantis commentedComment #29
vas-alyfantis commentedComment #30
vas-alyfantis commentedComment #31
vas-alyfantis commentedComment #33
theodorosploumisPatch from #32 works for 9.5.x.
Comment #34
theodorosploumisAdding the latest diff as attachment since the Merge Request diff is dynamic and may change over time.
Comment #36
alexpottI've tried to re-create this bug according to the information in the issue summary. I created an entity reference field and used it as a token replacement in a text field that supported tokens - it looked something like
[node:field_test:entity:url]- this works absolutely fine. The token module is correctly calling toString() on the URL object to get the token value.We need more information with steps to reproduce. It feels that the real problem here is in the token provider not calling ->toString() on URL objects and fixing this in HtmlEscapedText is in the wrong place.
Comment #37
alexpottI also tested this with a link field and
[node:field_link:uri]works as expected without error.Comment #39
smustgrave commentedSince there hasn't been a follow up going to close out as outdated for now. If still a valid bug in D10 please update issue summary with steps to reproduce.
Comment #40
dimitriskr commentedI got a similar error on my project. I'm trying to find clean reproducible steps. For now, I got it when I tried to render a message entity that had an entity reference field for a view and I used a token
[message:field_view_ref:entity:label]in the message's text and rendered it programmaticallyError: Object of class Drupal\views\Entity\View could not be converted to string in Drupal\Component\Render\HtmlEscapedText->__construct() (line 31 of core/lib/Drupal/Component/Render/HtmlEscapedText.php).Comment #41
erwangel commentedObject of class Drupal\media\Entity\MediaType could not be converted to string in Drupal\Component\Render\HtmlEscapedText->__construct() (line 31...
when saving an image (media entity) with a "description" field having format "filtered HTML".
MR 2707 resolved the problem.
Drupal 10.5.6, php 8.3
Comment #42
jcandan commentedForcing the string in HtmlEscapedText() is the wrong fix.
We need to look at why we're skipping the
isset($field_item->$property_name) && ($field_item->$property_name instanceof FieldableEntityInterfaceconditional branch inDrupal\token\Hook\TokenTokensHooks::fieldTokens().I cannot currently reproduce this in a clean installation, but debugging the instance where I am seeing this confirms that
isset($field_item->entity)returns FALSE whileget_class($field_item->entity)returns a valid entity class in the same request.MR forthcoming over at #3574391: Fix Object of class could not be converted to string in HtmlEscapedText error.