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()->

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();
| 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.