I know I can render my custom block programmatically with

$block = \Drupal\block_content\Entity\BlockContent::load(2);
$vars['contact_us_info_block']= \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);
$vars['contact_us_info_block'] = drupal_render($vars['contact_us_info_block']); 

Now I want to render SimpleNewsletter Block programmatically,I tried

$block_manager = \Drupal::service('plugin.manager.block');
$config = [];
$plugin_block =  $block_manager->createInstance('simplenewssubscription', $config);
$vars['newsletterblock'] = $plugin_block->build();

but faced with following message in output

This block is broken or missing. You may be missing content or you might need to enable the original module.

SimpleNewsletter module is enabled,how can I render contributes modules block programmatically?


I render it with following snippets and work correctly

$block = \Drupal\block\Entity\Block::load('simplenewssubscription');
$block_content = \Drupal::entityTypeManager()
$vars['newsletterblock'] = $block_content;
| improve this answer | |

Here is the example from Block Token module to render block based on its id:

 * Renders the block.
 * @param  string $bid
 *   Block Id.
 * @return string
 *   Rendered block.
function block_token_block_render($bid) {
  $block = \Drupal\block\Entity\Block::load($bid);
  $block_content = \Drupal::entityManager()
  return \Drupal::service('renderer')->renderRoot($block_content);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.