Score:1

Add HTML markup to a new paragraph text field

es flag

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.

Score:1
cn flag

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',
  ],
]);
scaffolding avatar
es flag
Thank you @4k4, that worked perfectly.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.