1

I've created a custom block type.

I want to create a block with 19 translations programmatically. I've seen that you can use Block::create() method...for which I can neither find proper documentation nor examples. Could anyone provide an example?

Fyi my code is:

Block::create([
  'id' => 'my_header_dk',
  'settings' => [
    'id' => 'my_header',
    'label' => 'My header',
    'label_display' => FALSE,
    'provider' => 'my_block',
    'status' => TRUE,
    'langcode' = 'da',
    'icon' => [
      33,
     ],
    'header' => 'This is the header text',
    'link_text' => 'Click here',
    'visibility' => [],
    'region' => 'header',
    'plugin' => 'my_header',
    'theme' => 'my_theme',
    'status' => TRUE,
 ])->save();

1 Answer 1

9

If you want to create content for the custom block type basic:

use Drupal\block_content\Entity\BlockContent;

$block = BlockContent::create([
  'info' => 'block 1',
  'type' => 'basic',
  'langcode' => 'en',
]);
$block->save();

And add a french translation:

$french_block = $block->addTranslation('fr', ['info' => 'bloc 1']);
$french_block->save();

Custom block types are content, not configuration. See this answer https://drupal.stackexchange.com/a/218244/47547

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.