1

For create block programmatically I use

$block = BlockContent::create([
  'info' => 'block 3',
  'type' => 'basic',
  'langcode' => 'en',
  'body' => [
    'value' => '<h1>Block's body</h1>',
    'format' => 'full_html',
  ],
]);
$block->save();

It works good enough but is it possible also to specify region and then on which pages created block must shown on?

3
  • I think it is not possible to mention region while creating custom block. Better go to structure -> block layout-> click place block in any one of the region and assign the block once you created a custom block Commented Nov 29, 2017 at 8:59
  • If that true it's very sad becuase I need ceate many blocks and manually specify region and pages for each of them will took too much time. Anyway thanks for the answer.
    – Czaa
    Commented Nov 29, 2017 at 9:07
  • Or maybe somone knows way to do it after creating of block? I mean add some code after string $block->save();
    – Czaa
    Commented Nov 29, 2017 at 9:18

2 Answers 2

5

You have to create a block config entity to place a block:

use Drupal\block\Entity\Block;

  $placed_block = Block::create([
    'id' => 'block_machine_name',
    'theme' => 'classy',
    'weight' => 0,
    'status' => TRUE,
    'region' => 'footer',
    'plugin' => 'block_content:' . $block->uuid(),
    'settings' => [],
    'visibility' => [
      'request_path' => [
        'id' => 'request_path',
        'negate' => FALSE,
        'pages' => '/path',
      ],
    ],
  ]);
  $placed_block->save();
6
  • Oh! Many thanks!!! I'll try it for sure but what about pages on which block must shown on? Is it possible to set it in code above?
    – Czaa
    Commented Nov 29, 2017 at 12:40
  • Yes, put this in the now empty visibility array. Each visibility plugin has different parameters. Configure one in UI and look in the exported config.
    – 4uk4
    Commented Nov 29, 2017 at 12:51
  • You wrote "put this in the now empty visibility array" what do you mean by word "this"? You missed smth?
    – Czaa
    Commented Nov 29, 2017 at 13:01
  • I add an example for a path visibility condition.
    – 4uk4
    Commented Nov 29, 2017 at 13:02
  • I was trying it but something went wrong. My block was created but it didn't attached to region. What I need to place at 'id' field instead of 'block_machine_name' ? I was trying to put there name of my block but seems it improperly.
    – Czaa
    Commented Nov 29, 2017 at 18:02
5

OK, at least I managed to solve this problem.

Below the full code that create block and then bind it to region and also set pages on which created block must shown on. Maybe it can helps anyone.

 $block = BlockContent::create([
         'info' => 'block242',
         'type' => 'basic',
         'langcode' => 'en',
         'body' => [

          'value' => '<h1>Block's body</h1>',
            'format' => 'full_html',
           ],
          ]);


     $block->save();


       $placed_block = Block::create([
         'id' => 'block242',
            'theme' => 'scholarly',
'weight' => -7,
'status' => TRUE,
'region' => 'sidebar_second',
'plugin' => 'block_content:' . $block->uuid(),
'settings' => [],
'visibility' => [
  'request_path' => [
    'id' => 'request_path',
    'negate' => FALSE,
    'pages' => '/your_page',
  ],
],
        ]);
    $placed_block->save();  

And of course, very very BIG thanks to user 4k4. Without his help I'd never do this.

1
  • Don't forget to use Drupal\block_content\Entity\BlockContent;
    – JiteshNK
    Commented Jan 10, 2020 at 7:25

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.