I'm using Display Suite fields on a view.
I'm configuring my display suite view on 2 cols. In left, i have an image field and on 2nd, body and title :
LEFT | RIGHT
image title
body

On my view, on image field definition, i check 'Hide if empty' and 'Hide rewriting if empty'. I haven't label and doesn't display label for this field. I also check on display suite 'Hide empty regions'.

But the result prints an empty markup :

<div class="group-left">
  <div class="views-field-field_logo">
    <div class="field-label"></div>
  </div>
</div>

I fix this bug on views_plugin_ds_fields_view.inc l.97 :

      // Create the region content.
      $region_content = '';
      if (isset($layout['settings']['regions'][$region_name])) {
        foreach ($layout['settings']['regions'][$region_name] as $key => $field) {
          if(!empty($view->style_plugin->rendered_fields[$row_index][$field]) || !$view->field[$field]->options['hide_alter_empty']) {
            $region_content .= '<div class="views-field-' . $field . '">';
            if (isset($view->field[$field]->options['label']) && $view->field[$field]->options['hide_alter_empty']) {
              $region_content .= '<div class="field-label">';
              $region_content .= check_plain($view->field[$field]->options['label']);
              if ($view->field[$field]->options['element_label_colon']) {
                $region_content .= ':';
              }
              $region_content .= '</div>';
            }
            $region_content .= $view->style_plugin->rendered_fields[$row_index][$field];
            $region_content .= '</div>';
          }
        }
      }
      $vars[$region_name] = $region_content;

With this fix, i have no markup and so no region if my field is empty.

Patch on next comment

Comments

goz’s picture

Status: Active » Fixed

Shame on me, it's already in 7.x-1.x-dev

if (isset($layout['settings']['regions'][$region_name])) {
        foreach ($layout['settings']['regions'][$region_name] as $key => $field) {
          // Do not render the field when empty and configured to be hidden.
          if ($view->field[$field]->options['hide_empty'] && empty($view->style_plugin->rendered_fields[$row_index][$field])) {
            continue;
          }
          $region_content .= '<div class="views-field-' . $field . '">';
          if (isset($view->field[$field]->options['label'])) {
            $region_content .= '<div class="field-label">';
            $region_content .= check_plain($view->field[$field]->options['label']);
            if ($view->field[$field]->options['element_label_colon']) {
              $region_content .= ':';
            }
            $region_content .= '</div>';
          }
          $region_content .= $view->style_plugin->rendered_fields[$row_index][$field];
          $region_content .= '</div>';
        }
      }

Sorry for that

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

kclarkson’s picture

Title: Do not display empty fields view and so not display empty regions » DS Region is still being rendered when Empty
Version: 7.x-1.4 » 7.x-1.x-dev
Status: Closed (fixed) » Active

I have a similar display at which I am using a custom 2 - column display tpl.php. I changed the % width's to 65% and 35% in the css.

In the settings I checked "hide region when empty". So in my view I have content where some have images and some do not but the regions always show leaving a blank space when a piece of content does not have images.

sonar_un’s picture

I am having the same problem as k-clarkson, using a custom ds display and specifically trying to display images..

The region continues to render even if I have the "hide region when empty" clicked and I am on the latest -dev version.

dufferin’s picture

Version: 7.x-1.x-dev » 7.x-1.5

I have no problem in 'full content' view mode, 'hide empty region' works fine,
though doesn't give any result in 'Teaser' view mode.

DanielFriis’s picture

Have you guys checked if you're using any other modules that rewrites the output? I had this issue when using the module DownloadFile in custom layout. Either changing to regular Full content or setting the file rendering to generic file fixed it for me.

swentel’s picture

Status: Active » Postponed (maintainer needs more info)

As DanielFriis points out, check if there is any data from that field. It's possible fields return empty wrappers. If so, in that case, there's nothing that I can do.

swentel’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

In fact, closing this for now. If there's a region that's still there, it means there's output, even it that means there are empty wrappers. in that case, it's another module.

thoughtcat’s picture

Status: Closed (fixed) » Active

Hope you don't mind me reopening this, but I'm having the same problem. I've got an optional text field in which the user pastes YouTube embed code. After quite a lot of trial and error with filters and tokens I've got the videos to display in the node where there is one using this:

<p><iframe align="left" frameborder="0" height="335" scrolling="no" src="[node:field_youtube2]" width="450"></iframe></p>

But where there is no value for the field I get a large white space in place of the video. I do have "hide empty regions" checked for the full display and I'm afraid I don't know what else could be outputting the space, or how to find this out.

I tried solving it with an attempt at conditional php:

$field = field_get_items('node', $node, 'field_youtube2');
if($field){
print <p><iframe align="left" frameborder="0" height="335" scrolling="no" src="[node:field_youtube2]" width="450"></iframe></p>
}else{
     //do nothing
}

But this doesn't display anything whether there is a value in the field or not. I'd be very grateful for any suggestions.

swentel’s picture

Status: Active » Closed (fixed)

The token won't be evaluated inside a php print statement. What you'll need todo is something like this:

$field = field_get_items('node', $entity, 'field_youtube2');
if (isset($field[0]) {
  print $output = field_view_field('node', $entity, 'field_name', $field[0]);
}

There's a good article re: field_get_items and field_view_field at http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

thoughtcat’s picture

Thanks again Swentel, unfortunately it didn't work - I initially got the same as before (the field didn't display whether there was any content in it or not) - then after some fiddling I managed to get it to output the raw user input but not display the actual video. Guess I'll have to keep looking! Cheers :)

bloomt’s picture

Is there a way to hide the display suite regon on drupal 8 if the field is empty?

finex’s picture

Hi, the bug is still valid on D8. Do you need a separate bug report?

aspilicious’s picture

What version of DS are you running? and which template? Yes please open a new report.

finex’s picture

@spilicious: hi, I'm using DS 8.x-2.5 with ds-1col template and Drupal 8.1.7. I've opened a new bug report: https://www.drupal.org/node/2779437