0

I have been searching for a while, but cannot find an answer to this. How can I get access to the field name in a view (the label used in the content type)?

I am trying to create an anchor navigation through a view that only displays a link to a field if it has content. I have a custom content type with a number of long text fields and a view, and I would like to rewrite the content of the field with its field label but I cannot figure out how to access the field label. I would like to do it programatically in case the user asks to change the name of a field.

1

In Drupal 7 the _field_info_field_cache() function retrieves the FieldInfo which gives you access to various field data.

function get_field_label($entity, $bundle, $field) {
  $field_info = _field_info_field_cache();
  $fields = $field_info->getBundleInstances($entity, $bundle);
  return isset($fields[$field]['label']) ? $fields[$field]['label'] : FALSE;
}

//Retrieve the tags label from an article node in a D7 Standard install
get_field_label ('node','article','field_tags');

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.