Getting the label for a bundle is still complicated, backport entity_get_bundles() so that the task is easier.

Original report

Just as the title says, there is no way that I can see to get from a bundle machine name (eg, "page") to its human-name (eg, "Basic page"). This calls for a utility function.

Issue fork drupal-969180

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Crell’s picture

Status: Active » Needs review
StatusFileSize
new1.06 KB

Something like this, for instance.

dave reid’s picture

Status: Needs review » Needs work
+++ includes/common.incundefined
@@ -7283,6 +7283,29 @@ function entity_label($entity_type, $entity) {
+ * @return
+ *   A string with the human-readable name of the bundle, or FALSE if not specified.

Needs to be wrapped since it exceeds 80 chars.

Also not sure why we need a blank line in the middle, otherwise I could use this too in a couple modules, although it's really not too hard to grab from the entity information directly.

Powered by Dreditor.

Crell’s picture

Status: Needs work » Needs review
StatusFileSize
new1.07 KB

You know we're way too religious about that at times.

And yes it's a simple function, but a common one. Common simple pattern = good case for utility function. :-)

dave reid’s picture

Status: Needs review » Needs work
+++ includes/common.incundefined
@@ -7283,6 +7283,29 @@ function entity_label($entity_type, $entity) {
+    $info = entity_get_info($entity_type);
+    $labels[$entity_type][$bundle] = !empty($info[$entity_type]['bundles'][$bundle]['label']) ? $info[$entity_type]['bundles'][$bundle]['label'] : FALSE;

1. We should probably pre-build all the possible bundle labels if !isset($labels[$entity_type]) to avoid multiple calls to entity_get_info(). Presumably this code will get called often when listing all bundles of an entity.

2. $info is already looked up by entity type, so trying to do $info[$entity_type] is invalid.

Powered by Dreditor.

Crell’s picture

Status: Needs work » Needs review
StatusFileSize
new1.43 KB

At Dave's suggestion, this version pre-fetches all bundle names so that subsequent calls are trivial.

Crell’s picture

StatusFileSize
new1.41 KB

And without the debug code... *sigh*

drasgardian’s picture

StatusFileSize
new1.23 KB

The function definition comment was repeated and had an incorrect parameter. And the parameter names were re-used within the for loops, which caused some issues.

Also, where a label isn't available then maybe returning the machine name back would be better than false.

My patch is untested, I had no intention to use a patched core but have borrowed the function for custom module.

Status: Needs review » Needs work

The last submitted patch, 969180-bundle-label_7.patch, failed testing.

jonathan_hunt’s picture

Status: Needs work » Needs review
StatusFileSize
new1.16 KB

I've fixed a couple of bad variable names from #7.

nick_vh’s picture

Status: Needs review » Reviewed & tested by the community

Using this and very useful still. Marking RTBC

David_Rothstein’s picture

Version: 7.x-dev » 8.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +needs backport to 1.x
  1. I think Drupal 8 needs this too, right? (Took a quick look through the D8 code and field_ui_widget_type_form() seems like a place that could use it.)
  2. I don't see a point in caching here since entity_get_info() is already cached. (Perhaps that was not the case in November 2010 when this patch was originally written.)
  3. If we need to avoid duplicate variable names, then the "normal" names ($entity_type and $bundle) should be used in the function parameters rather than the other way around. However, if we remove the caching that probably won't be necessary anyway.
David_Rothstein’s picture

Er, that is not the tag I intended to add... That would have been an interesting backport! :)

Crell’s picture

Assigned: Crell » Unassigned

The D8 version of this would be so different it's not even funny. :-) EntityNG may already have something for this; not sure.

alberto56’s picture

Related: See the comments on the entity_get_info API page, and also, for an example of somewhere this might be needed: #1953672: Make "These settings apply to the field everywhere it is used" clearer

tim.plunkett’s picture

Status: Needs work » Needs review
StatusFileSize
new952 bytes

If anyone thinks this is useful for D8, this would be the patch. I'm not so sure its needed, you could just call entity_get_bundles() yourself (it's heavily cached).

alberto56’s picture

@tim.plunkett Thanks! I used you technique for my latest patch in #1953672: Make "These settings apply to the field everywhere it is used" clearer. It's quite simple, so I'm not sure a dedicated function is necessary though.

tim.plunkett’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

Agreed.

