From doing some experimentation, the only way I've been able to find to get the string property out of an instance of Drupal\Core\Render\Markup is to use the __toString() method:


But I'm guessing those initial underscores indicate that this is a private function, not to be used on the outside. Is there some better method to get at the content? My ultimate goal is modifying that content in some way.


You can't edit the content. There is no public method to change it.

Usually you simply override it with a new instance:

$variables['fields']['title'] = Drupal\Core\Render\Markup::create('New content');

The double underscore indicates a magic function, which would be called automatically if you use the object in a string context. This is readonly.

If you want to add a string to the old markup, that would be an example how the magic function __toString() works:

$variables['fields']['title'] = Drupal\Core\Render\Markup::create($variables['fields']['title'] . '!!!');

You can invoke the magic function __toString() by casting to a string: $markup = $variables['fields']['title']->content; (string) $markup === $markup->__toString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.