HTML tags in a block body are not rendred
-
26-12-2020 - |
Question
I'm using Drupal 8. I have created a custom installation profile. Inside the profile profileName.install file, I created the following function:
function setupCustomBlocks() {
$blockContent = BlockContent::create([
'type' => 'basic',
'info' => 'Why choose us?',
]);
$blockContent->set('body', '<p><i class="fa fa-drupal"> </i></p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>'
);
$blockContent->save();
$block = Block::create([
'id' => 'why_choose_us',
'plugin' => 'block_content:' . $blockContent->uuid(),
'region' => 'topwidget_first',
'provider' => 'block_content',
'weight' => 0,
'theme' => \Drupal::config('system.theme')->get('default'),
'visibility' => array(),
'settings' => [
'label' => 'Why choose us?',
'label_display' => TRUE,
],
]);
$block->save();
}
The bellow function, allows me to create a custom block when I install the profile. Here is the result:
As you can see, the HTML tags are not rendering. How can fix that?
Solution
It's rendering the block's content a plain text, not HTML, because you aren't setting the body field's format property when creating the BlockContent
entity.
$blockContent = \Drupal\block_content\Entity\BlockContent::create([ 'type' => 'basic', 'info' => 'Why choose us?' ]);
$blockContent->body->value = '<p><i class="fa fa-drupal"> </i></p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>';
$blockContent->body->format = 'full_html';
$blockContent->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange