Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DX] Add entity_get_bundles() as a convenient method for getting the label of a bundle. #3243

Open
klonos opened this issue Aug 5, 2018 · 0 comments

Comments

@klonos
Copy link
Member

klonos commented Aug 5, 2018

While testing and troubleshooting #3033, #3241, and #3242, I came across this issue in d.org that was originally filed against D7 back in 2010, was pushed to 8.x-dev, and then was wontfixed in 2013. People reopened it in 2016 and have asked for it to be added to D7, but there is no maintainer consensus about adding new features in D7 (even if they admitted that this was a "non-invasive" API addition).

There have been suggestions to backport entity_get_bundles() from D8.

There is an existing patch that has been tested by people, which basically adds this code to /includes/common.inc:

/**
 * Returns the label of a bundle.
 *
 * @param $input_entity_type
 *   The entity type; e.g. 'node' or 'user'.
 * @param $input_bundle
 *   The machine name of the bundle for which we want the human-readable label.
 *
 * @return
 *   A string with the human-readable name of the bundle, or the machine
 *   readable name if not specified.
 */
function entity_bundle_label($input_entity_type, $input_bundle) {
  $labels = &drupal_static(__FUNCTION__, array());

  if (empty($labels)) {
    foreach (entity_get_info() as $entity_type => $entity_info) {
      foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
        $labels[$entity_type][$bundle] = !empty($bundle_info['label']) ? $bundle_info['label'] : $bundle;
      }
    }
  }

  return isset($labels[$input_entity_type][$input_bundle]) ? $labels[$input_entity_type][$input_bundle] : $input_bundle;
}

...and finally, there is a comment that recommends the following:

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant