On my node edit form I've got a field widget that needs some information added to it. Before my alterations, the kint dump of $variables['children'] looks like:

enter image description here

However, after manually setting $variables['children'], the dump looks like:

enter image description here

Before alteration, html renders correctly. However, afterward it is all just rendered as a string with the HTML displaying on the page.

enter image description here

I've tried using the create() method instead that's shown as available under:

enter image description here

...but it doesn't do anything at all.

I've also instead tried changing $variables['element']['display']['#markup'], but that doesn't have any visible effect.

1 Answer 1


You can't modify a markup object, but you can use the create() method on a new markup object to replace the old one.

$variables['children'] = \Drupal\Core\Render\Markup::create('<div>Manually set</div>');

By the way, you are modifying a twig debug message which won't be there in a production environment.

  • Ah, \Drupal\Core\Render\Markup::create() is what I was looking for. And thanks for the heads up on the debug element -- ended up finding the proper one in the end.
    – Mrweiner
    Commented Jul 18, 2017 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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