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)

Issue fork drupal-2877953

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

brtamas created an issue. See original summary.

brtamas’s picture

Patch attached.

cilefen’s picture

How does this happen?

kenorb’s picture

Version: 8.3.x-dev » 8.4.x-dev
Status: Needs review » Needs work
Related issues: +#2692923: Notice: Array to string conversion when token installed
kenorb’s picture

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

didebru’s picture

Ran into the same error with [node:field_teaser_media:entity:url] token.

didebru’s picture

Patch #2 fixed it for me, Thanks!

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

acke’s picture

I 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

sivaji_ganesh_jojodae’s picture

#8 & #13 are confirming the occurrence of issue & fix by patch #2.

/**
   * Constructs an HtmlEscapedText object.
   *
   * @param $string
   *   The string to escape. This value will be cast to a string.
   */
  public function __construct(string $string) {
    $this->string = (string) $string;
  }

The documentation reads needed as a string. Maybe we can add a type hinting to handle appropriately at the incorrect caller?

sivaji_ganesh_jojodae’s picture

Status: Needs work » Needs review
StatusFileSize
new632 bytes

Patch #2 re-rolled.

longwave’s picture

Title: HtmlEscapedText object to string conversation error » HtmlEscapedText object to string conversion error

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

pavlosdan’s picture

The issue also happens when trying to use a Link field URL through a token. The patch in #15 works for 8.8 as well.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

abhinand gokhala k’s picture

Reviewed and tested #15. it's working fine.
RTBC

alexpott’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

We need some test coverage.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jejddx’s picture

Patch #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

@@ -28,6 +28,9 @@ class HtmlEscapedText implements MarkupInterface, \Countable {
    *   The string to escape. This value will be cast to a string.
    */
   public function __construct($string) {
+    if (is_object($string) {
+      if (method_exists($string, 'toString')) {
+        $string = $string->toString();
+      } else {
+        $string = '';
+      }
+    }
     $this->string = (string) $string;
   }

Sorry, i don't have time to make a patch.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vas-alyfantis’s picture

StatusFileSize
new426 bytes
vas-alyfantis’s picture

vas-alyfantis’s picture

StatusFileSize
new705 bytes
vas-alyfantis’s picture

vas-alyfantis’s picture

StatusFileSize
new705 bytes
vas-alyfantis’s picture

StatusFileSize
new778 bytes

theodorosploumis’s picture

Patch from #32 works for 9.5.x.

theodorosploumis’s picture

StatusFileSize
new867 bytes

Adding the latest diff as attachment since the Merge Request diff is dynamic and may change over time.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Status: Needs work » Postponed (maintainer needs more info)

I'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.

alexpott’s picture

I also tested this with a link field and [node:field_link:uri] works as expected without error.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

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

dimitriskr’s picture

I 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 programmatically

Error: 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).

    $view_builder = \Drupal::entityTypeManager()->getViewBuilder('message');
    $pre_render = $view_builder->view($message_entity, 'default');
erwangel’s picture

Object 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

jcandan’s picture

Forcing 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 FieldableEntityInterface conditional branch in Drupal\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 while get_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.