4

I try to load the body field from a custom block in Drupal 8.

$block = \Drupal::entityManager()->getStorage('block')->load($block_id);

The result in $block is NULL.

A similar code for a node does work:

$node = \Drupal::entityManager()->getStorage('node')->load($node_id);
$body = $node->get('body')->value;

getStorage should work for all entities, why not for blocks?

What is wrong?

1
  • 2
    Presumably your $block_id is incorrect...
    – Clive
    Commented May 20, 2015 at 14:39

1 Answer 1

5

Block is the wrong machine name, that is a placed config entity, you want custom_block or block_content, can't remember which off the top of my head, check the ID in the entity type annotation in block content module

2
  • 3
    Oh, I missed the custom in there. Right, that's block_content.
    – Berdir
    Commented May 20, 2015 at 23:15
  • 3
    @Berdir has the correct answer, the entity is block_content, this code works: `$block = \Drupal::entityManager()->getStorage('block_content')->load('5'); $body = $block->get('body')->value;
    – 4uk4
    Commented May 21, 2015 at 6:30

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.