2

How do I get machine name by entity type and its ID?

The code I am using is the following.

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

1 Answer 1

2

To get the type and id of a given entity, you can do:

$type = $entity->bundle();
$id = $entity->id();

Some entities, like Node, provide wrapper methods to return the bundle:

$type = $node->getType();

But Block doesn't. So if it is Block that you are after:

$block = \Drupal\block_content\Entity\BlockContent::load(2);
$type = $block->bundle();
$id = $block->id();

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.