I have the label of some fields set to display above the content, and would like the labels to show up only if the fields have any content, right now even if the fields are empty, the label shows.

looking at ds.module at line 535-543

function ds_render_region($content, $region, $layout) {
  $output = '';
  if (isset($layout['settings']['regions'][$region])) {
    foreach ($layout['settings']['regions'][$region] as $key => $field) {
      $output .= render($content[$field]);
    }
  }
  return $output;
}

By adding an extra if statement:

function ds_render_region($content, $region, $layout) {
  $output = '';
  if (isset($layout['settings']['regions'][$region])) {
    foreach ($layout['settings']['regions'][$region] as $key => $field) {
      if ($content[$field]['#items'][0] != '') {
        $output .= render($content[$field]);
      }
    }
  }
  return $output;
}

The labels are gone from empty fields, but I am getting a warning

Notice: Undefined offset: 0 in ds_render_region() (line 539 of /Volumes/Data/Users/vinsab/Sites/feral/sites/all/modules/ds/ds.module)

Would it be possible to have this feature ? Many thanks

Comments

vinsabus’s picture

sorry was looking at RC3 code.. in the latest dev the code is between lines 523-531

edit: No, the code I pasted above does not work if I am using fieldgroups

swentel’s picture

Using 'if (isset($content[$field]['#items'][0]))' will make the notice go away normally. I'm going to check how core Field API does this and see what the most elegant solution is. I'm going to add some more options for developers/themers too so they can more easily override the default behavior of the rendering that DS does now. Will come back on this later.

swentel’s picture

Status: Active » Postponed (maintainer needs more info)

Can you give me an example of such a field, because when I add new fields (say text fields) and leave their content empty, I don't get labels at all.

altrugon’s picture

Version: 7.x-1.x-dev » 7.x-1.0
Status: Postponed (maintainer needs more info) » Active
StatusFileSize
new63.97 KB

I having the same problem and also have noticed that the markup for the field get into the output even if the field is empty and there is not label to displayed.

I have attached a screenshot so you can see the type of fields that I'm using.

vinsabus’s picture

Sorry, I've been away for long. The fields I've had problems with are file field with display set to jplayer. And multi-image field with display set to field-slideshow.

swentel’s picture

