Question
How to create 'basic' blocks content programmatically?
Solution
In your module, create the mymodule.install
file that will serve us for the creation of our block.
Here is the code for creating a simple "basic" block with Drupal 8:
<?php
/**
* Implements hook_install().
*/
function mymodule_install() {
$blockEntityManager = \Drupal::service('entity.manager')->getStorage('block_content');
// Create a block of type 'basic'
$block = $blockEntityManager->create(array(
'type' => 'basic'
));
// Every block should have a description
$block->info = 'How to create basic blocks content programmatically ?';
// Block content
$block->body->value = "<h1>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</h1>
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries.</p>
<h1>but also the leap into electronic typesetting, remaining essentially unchanged.</h1>
<p>It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
";
// Block body format
$block->body->format = 'full_html';
// save the block
$block->save();
}