I created a new custom block type from /admin/structure/block/block-content/types having x and y fields with field types image and textfield respectively. Then created a block with the same block type and uploaded image in x and some text in y. Here my problem is: how to load this block via code?

More than that how to get values of these block fields x and y by loading the block?

I tried these:

$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityManager()->getViewBuilder('block_content')->view($block);

but it didn't work.


Place the block in the disabled section at admin/structure/block, this creates a block instance with your block_content type, give it a nice easy machine name to remember.

place block in disabled section

By placing it in the disabled section, it won't show up, until you load it programatically.

Then using your machine name:

$machine_name = 'myblock';

$block = \Drupal::entityTypeManager()
if (!empty($block)) {
  $block_content = \Drupal::entityTypeManager()

  $pre_render = $block_content;

If you really want to get the rendered fields, you can do:

$render = \Drupal::entityTypeManager()

But it is more standard practice to use Drupal's formatters and such and perhaps a custom template, rather than building the block and extracting elements from the rendered render array.


There are two questions. How to load and render a configured block is answered perfectly by @oknate.

The second question: More than that how to get values of these block fields 'x' and 'y' by loading the block?

That's easy, because you've already loaded the block content entity, where the fields are stored in the database.

$block = \Drupal\block_content\Entity\BlockContent::load($bid);

You can access the value from the text field

$text = $block->field_y->value;

and get the url from the image field.

$image_url = file_create_url($block->field_x->entity->uri->value);
  • Please explain how to get this $bid?
    – user21513
    Dec 21 '16 at 10:11
  • $bid is the block content id. You see this in the url: /block/[bid]. This is the same as $nid is for a node: /node/[nid]
    – 4k4
    Dec 21 '16 at 10:32
  • Suppose you are placing the block in , contact/form page . I need to get this bid dynamically , How to get? hook_page_preprocess dynamically able to get the machine id . with $block = \Drupal\block_content\Entity\BlockContent::load($machine_bid); Its returning null
    – user21513
    Dec 21 '16 at 10:35
  • See drupal.stackexchange.com/questions/216672/…
    – 4k4
    Dec 21 '16 at 10:55

My case was different: I am loading a custom block in the .theme file. I was not able to get the exact "Block ID" in a digit.

I got the block machine name. In this case, use the following code.

$block_config = \Drupal\block\Entity\Block::load($machine_blockid);
$uuid = $block_config->getPlugin()->getDerivativeId();
$block_content = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid);
if ($block_content) {
  $img = file_create_url($block_content->field_X_image->entity->uri->value);
  $color = $block_content->field_Y_textfield->value;

Otherwise, use the following code. $bid is the block content id. You see this in /block/[bid].

$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$x_img_url = file_create_url($block_content->field_X_image->entity->uri->value);;
$y_textfield_value = $block_content->field_Y_textfield->value;

Try this code by including EntityInterface:

// Block 
use Drupal\Core\Entity\EntityInterface;
// the last part of the parameter is the block id
$block = \Drupal\block\Entity\Block::load('yourblockid');
$block_content = \Drupal::entityManager()
$block = \Drupal::service('entity.repository')->loadEntityByUuid('block_content', $uuid);
      if (!empty($block)) {
        $recArticleAdView = \Drupal::entityTypeManager()
        $rendered = \Drupal::service('renderer')->render($recArticleAdView);
        $response = new CacheableResponse($rendered);
        $variables['content']['sponsored_content'] = Markup::create(trim(strip_tags($response->getContent(), '<div>')));

Your Answer

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