Status: Active » Closed (won't fix)

Well, I'm still not really able to reproduce. Granted, haven't tested all fields from #5, but I'm assuming one of them is the e-mail field from the email module and I don't have any problems with it. I also tested with the jplayer and the problem there is with that module that doesn't check if $items is empty or not in jplayer_field_formatter_view(). Even without a display suite layout, you will still see the label, so this isn't DS's fault at all.

altrugon’s picture

I fell into this problem again and found out that you have to return an empty array on your hook_field_formatter_view(), you can check taxonomy_field_formatter_view() as example.

drupalreggie’s picture

I've got this problem also using the D6 version of DS.

My conditions:

  1. Set DS exclude matrix to exclude all build types of content type except Full Node.
  2. Add content type's field to Display suite middle region.
  3. When viewing node in full node view the field label shows up even when field is empty.

Any clues?

havabeer’s picture

The issue occurs when using Term Reference fields. The label prints even when the field is empty.

d7.9 ds7x-1.4

azinck’s picture

I'm seeing this with Term Reference fields as well. Same exact version of Drupal and DS as havabeer.

arcane’s picture

I'm curious as to why this is a won't fix as this seems to be a fairly common problem.

azinck’s picture

@arcane: I think the problem is caused by the display formatter, not DS. In the case of Term Reference fields I was using Taxonomy Formatter which returned a wrongly-constructed render array: #1352050: Label is always printed, when taxonomy is empty.

What specific display formatter are you seeing this with?

arcane’s picture

I am using a view that is taken over by display suite. The field is a long text type.

arcane’s picture

Never mind, it was a views field setting. Thanks!

a.milkovsky’s picture

I've fixed this problem by altering field output with hook_field_attach_view_alter().

/**
 * Implements hook_field_attach_view_alter().
 * 
 * Hide label of display suite fields if field content is empty.
 */
function example_field_attach_view_alter(&$output, $context) {
  foreach ($output as $field_name => $field) {
    if (!empty($field['#label_display'])) {
      if(isset($field[0]['#markup']) && empty($field[0]['#markup'])) {
        $output[$field_name]['#label_display'] = 'hidden';
      }
    }
  }
}
skdrupal88’s picture

Issue summary: View changes

thanks a.milkovsky it works, but you also need to add condition:

&& ($field[0]['#markup'] != 0)) {

because your code hides labels for fields with value 0.

a.milkovsky’s picture

Sergey, thank you. You're absolutely right.
Here is updated code:

/**
 * Implements hook_field_attach_view_alter().
 *
 * Hide label of display suite fields if field content is empty.
 */
function example_field_attach_view_alter(&$output, $context) {
  foreach ($output as $field_name => $field) {
    if (!empty($field['#label_display'])) {
      if(isset($field[0]['#markup']) && empty($field[0]['#markup']) && ($field[0]['#markup'] != 0)) {
        $output[$field_name]['#label_display'] = 'hidden';
      }
    }
  }
}
hongpong’s picture

also for complex field hiding in DS conditionally this gets the jerb done: https://drupal.org/project/ffc - if field A is empty it can hide B.

hongpong’s picture

/**
 * Implements hook_field_attach_view_alter().
 *
 * Hide label of display suite fields if field content is empty.
 */
function module_field_attach_view_alter(&$output, $context) {
  foreach ($output as $field_name => $field) {
    //    drupal_set_message(t('field is ' . printf($field)), 'error');

    if (!empty($field['#label_display'])) {
      // removes labels of empty-markup regular fields
      if(isset($field[0]['#markup']) && empty($field[0]['#markup']) && ($field[0]['#markup'] != 0)) {
    //drupal_set_message(t('empty label field is ' . printf($field)), 'error');
        $output[$field_name]['#label_display'] = 'hidden';
      }
      // eliminates empty URL linkfields.
      if (isset($field[0]['#element']['url']) && empty($field[0]['#element']['url']) ) {
         //       drupal_set_message(t('url is zero field is ' . printf($field['#title'])), 'error');
        $output[$field_name]['#label_display'] = 'hidden';
      }
      // catches address without thoroughfares (aka no real street address)
      //if ((isset($field['#field_type'])) && ($field['#field_type'] == 'addressfield')) {

      if (isset($field['#field_type']) && ($field['#field_type'] == 'addressfield') && empty($field['#items'][0]['thoroughfare'])) {
        //drupal_set_message(t('addressfield is ' . $field['#title']), 'error');
        //dpm($field);
        $output[$field_name]['#label_display'] = 'hidden';
        //empty($field['#items'][0]['thoroughfare']) {
          //
        }
      

     // if (isset($field[0]['#element']['url'])) { // works
       // drupal_set_message(t('urlfield is ' . $field['#title']), 'error');
      //}

      if (isset($field[0]['#element']['#items'][0]['thoroughfare']) && empty($field[0]['#element']['#items'][0]['thoroughfare']) ) {
        $output[$field_name]['#label_display'] = 'hidden';
    }
  }
    

  }
}

i expanded this logic for addressfields with no thoroughfare field data, and link fields with no URL data. (how annoyingly difficult to nail addressfield labels. anyway)

jim_keller’s picture

#18 needs an update in order to work correctly. The "if $field[0]['#markup'] != 0" statement should be updated to have two equals signs, since a blank string is == 0 in PHP, but not === 0. The if statement evaluates to false on blank strings, which is where we want it to evaluate to true.

updated code is:

/**
 * Implements hook_field_attach_view_alter().
 *
 * Hide label of display suite fields if field content is empty.
 */
function example_field_attach_view_alter(&$output, $context) {

  foreach ($output as $field_name => $field) {
    if (!empty($field['#label_display'])) {
      if(isset($field[0]['#markup']) && empty($field[0]['#markup']) && ($field[0]['#markup'] !== 0)) {
        $output[$field_name]['#label_display'] = 'hidden';
      }
    }
  }
}
a.milkovsky’s picture

thank you, agree with jim_keller

eigentor’s picture

StatusFileSize
new14.5 KB

O.K. I was searching for a solution for field collections. I see this issue is for regular fields.

dremy’s picture

Note this can also be done with EVA (Entity Views Attachment) module by creating a view that shows the fields and their values of the node through an argument. Then you can adjust the rewrite results if empty configuration for the field. Lastly, you need to hide all the fields on the node display that are being now showcased through the view.

2dareis2do’s picture

#20 HongPong works like a charm for me. Many thanks. Maybe there should be a module for this?

kristindev’s picture

Anyone got a fix for this for D8?