1

I'm creating a new paragraph item within a hook_node_presave() function.

The paragraph item is fairly simple, just a title and body text written into a text field with full html text editing. When I attempt to html elements within the body text, I'm unable to get the content to render the HTML tags.

Is there a way to do this using a render array, or another technique?

This is my initial attempt to build the paragraph, putting my html tags directly into the field that contains the body content.

$summary_paragraph = Paragraph::create([
  'type' => 'paragraph_text', //this is the paragraph type
  'field_title' => 'Title text',
  'field_content' => '<p>Sentence one.</p><p>Sentence two</p>.',
]);

The title appears correctly and field_content prints the tags along with the text.

I also tried with a render array.

$content_text = [
  '#type' => 'processed_text',
  '#text' => '<p>Paragraph one.</p><p>Paragraph two.</p>',
  '#format' => 'full_html',
];
      
$summary_paragraph = Paragraph::create([
  'type' => 'paragraph_text', //this is the paragraph type
  'field_title' => 'Title text',
  'field_content' => $content_text,
]);

In this case, only the title was printed.

1 Answer 1

1

To store a formatted text field you need a field array with two properties value and format:

$summary_paragraph = Paragraph::create([
  // ...
  'field_content' => [
    'value' => '<p>Paragraph one.</p><p>Paragraph two.</p>',
    'format' => 'full_html',
  ],
]);
1
  • Thank you @4k4, that worked perfectly. Aug 20, 2021 at 10:04

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.