By default statistics.module has node visit counting disabled, and if you use the [node:last-view] token, we're sending a FALSE value into format_date() which causes these errors:

Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date() (line 1896 of /Users/davereid/Dropbox/Projects/drupal7dev/includes/common.inc).
Warning: date_format() expects parameter 1 to be DateTime, boolean given in format_date() (line 1906 of /Users/davereid/Dropbox/Projects/drupal7dev/includes/common.inc).

Also, if a node hasn't been viewed yet, we're also returning no value for [node:day-count] and [node:total-count], when we should actually return '0', just like we do with [node:comment-count] when a node does not have any comments yet.

Comments

dave reid’s picture

Status: Active » Needs review
StatusFileSize
new4.31 KB
pancho’s picture

Same applies and needs to be checked for chained tokens.

Also:
- doing a statistics_get() once is enough
- $language_code is needed, but only in one of the cases, so don't assign it otherwise

Also, the token output is still not consistent. We need to decide if we want to return NULL or 0 in case there is no statistics for the node.
The former would mean not to return 0 as a default. The latter would mean we'd also return format_date(0) as replacement for 'last-view'.
I decided for the former, which also means if statistics_get() is empty we don't need to proceed at all.

Status: Needs review » Needs work

The last submitted patch, 930574_statistics_tokens_cleanup_D7_2.patch, failed testing.

dave reid’s picture

Status: Needs work » Needs review
StatusFileSize
new3.64 KB

I disagree. If the node has no views, total-count and day-count should return '0'. Other changes (calling statistics_get() before we know the user wants any statistic-related tokens, moving definition of $language_code) are not consistent with our use of the token API in other core modules. Revised patch adds static caching to statistics_get() so it won't hurt calling it more than once.

dave reid’s picture

StatusFileSize
new5.46 KB

With the test changes this time.

dave reid’s picture

StatusFileSize
new5.4 KB

Actually we don't have to do a double check if statistics_get() returns a non-FALSE value and if $statistics['timestamp'] is not empty because there will not be any record that exists if the node hasn't been viewed.

Status: Needs review » Needs work

The last submitted patch, 930574-statistics-tokens-cleanup.patch, failed testing.

pillarsdotnet’s picture

Status: Needs work » Needs review
StatusFileSize
new977 bytes

Another strategy would be to replace invalid timezones with the default timezone in the format_date() function.

(I'm getting exactly the same error with the statistics module disabled, after using devel generate to create random nodes.)

dave reid’s picture

Status: Needs review » Needs work

#8 is not the correct approach as fixing it there would no longer point out failures that are happening higher up like this token issue that need to be addressed.

pillarsdotnet’s picture

#8 is not the correct approach as fixing it there would point out failures that are happening higher up like this token issue that need to be addressed.

Some day when you have more than thirty seconds available to compose a reply, could you do so in a way that makes sense to ignorant non-telepaths such as myself?

dave reid’s picture

Ok - #8 is the wrong approach. If you are seeing this error it means something *else* is going wrong and needs to be fixed. Likely with devel_generate. So please don't hijack this issue to fix the node statistics tokens.

pillarsdotnet’s picture

If you are seeing this error it means something *else* is going wrong and needs to be fixed. Likely with devel_generate.

Thank you. That made a whole lot more sense to me.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.