function _token_field_label in Token 8
Returns the label of a certain field.
Therefore it looks up in all bundles to find the most used instance.
Based on views_entity_field_label().
@todo Resync this method with views_entity_field_label().
1 call to _token_field_label()
- field_token_info_alter in ./
token.tokens.inc - Implements hook_token_info_alter() on behalf of field.module.
File
- ./
token.tokens.inc, line 1653 - Token callbacks for the token module.
Code
function _token_field_label($entity_type, $field_name) {
$labels = [];
// Count the amount of instances per label per field.
foreach (array_keys(\Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type)) as $bundle) {
$bundle_instances = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle);
if (isset($bundle_instances[$field_name])) {
$instance = $bundle_instances[$field_name];
$label = (string) $instance
->getLabel();
$labels[$label] = isset($labels[$label]) ? ++$labels[$label] : 1;
}
}
if (empty($labels)) {
return [
$field_name,
];
}
// Sort the field labels by it most used label and return the labels.
arsort($labels);
return array_keys($labels);
}