Create a content type, let's call it "product". Add a float field called "Price" and give that field a prefix of "$". Create a view to display a list of products, and for the Price field, for No Results Behavior, tell it to print "Unavailable" or something similar, and to count 0 as empty. Also for the price field, tell it to display prefixes and suffixes for that field.

So, if you created a product with a price of 0, it should print as "Unavailable" within your view. However, because you want to display the dollar sign ($) when the price isn't 0, you have the prefixes and suffixes turned on. So the products with a price of 0 will print as "$0", rather than deciding that the "0" makes it empty and printing "Unavailable".

So the logic, as it stands, goes:
Get field x. Concatenate prefix/suffix. Is x empty and/or 0? If yes, print no results behavior. If no, print x.

My proposal is:
Get field x. Is x empty and/or 0? If yes, print no results behavior. If no, concatenate prefix/suffix and print x.

At the very least, there should be an option to choose which way it works, but since a field will never be empty if it has a prefix/suffix attached before checking, it doesn't make much/any sense (to me, at least) to check after the concatenation, except in rare cases (but that's what an option would be for).

My testing indicates that empty fields rather than fields with 0 in them also do this, and that suffixes as well as prefixes can cause it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

Can you please show your configuration of your view?

There are two places where you can add suffixes/prefixes: directly on the field/in views.

if you use the prefix/suffix of views it should work already.

alexdmccabe’s picture

FileSize
120.99 KB

I'm not sure what you mean by "The prefix/suffix of views", unless you mean the "Use Prefix/Suffix" checkbox in the field in views, which merely appends the prefix/suffix that you set on the field in the Structure->Content Types settings. The only other option I can think of that you mean is rewriting the field with itself and something in front of it (like $[field_price]), which also doesn't work, and is kind of silly to need to do anyway, since that's just duplicating the prefix functionality.

I've attached a series of images (in a .zip for convenience, although I could upload them individually if that's the standard, just not sure) that chronicle these steps:

001: The price field has been added to the Product type. Minimum and default 0, and the prefix is NOT set.
002: An overview of the fields that the Product content type has. Nothing fancy.
003: The Price field in the view. "Display prefix and suffix" is notably unchecked. No results text also displays as "Unavailable", with "0" counting as empty.
004: A product I created called "Hat" that has a price of 0. I also created two others, not pictured here, that have non-zero prices, called "TV" and "Game".
005: The Views preview of the results thus far. Close, but I'd like a dollar sign in front of the prices.
006: The prefix is now set on the Price field of the Product content type. Again, this was done in manage fields. The prefix, for reference, is "$" (not including quotes).
007: As we saw in 003, "display prefix/suffix" is NOT checked. So now, despite being set on the field settings for the content type, it still doesn't show up, because it has not yet been turned on in the view.
008: NOW we turn "display prefix/suffix" on in the view settings.
009: Prices now display with "$" in front - including the price of 0, which (to my mind at least) should count as empty and display "Unavailable".

View query:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, 'node' AS field_data_field_price_node_entity_type
FROM 
{node} node
WHERE (( (node.status = '1') AND (node.type IN  ('product')) ))
ORDER BY node_created DESC
LIMIT 5 OFFSET 0

View code:

