22

Is there a way to hide the field label when rendering a custom field in Drupal 7?

In my node template I have:

<?php print render($content['field_image']); ?>

Which gives me:

<div class="field field-name-field-image field-type-image field-label-above">
  <div class="field-label">Image:&nbsp;</div>
  <div class="field-items">
    <div class="field-item even">
      <img typeof="foaf:Image" src="http://site.com/sites/default/files/image.jpg" alt="">
    </div>
  </div>
</div>

I'd like to not have the <div class="field-label">Image:&nbsp;</div>.

Is there a way to get rid of this other than in the CSS or manually creating the markup?

I've tried unset($content['field_image']['#title']), but that gives me <div class="field-label">:&nbsp;</div>.

  • 7
    Nevermind I figured it out, you set #label_display to hidden: <?php $content['field_image']['#label_display'] = 'hidden'; ?> – Ben Apr 27 '11 at 19:31
  • 1
    You could add that as an answer and accept it, so that it is clear the answer is resolved. – Berdir Apr 27 '11 at 19:51
  • i had same issue but i want to do a field template for my node . how can i ? instead of removing the label – user4298 Dec 2 '11 at 7:04
  • +1 on your question, Ben, but your accepted answer should be @Marta's answer below. NO need to write ANY CODE for this problem! Go to: admin/structure/types/manage/YOUR CONTENT TYPE/display to show your list of fields and under the Label, select <Hidden> from the drop down menu. Click the Save button. This answer should apply to field types, perhaps all of them, not just files. – therobyouknow Jun 28 '13 at 15:18
24

Instead of hardcoding the setting for field labels in the template you should instead use the setting under Manage display tab where you add and remove fields for your entity.

  • Googletorp, how do you hide the label of the title then? – user4422 Dec 9 '11 at 5:47
  • 1
    If you edit the content type you can change the value in Title field label. It's a bit annoying that the title is still mandatory, but that's life I guess. – Malks Dec 9 '11 at 5:52
  • 1
    Is it possible to display the field labels differently when creating content and when displaying it publicly? – Jay Jan 17 '12 at 22:36
  • @Jay it would be possible, but would require code, either custom or through a module. I don't know of any module that does this for you right now though. – googletorp Jan 18 '12 at 13:30
  • 1
    Ben, your accepted answer should be @Marta's answer below. No need to write any code! Go to: admin/structure/types/manage/YOUR CONTENT TYPE/display to show your list of fields and under the Label, select <Hidden> from the drop down menu. Click the Save button. This answer should apply to field types, perhaps all of them, not just files. – therobyouknow Jun 28 '13 at 15:17
11

To hide the name of a custom field (file field in this case with the name "file") in order to only show a list of downloadable files and not the name of the field do the following:

  • Choose the Content Type that contains the file field (I created a new type called "File List")
  • Navigate to the "Manage Fields" Tab
  • Under Label choose "Hidden" and for Format, "Generic File"
  • Save and create a new node of this content type and the name "file" should no longer be displayed
  • 2
    +1 @Marta's answer above should be the accepted answer - admin/structure/types/manage/YOUR CONTENT TYPE/display to show your list of fields and under the Label, select <Hidden> from the drop down menu. Click the Save button. This answer should apply to field types, perhaps all of them, not just files. – therobyouknow Jun 28 '13 at 15:16
  • I agree! The less you do with coding or css in drupal the better :) – jasonflaherty Mar 18 '14 at 17:25
7

Yes, using the #title_display property of the Form API.

  • Is not that '#label_display'? – Shoaib Nawaz Feb 11 '14 at 18:15
7
$content['field_image']['#label_display']='hidden';
  • Best answer for my use case. – Kiee Apr 27 '17 at 8:22
4

using the template field.tpl.php you can remove or customize the label ($label in the template) of your field. to use it you must add the template field field.tpl.php to your theme:

<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item): ?>
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
    <?php endforeach; ?>
  </div>
</div>

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