brunodbo’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Closed (won't fix) » Active
Issue tags: -Needs backport to D7

This was closed for D8, but could we still get the patch in #9 into D7? Patch still applies, and it seems like a useful addition.

brunodbo’s picture

Status: Active » Reviewed & tested by the community

I'm using this patch to get the entity bundle label in an entityreference selection plugin, and it works great.

brunodbo’s picture

StatusFileSize
new1.16 KB

Fixing docs line wrapping and trailing whitespace.

chrisrockwell’s picture

#21 works well for me

Crell’s picture

I'm pretty sure we're not adding new features to D7 at this point, and this is a new feature (even if it's a non-invasive one).

fabianx’s picture

#23 As long as the same functionality is present in Drupal 8, we are good to go per out backports policy.

https://www.drupal.org/node/1527558 describes what changes can be added to a stable release a little bit more.

Features can be added at maintainer discretion, but need to be opt-in.

API additions are fine.

--

So we need to check if similar functionality is present in D8.

fabianx’s picture

Title: No way to get bundle label from bundle name » Backport entity_get_bundles() from Drupal 8 (No way to get bundle label from bundle name)
Issue summary: View changes
Status: Reviewed & tested by the community » Needs work

I've thought about this. While entity_get_bundle_name() was won't fix for Drupal 8, I would be okay to add a entity_get_bundles() function that does the same as in D8 - where it is deprecated, but has a replacement in the OO layer.

The code in #15 shows how simple the task to retrieve a bundle label then is.

If people disagree, we can also won't fix this.

David_Rothstein’s picture

Note this also still needs (or would have needed) work for #11 - that was not addressed in the latest Drupal 7 patch.

I have to say I wonder if there is really any significant difference between Drupal 7 and 8 here. I'm going to take a closer look...

David_Rothstein’s picture

Title: Backport entity_get_bundles() from Drupal 8 (No way to get bundle label from bundle name) » Add a convenience method for getting the label of a bundle
Version: 7.x-dev » 8.2.x-dev
Category: Bug report » Feature request
Issue tags: +Needs backport to D7

So yeah, minus some uncertainly over what you do when the label doesn't exist:

  1. The code in Drupal 8 (the procedural version) is basically this:
      $bundles = entity_get_bundles($entity_type);
      return isset($bundles[$bundle]['label']) ? $bundles[$bundle]['label'] : NULL;
    
  2. Whereas the code in Drupal 7 is basically this:
      $entity_info = entity_get_info($entity_type);
      return isset($entity_info['bundles'][$bundle]['label']) ? $entity_info['bundles'][$bundle]['label'] : NULL;
    

Looks pretty similar to me.

Backporting entity_get_bundles() to Drupal 7 (either as part of this issue or separate) doesn't sound like a bad idea, but I don't think it materially affects the issue this was originally filed about.

Based on the above, I think if this is "won't fix" for Drupal 8 it should be "won't fix" for Drupal 7 also. But I'm not sure. Just because it only takes two lines of code to do something doesn't mean a helper method isn't useful. It still is useful if the two lines of code aren't so intuitive. Are they?

For Drupal 8 at this point I suppose a new function would look something like EntityTypeBundleInfoInterface::getBundleLabel($entity_type, $bundle). I am not sure offhand if that is an interface that it is OK to add a new method to though.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joachim’s picture

I would suggest we close this in favour of #2699835: Add a method to ContentEntityBase for getting its Bundle entity.

If that adds a way of doing $entity->getBundleEntity() then you can just chain on to get the label like this: $entity->getBundleEntity()->getLabel().

David_Rothstein’s picture

@joachim, that requires you to have a particular entity to work with, but there's no reason to assume your code would have one (or even to assume that one exists on the site yet).

I played around it with a bit though and it seems that in Drupal 8 you can do things like this:

  entity_load('node_type', 'article')->label();        // Gives "Article"
  entity_load('taxonomy_vocabulary', 'tags')->label(); // Gives "Tags"

Which is maybe good enough. However, it was a bit non-intuitive to have to use "node_type" there; it would still be nice to have a way to go from "node" and "article" to the bundle label, as described above.

joachim’s picture

@David_Rothstein ah right, fair enough.

Homotechsual’s picture

For Drupal 7 this code works:

For the Bundle Machine Name

/**
 * Helper. Get bundle name.
 */
function entity_get_bundle_name($entity, $type) {
  $entity_info = entity_get_info($type);
  if (empty($entity_info['entity keys']['bundle'])) {
    $bundle = $type;
  } else {
    $bundle = $entity->{$entity_info['entity keys']['bundle']};
  }
  return $bundle;
}

For the Bundle Label

/**
 * Helper. Get bundle label.
 */
function entity_get_bundle_label($entity, $type) {
  $entity_info = entity_get_info($type);
  if (empty($entity_info['entity keys']['bundle'])) {
    $bundle = $type;
    $entity_type = $entity_info['bundles'][$bundle]['label'];
  } else {
    $bundle = $entity->{$entity_info['entity keys']['bundle']};
    $entity_type = $entity_info['bundles'][$bundle]['label'];
  }
  return $entity_type;
}

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

anicoto@gmail.com made their first commit to this issue’s fork.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.