$view = new view;
$view->name = 'products';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Products';
$view->core = 7;
$view->api_version = '3.0-alpha1';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Products';
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '5';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['title']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['title']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0;
$handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['title']['alter']['trim'] = 0;
$handler->display->display_options['fields']['title']['alter']['html'] = 0;
$handler->display->display_options['fields']['title']['hide_empty'] = 0;
$handler->display->display_options['fields']['title']['empty_zero'] = 0;
$handler->display->display_options['fields']['title']['link_to_node'] = 1;
/* Field: Content: Price */
$handler->display->display_options['fields']['field_price']['id'] = 'field_price';
$handler->display->display_options['fields']['field_price']['table'] = 'field_data_field_price';
$handler->display->display_options['fields']['field_price']['field'] = 'field_price';
$handler->display->display_options['fields']['field_price']['alter']['alter_text'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['make_link'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['absolute'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['external'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['replace_spaces'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['trim_whitespace'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['nl2br'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['word_boundary'] = 1;
$handler->display->display_options['fields']['field_price']['alter']['ellipsis'] = 1;
$handler->display->display_options['fields']['field_price']['alter']['strip_tags'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['trim'] = 0;
$handler->display->display_options['fields']['field_price']['alter']['html'] = 0;
$handler->display->display_options['fields']['field_price']['element_label_colon'] = 0;
$handler->display->display_options['fields']['field_price']['element_default_classes'] = 1;
$handler->display->display_options['fields']['field_price']['empty'] = 'Unavailable';
$handler->display->display_options['fields']['field_price']['hide_empty'] = 0;
$handler->display->display_options['fields']['field_price']['empty_zero'] = 1;
$handler->display->display_options['fields']['field_price']['hide_alter_empty'] = 0;
$handler->display->display_options['fields']['field_price']['settings'] = array(
  'thousand_separator' => ' ',
  'decimal_separator' => '.',
  'scale' => '0',
  'prefix_suffix' => 1,
);
$handler->display->display_options['fields']['field_price']['field_api_classes'] = 0;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 0;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'product' => 'product',
);

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');
dawehner’s picture

Status: Postponed (maintainer needs more info) » Active

So you have a prefix in fieldapi not in views, that's the harder issue :(

Update status

awdlewis’s picture

If you add the suffix / prefix by rewrriting the result in views ($ or whatever), add the empty text and set to display, then also click hide if empty - this should work, no? At least it does for me in DR6.

dawehner’s picture

@adlw
Yes this should work! To be able to remove the display of the prefix on the field level you might need a formatter which allows to configure this, but then this is a usable solution.

MaienM’s picture

Any updates on this? The issue still exists in the latest version... Being able to set the prefix of a field in the field api is neat, having to set it through views for this to work is... less than ideal.

dawehner’s picture

I'm wondering whether using "is_empty" of fieldapi would solve this solution.

jonathan_hunt’s picture

Format plural interferes with this also. I am displaying comment count field on a view. I had no results behaviour set to show text "No comments" but if I enable format plural, then it looks the value 0 goes through the plural option ("@count comments") and the no results logic fails to fire.

jzornig’s picture

Also suffering from this. I have a count field for number of answers on a question content that I want to display as Unanswered/ 1 Answer / n Answers
depending on the count being 0, 1 or >1. If I use the Suffix or format Plural settings the No results behaviour setting gets ignored. So I can
0 Answers / 1 Answer / n Answers by using the plural settings, or 0 Answers / 1 Answers / n Answers using the Suffix or Unanswered / 1 / n using the No results behaviour, but i can't combine them to get what I want.
The No results behaviour should get tested befor the Plural and Prefix/Suffix processing.

jzornig’s picture

Status: Active » Needs review
FileSize
1.89 KB

I don't think there is a way to process the plural and prefix/suffix settings after the No Results behaviour. So I've created a patch that adds a setting to handle the zero case to the Format plural settings. So for my use case I would fill in the settings as:-
Zero Case: Unanswered
Singular Case: 1 Answer
Plural Case: @count Answers
and not use the No Results Behaviour setting at all.

I hope the maintainers pick up this patch because it seems a useful feature to have.

Status: Needs review » Needs work

The last submitted patch, views_zero_case.patch, failed testing.

jzornig’s picture

Version: 7.x-3.0-rc1 » 7.x-3.x-dev
FileSize
1.89 KB

try again. Patch is against latest dev.

jzornig’s picture

Status: Needs work » Needs review
Ken Hawkins’s picture

Status: Needs review » Needs work

Redacted.

Ken Hawkins’s picture

Status: Needs work » Needs review
FileSize
1.94 KB

Patch in #12 works but results in notice:

Notice: Undefined index: format_plural_zero in views_handler_field_numeric->render() (line 141 of /var/www/sites/all/modules/views/handlers/views_handler_field_numeric.inc).

Patch rerolled with an "isset".

osopolar’s picture

+1 RTBC the solution works for me too. But I also need it for math expressions (in views_handler_field_math.inc).

Status: Needs review » Needs work

The last submitted patch, 16: views_zero_case_1239522-16.patch, failed testing.