As in the subject: how can I hide entire field (including its label) if the view produces null output, please?

CommentFileSizeAuthor
#7 1416818-7.patch1.96 KBjelle_s

Comments

colle901’s picture

Component: Documentation » Code
Category: support » bug

I need this as well. Panels will not hide the field because the field is returned with markup (empty divs) even when the view result is empty. I would consider this to be a bug unless there is a workaround I am not aware of.

mattcasey’s picture

Did you try copying the tpl.php for your field and modifying it in your theme? In D6, you could get a list of templates w/source code by clicking "Information" under Style Settings...also in D6 there was an option on each field to "hide if empty"

kansaj’s picture

I think the problem has been solved for version 6, but 7 still have the problem.

fraweg’s picture

Hello,
I have the same problem. Did anyone solve this issue?

Best regards
Frank

Edit: Is this project still alive?

moniuch’s picture

Count me in. And I second the question: is there anybody home?

keithm’s picture

Category: bug » support

This is a theming question that could apply to any field, not just viewfield. One approach is to override theme_field() and note if Views produced any output. For example, you could use the following in your theme's template.php file:

function MYTHEME_field($variables) {
  $output = '';

  // Theme viewfields specially.
  if ($variables['element']['#field_type'] === 'viewfield') {
    $items_produced_output = FALSE;
    
    // Render the items.
    $output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
    foreach ($variables['items'] as $delta => $item) {
      $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
      $item_output = drupal_render($item);
      if (!empty($item_output)) {
        $items_produced_output = TRUE;
      }
      $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . $item_output . '</div>';
    }
    $output .= '</div>';

    // Theme normally if item rendering produced some output.
    if ($items_produced_output) {
      // Render the label, if it's not hidden.
      if (!$variables['label_hidden']) {
        $output = '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>' . $output;
      }

      // Render the top-level DIV.
      $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';
    }
    // Output nothing at all if items produced no output.
    else {
      $output = '';
    }
  }
  // Theme non-viewfields using default theme function.
  else {
    $output = theme_field($variables);
  }

  return $output;
}
jelle_s’s picture

Status: Active » Needs review
StatusFileSize
new1.96 KB

This patch solved the issue without having to override theme_field (which is a bit drastic)

attiks’s picture

Status: Needs review » Reviewed & tested by the community

works for me after clearing the cache

attiks’s picture

Status: Reviewed & tested by the community » Needs work

Back to NW, I have a view for openlayers that isn't showing anymore, after reverting the patch it's back again.

juves’s picture

#7 doesn't work for me, I'm still having an empty view-wrapper.

lolmaus’s picture

Category: support » bug

When a normal field has no data, its wrapping markup and title are not displayed. So marking this back a bug.

leex’s picture

#7 didn't work for me.

merilainen’s picture

For me #6 works, but it's not ideal solution.

fraweg’s picture

Thanks sooo much Jelle_S !

After clearing the cache #7 works vor me.

Best regards
Frank

Edit: I crow too soon...Today I see that this issue already there :-( I don't know why...

Intead I want to use the solution ftom #6 but I get this error:

Parse error: syntax error, unexpected T_VARIABLE in /.../sites/all/themes/andromeda/template.php on line 214

Has someone a Idea what to do?

Best regards
Frank

SilviuChingaru’s picture

Status: Needs work » Closed (duplicate)

This is not a theme issue. Viewfield is prepared for viewing using field API in a wrong way: An empty field should have an empty array of items. This field is not checking on the correct hook hook_field_prepare_view if it is empty or not and the field api is trying to theme an empty field and put the divs arround it because it is told to do so.

@see file.inc in core field module for right approch on empty field: It checks in above hook if a file should not be displayed and is working correctly.

Fixed here: https://drupal.org/node/477244#comment-7510777

SilviuChingaru’s picture

This is not a theme issue. Viewfield is prepared for viewing using field API in a wrong way: An empty field should have an empty array of items. This field is not checking on the correct hook hook_field_prepare_view if it is empty or not and the field api is trying to theme an empty field and put the divs arround it because it is told to do so.

@see file.inc in core field module for right approch on empty field: It checks in above hook if a file should not be displayed and is working correctly.

Fixed here: https://drupal.org/node/477244#comment-7510777