Needs work
Project:
Drupal core
Version:
main
Component:
base system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
11 Nov 2010 at 20:16 UTC
Updated:
21 Mar 2022 at 16:59 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
Crell commentedSomething like this, for instance.
Comment #2
dave reidNeeds 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.
Comment #3
Crell commentedYou 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. :-)
Comment #4
dave reid1. 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.
Comment #5
Crell commentedAt Dave's suggestion, this version pre-fetches all bundle names so that subsequent calls are trivial.
Comment #6
Crell commentedAnd without the debug code... *sigh*
Comment #7
drasgardian commentedThe 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.
Comment #9
jonathan_hunt commentedI've fixed a couple of bad variable names from #7.
Comment #10
nick_vhUsing this and very useful still. Marking RTBC
Comment #11
David_Rothstein commentedComment #12
David_Rothstein commentedEr, that is not the tag I intended to add... That would have been an interesting backport! :)
Comment #13
Crell commentedThe D8 version of this would be so different it's not even funny. :-) EntityNG may already have something for this; not sure.
Comment #14
alberto56 commentedRelated: 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
Comment #15
tim.plunkettIf 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).
Comment #16
alberto56 commented@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.
Comment #18
tim.plunkettAgreed.
Comment #19
brunodboThis 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.
Comment #20
brunodboI'm using this patch to get the entity bundle label in an entityreference selection plugin, and it works great.
Comment #21
brunodboFixing docs line wrapping and trailing whitespace.
Comment #22
chrisrockwell commented#21 works well for me
Comment #23
Crell commentedI'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).
Comment #24
fabianx commented#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.
Comment #25
fabianx commentedI'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.
Comment #26
David_Rothstein commentedNote 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...
Comment #27
David_Rothstein commentedSo yeah, minus some uncertainly over what you do when the label doesn't exist:
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.
Comment #29
joachim commentedI 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().
Comment #30
David_Rothstein commented@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:
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.
Comment #31
joachim commented@David_Rothstein ah right, fair enough.
Comment #32
Homotechsual commentedFor Drupal 7 this code works:
For the Bundle Machine Name
For the Bundle Label