#768060: Better empty 'field' replacement allows a field replacement token to be used as the field empty text. (Note: a patch was already committed for the issue, despite its status as RTBC; there is a second patch that fixes a different bug. This is an issue with the patch that has already been committed, not the one that is RTBC, and so I have opened a second issue so as not to interfere with that second patch getting committed.)

There is a problem when the user configures a field thus:
Empty text: [fieldname]
[ x ] Count the number zero as empty
[ x ] Hide if empty

Expected behavior: When the field has a value of zero, its text is replaced with the text for [fieldname].

Actual behavior: When the field has a value of zero, nothing is displayed.

(Note that if Hide if empty is unchecked, then the field displays the rendered value for zero rather than using the empty text replacement. E.g., for a time: if the value is zero, '0:00' will be displayed instead of the empty text replacement.)

The cause is in views_handler_field::render_text():

  function render_text($alter) {
    $value = trim($this->last_render);
    if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
      return '';                                                              
    }

    if (!empty($alter['alter_text']) && $alter['text'] !== '') {
      $tokens = $this->get_render_tokens($alter);
      $value = $this->render_altered($alter, $tokens);
    }

The problem is that an empty string is being returned for a value of zero before the replacement is checked. The solution is to switch the order around so that $alter is checked first.

CommentFileSizeAuthor
#1 936828.patch812 bytesxjm
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xjm’s picture

Status: Active » Needs review
FileSize
812 bytes

Patch attached.

bojanz’s picture

Status: Needs review » Reviewed & tested by the community

Now that's a well explained bug report & fix.

merlinofchaos’s picture

Status: Reviewed & tested by the community » Fixed

Committed to all branches.

Status: Fixed » Closed (fixed)

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