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.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | format_date-ensure_valid_timezone-930574-8.patch | 977 bytes | pillarsdotnet |
| #6 | 930574-statistics-tokens-cleanup.patch | 5.4 KB | dave reid |
| #5 | 930574-statistics-tokens-cleanup.patch | 5.46 KB | dave reid |
| #4 | 930574-statistics-tokens-cleanup-D7.patch | 3.64 KB | dave reid |
| #2 | 930574_statistics_tokens_cleanup_D7_2.patch | 5.11 KB | pancho |
Comments
Comment #1
dave reidComment #2
panchoSame 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.
Comment #4
dave reidI 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.
Comment #5
dave reidWith the test changes this time.
Comment #6
dave reidActually 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.
Comment #8
pillarsdotnet commentedAnother 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.)
Comment #9
dave reid#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.
Comment #10
pillarsdotnet commentedSome 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?
Comment #11
dave reidOk - #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.
Comment #12
pillarsdotnet commentedThank you. That made a whole lot more sense to me.