8

How can I get the labels of specific fields within a node, in Drupal 7?

The field for which I am interested has the following structure:

    [field_text_question1] => Array(
      [und] => Array(
        [0] => Array(
          [value] => Sales Intern
          [format] => 
          [safe_value] => Sales Intern
        ) 
      )
    )

3 Answers 3

13

The best way is to use field_info_instance($entity_type, $field_name, $bundle_name) to get information about specific instace of a field. The label of the field is contained in the array returned from that function.

 $info = field_info_instance('node','field_name','content_type_name');
 print $info['label'];
1
  • i need to use the field_name but with entity translation can i ? i tried that but it's not working Oct 18, 2016 at 11:11
1

I believe you are looking inside the $nodevariable. It is not available there. Only the field values are store inside the $node variable not the field settings (such as the label). If you want the label look in the $content variable.

$content variable is available in the MYTHEME_preprocess_node(&$variable) theme template function, node.tpl.php, as well as, a few others.

If it is not available, you can call field_info_field($field_name)

0

The easiest way to print the field label of a node is:

<?php print $content['field_your_field_name']['#title']; ?>

(I've discovered it after a lot of print_r arrays and objects of Drupal! If it's so easy, why isn't explained anywhere, or why is so hidden that I've not found the trick? And passed a long time from the question to this answer...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.