14

How do I get the human-readable label of a bundle from a loaded entity.

Lets say I want to get the label of a node of bundle my_article which is "My awesome article"

// Load a node
$node = Drupal::entityManager()->getStorage('node')->load(4);

print $node->bundle(); // prints the machine-readable name. e.g. 'my_article'.

print $node->getEntityType()->getBundleLabel(); // prints 'content type'.

I am only able to get the machine-readable name of the bundle or the Label of entity ("content type", "taxonomy term", etc), but how do I get the Label of the bundle?

P.S. I would prefer it to not use deprecated classes/functions

23

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();
  • 1
    Thanks, that works. I would just use entityTypeManager instead of the deprecated entityManager. – Linus Jan 25 '16 at 18:44
  • 1
    My bad, I didn't realise it was deprecated. Fixed it up – Clive Jan 25 '16 at 19:41
  • 9
    It's not available as a method, but the type is an entity reference, so you can also just do this: $node->type->entity->label() – Berdir Jan 25 '16 at 22:42
  • 1
    This is answering only the node type.. and not entities in general.. – ssibal May 9 '18 at 11:05
  • 2
    @ssibal Yep, as per the question. The bundle key will be different per entity type (but can also be retrieved from metadata) – Clive May 9 '18 at 11:18
9

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.

  • Very helpful to have an answer that works for all entity types. Thanks. – JamesWilson Feb 15 at 22:13
5

The following works as well:

$type = NodeType::load($node->bundle());
3

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();
}
  • This should be the correct answer: original question asking the "bundle label of entity" not the bundle label of a node.. – ssibal May 9 '18 at 11:04
1

Not the most efficient solution, but for sake of completeness, this works:

\Drupal::token()->replace('[node:type-name]', ['node' => $node]);

or

\Drupal::service('token')->replace('[node:type-name]', ['node' => $node]);

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.