Bug #23545 Bug Fixed

Bug in parse_channel_entries when no results are found

Version: 3.5.14 Reporter: Brian Litzinger

This is an archived bug report. If you are experiencing a similar issue, upgrade to the latest release and if that does not solve the problem, submit a new bug report

I found a bug where the no_results conditional pair would not return its contents when parsing the template. I have a scenario where channel:entries finds a single entry, then the channel_entries_query_result hook is called, which then returns no results b/c its filtering the queries. When this happens, the no_results tag does not work. Fix is below. I also checked out EE4’s code and it appears to be doing the same thing.

public function no_results()
    {
     if ( ! preg_match("/".LD."redirect\s*=\s*(\042|\047)([^\\1]*?)\\1".RD."/si", $this->no_results, $match))
     {
      $this->log_item("Returning No Results Content");
      return $this->no_results;

The channel->entries() method calls $this->parse_channel_entries() on line 297 (ee3), and parse_channel_entries() calls the channel_entries_query_result hook, and if that the $query_result is empty, it then sets $this->return_data = ee()->TMPL->no_results();

public function parse_channel_entries($per_row_callback = NULL)
    {
     // For our hook to work, we need to grab the result array
     $query_result = $this->query->result_array();

     // Ditch everything else
     $this->query->free_result();
     unset($this->query);

     // -------------------------------------------
     // 'channel_entries_query_result' hook.
     //  - Take the whole query result array, do what you wish
     //  - added 1.6.7
     //
      if (ee()->extensions->active_hook('channel_entries_query_result') === TRUE)
      {
       $query_result = ee()->extensions->call('channel_entries_query_result', $this, $query_result);
       if (ee()->extensions->end_script === TRUE) return ee()->TMPL->tagdata;
      }
     //
     // -------------------------------------------

     if (empty($query_result))
     {
                    return ee()->TMPL->no_results();
     }

But since parse_channel_entries() is returning, its not doing anything that value returned:

// Calling the function, but not doing anything with the return value.
$this->parse_channel_entries();

     $this->track_views();

     if ($this->enable['pagination'] == TRUE)
     {
      $this->return_data = $this->pagination->render($this->return_data);
     }

     return $this->return_data;

The fix is to set $this->return_data = ee()->TMPL->no_results(), then return:

public function parse_channel_entries($per_row_callback = NULL)
    {
     // For our hook to work, we need to grab the result array
     $query_result = $this->query->result_array();

     // Ditch everything else
     $this->query->free_result();
     unset($this->query);

     // -------------------------------------------
     // 'channel_entries_query_result' hook.
     //  - Take the whole query result array, do what you wish
     //  - added 1.6.7
     //
      if (ee()->extensions->active_hook('channel_entries_query_result') === TRUE)
      {
       $query_result = ee()->extensions->call('channel_entries_query_result', $this, $query_result);
       if (ee()->extensions->end_script === TRUE) return ee()->TMPL->tagdata;
      }
     //
     // -------------------------------------------

     if (empty($query_result))
     {
            $this->return_data = ee()->TMPL->no_results();
            return;
     }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases