I'm posting here first in case I was doing something wrong, but I believe it's a bug. I didn't see another related issue in the queue.
I created a block view to pull out various fields from the node if the current page is a node. I am under the impression that if it doesn't find anything, it should be blank and therefore not display the block.
However, if the field returns empty, the view/block code still outputs html code wrappers around the fields. I am using attachments/upload field for this example, but have tried various other fields in other modules with the same result. This works as expected otherwise.
<div class="view-content">
<div class="views-field-upload-fid">
<span class="field-content"></span>
</div>
</div>
Any ideas? A bug or something I set incorrectly?
The view code is as follows:
$view = new view;
$view->name = 'test_empty_block';
$view->description = 'Testing an empty block';
$view->tag = 'links test';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('fields', array(
'upload_fid' => array(
'label' => '',
'type' => 'separator',
'separator' => ', ',
'empty' => '',
'link_to_file' => 0,
'only_listed' => 0,
'exclude' => 0,
'id' => 'upload_fid',
'table' => 'node',
'field' => 'upload_fid',
'relationship' => 'none',
),
));
$handler->override_option('arguments', array(
'nid' => array(
'default_action' => 'default',
'style_plugin' => 'default_summary',
'style_options' => array(),
'wildcard' => 'all',
'wildcard_substitution' => 'All',
'title' => '',
'default_argument_type' => 'node',
'default_argument' => '',
'validate_type' => 'none',
'validate_fail' => 'not found',
'break_phrase' => 0,
'not' => 0,
'id' => 'nid',
'table' => 'node',
'field' => 'nid',
'override' => array(
'button' => 'Override',
),
'relationship' => 'none',
'default_options_div_prefix' => '',
'default_argument_user' => 0,
'default_argument_fixed' => '453',
'default_argument_php' => 'if ($view->build_type == \'block\' && arg(0) == \'node\' && is_numeric(arg(1))) {
//$node=node_load(arg(1));
//$args[0] = $node->uid;
$args[0] = arg(1);
}
return $args;',
'validate_argument_node_type' => array(
'audio' => 0,
'event_college' => 0,
'page' => 0,
'pagecareers' => 0,
'pagedevelopment' => 0,
'pageresearch' => 0,
'pagestudyabroad' => 0,
'pageugs' => 0,
'service' => 0,
'story' => 0,
),
'validate_argument_node_access' => 0,
'validate_argument_nid_type' => 'nid',
'validate_argument_vocabulary' => array(
'2' => 0,
'3' => 0,
'1' => 0,
),
'validate_argument_type' => 'tid',
'validate_argument_php' => '',
),
));
$handler->override_option('access', array(
'type' => 'none',
'role' => array(),
'perm' => '',
));
$handler->override_option('title', 'Images (TESTING)');
$handler->override_option('empty_format', '1');
$handler->override_option('items_per_page', 20);
$handler->override_option('row_options', array(
'inline' => array(),
'separator' => '',
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', 'TESTING block - related image');
Comments
Comment #1
merlinofchaos commentedHmm. When I import this view it works as expected; the block only appears on node pages. I'm not sure if this is because there was a bug that was fixed since the rc or not. Maybe try the dev version (go to view all releases and search for 6.x-2.x-dev) and see if the behavior is different there.
Comment #2
dirtabulous commentedThe block only appearing on the node pages isn't the issue. The issue is that the block is appearing even if there is NO content returned.
Blocks that return empty, will not be displayed. In some cases a node may not have an attachment. If this is the case, this block should return empty and the block should not be displayed. However, with the current views, if a field returns empty, it still returns div tags, but really should output nothing.
In this particular case if the field is empty, it's still returning:
A valid case would have something between the span tags.
Am I doing something wrong, or is this a bug? I checked and that this type of thing worked in veiws1/drupal5.
I just tried this with the 6.x-2.x-dev release and get the same issue.
Thanks.
Comment #3
merlinofchaos commentedAHH. I understand; I misinterpreted your initial request.
Unfortunately, your assumption is mistaken.
Simply because a *field* is empty does not mean that the view is considered empty; it still picked up a record. Views does not check every field to see if all of the fields have data; if a *record was returned* then there is data, and the data will all attempt to display.
However, if you add the "Upload: Has Attached Files" filter, then nodes that do not have files attached will not have records, and no block will display.
Comment #4
dirtabulous commentedThanks a bunch. That did the trick. Lesson learned.
I know I've had these working before in my d5 sites. Maybe it's the new views interface that did it :)
Comment #5
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #6
darkodev commentedI know this is an old post, but perhaps merlin could comment on cases where filters are difficult (at least for me) to implement, like with nodereferences.
Trying to set a filter in Views 2: where nodereference cck field is not null
ie. I only want to pull audio nodes nodereferenced by artist nodes (without the filter, I'm getting a bunch of empty div's, and the block displays if no records match my criteria).
The views filter for the cck nodereference field asks me to select all of them for the "Is one of" but new ones will be added all the time.
Ideas very much appreciated.
Thanks for your crazy powerful module.