2

Unfortunately I haven't found anything on the web with this specific problem.

I have a field (text, formatted, long) and a form with a textarea element that should save the entered text to this field.

Now I want to render it in my twig template. I access the value like this in my controller:

'text' => $node->field_text->value

And I render it in twig normally:

<p>{{ text }}</p>

But whenever I entered a new line by pressing enter in the textarea, it doesn't render the new line. The same thing in the CKEditor when I edit the node and look at the content, there's no new line. textareackeditordpm

I'm sure that there's a linebreak though, because when I dpm() this value, it prints that.

I tried putting the value in a '#markup' => $text render array, but it was the same. How can I render this new line?

3
  • What is in dpm? In your CKEditor it does not seem you have a new line there.
    – Refilon
    Commented Jun 11, 2020 at 14:01
  • as @Refilion says , you have a problem in your CKEditor since the <br /> not added when you hit enter, focus on that problem and the rendering problem will be resolved auto. Commented Jun 12, 2020 at 10:34
  • @Refilon dpm() is a function of the devel module to print an input into the Drupal message area. And yes, as I stated, the CKEditor didn't show the new line even though I entered it in a custom form - and I wanted to keep using the custom form, not the editor. But thanks for the comments, the problem is solved (see accepted answer) :)
    – mly
    Commented Jun 12, 2020 at 15:46

1 Answer 1

3

I tried putting the value in a '#markup' => $text render array, but it was the same. How can I render this new line?

Yes, you need to put the text in a render element, but for a formatted text field the correct element is processed_text:

'text' => [
  '#type' => 'processed_text',
  '#text' => $node->field_text->value,
  '#format' => $node->field_text->format,
],

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.