Drupal - How to get bundle label from entity

I can't see anything direct, loading the node type entity itself is an option though:

$bundle_label = \Drupal::entityTypeManager()
  ->getStorage('node_type')
  ->load($node->bundle())
  ->label();

Or as Berdir pointed out in the comments, there's a quicker way through the entity reference field:

$bundle_label = $node->type->entity->label();

Note that the accepted answer applies specifically to node entities but all entities have bundles. Many entities, like user or menu_link_content (for custom menu links), only have one bundle which corresponds to the entity type itself.

The entity_type.bundle.info service, implemented by Drupal\Core\Entity\EntityTypeBundleInfo, provides access to entity bundle information. Its methods getAllBundleInfo() and getBundleInfo($entity_type_id) return an array keyed by entity type and bundle machine name respectively, with the former then containing an array of bundles keyed by bundle machine name. Each bundle has a label key with the translated bundle friendly name.

Below is an example that shows the difference between content entity machine names, labels, bundle machine names, and bundle labels. The code assumes that there is at least one custom menu link with an ID of 1. It also assumes there is an article node type (bundle), that there is at least one node with an ID of 1, and that the node is of the node type (bundle) article.

<?php

$entity_type_manager = \Drupal::entityTypeManager();
$bundle_info = \Drupal::service("entity_type.bundle.info")->getAllBundleInfo();

$current_user = \Drupal::currentUser()->getAccount();

// Prints "user".
print $current_user->getEntityTypeId() . PHP_EOL;

// Prints "User".
print $current_user->getEntityType()->getLabel() . PHP_EOL;

// Prints "user".
print $current_user->bundle() . PHP_EOL;

// Prints "User".
print $bundle_info[$current_user->getEntityTypeId()][$current_user->bundle()]['label'] . PHP_EOL;

$my_menu_link = $entity_type_manager->getStorage('menu_link_content')->load(1);

// Prints "menu_link_content".
print $my_menu_link->getEntityTypeId() . PHP_EOL;

// Prints "Custom menu link".
print $my_menu_link->getEntityType()->getLabel() . PHP_EOL;

// Prints "menu_link_content".
print $my_menu_link->bundle() . PHP_EOL;

// Prints "Custom menu link".
print $bundle_info[$my_menu_link->getEntityTypeId()][$my_menu_link->bundle()]['label'] . PHP_EOL;

$my_article = $entity_type_manager->getStorage('node')->load(1);

// Prints "node".
print $my_article->getEntityTypeId() . PHP_EOL;

// Prints "Content".
print $my_article->getEntityType()->getLabel() . PHP_EOL;

// Prints "article".
print $my_article->bundle() . PHP_EOL;

// Prints "Article".
print $bundle_info[$my_article->getEntityTypeId()][$my_article->bundle()]['label'] . PHP_EOL;

Be sure to use dependency injection where possible in your code rather than relying on the static methods of the Drupal class.


If you are not sure about the $entity type, it can be:

if ($entity->bundle()) {
  $bundle_type_id = $entity->getEntityType()->getBundleEntityType();
  $bundle_label = \Drupal::entityTypeManager()
    ->getStorage($bundle_type_id)
    ->load($entity->bundle())
    ->label();
}

Tags:

Entities

8