Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Message Error: Call to a member function getTotalCount() on bool in statistics_tokens() (line 47 of core/modules/statistics/statistics.tokens.inc)
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#22 | interdiff_18-20.txt | 1.63 KB | WalkingDexter |
#20 | 3190820-20.patch | 4 KB | WalkingDexter |
Comments
Comment #2
daffie CreditAttribution: daffie commented@pavnish: Could you tell a bit more how you got the error message. Helps with understanding how you got this error and how to fix it. Could you tell why you think this issue is "critical". See: https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-....
Comment #3
pavnish CreditAttribution: pavnish as a volunteer and at Srijan | A Material+ Company for Drupal India Association commented@daffie Hi ,
We are using token on Node detail ,This issue comes when we create a node and redirect to node detail page initially there is zero count for the node.
I think we need to be more take care of these line of code:
if ($name == 'total-count') {
$replacements[$original] = $stats_storage->fetchView($node->id())->getTotalCount();
}
elseif ($name == 'day-count') {
$replacements[$original] = $stats_storage->fetchView($node->id())->getDayCount();
}
elseif ($name == 'last-view') {
$replacements[$original] = \Drupal::service('date.formatter')->format($stats_storage->fetchView($node->id())->getTimestamp());
}
@daffie Can you please suggest we can above code like this.
$statistics = $stats_storage->fetchView($node->id());
if ($name == 'total-count') {
if (($statistics instanceof StatisticsViewsResult)) {
$replacements[$original] = $stats_storage->fetchView($node->id())->getTotalCount();
}else{
$replacements[$original] = 0;
}
Comment #4
pavnish CreditAttribution: pavnish as a volunteer commentedComment #5
daffie CreditAttribution: daffie commentedWhen I take a look at the interface of
Drupal\statistics\StatisticsStorageInterface
is see that the methodfetchView()
returns the expected result of FALSE:My suggestion would be to test the return value and only then call
getTotalCount()
. Just like you are suggestion in comment #2.Comment #6
durgeshs CreditAttribution: durgeshs as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedUploaded patch for this issue, @pavnish please review.
Comment #7
pavnish CreditAttribution: pavnish as a volunteer commentedComment #8
daffie CreditAttribution: daffie commentedI do not like the patch from comment #6. The method
fetchView()
is called twice and thefetchView()
callsfetchViews()
. ThefetchViews()
runs every time it gets called a database query. I think that it is better to store the result offetchView()
is a variable and then test the value of that variable.Comment #9
pavnish CreditAttribution: pavnish as a volunteer commentedWorking on it so I am assigning this to myself.
Comment #10
pavnish CreditAttribution: pavnish as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedHi @daffie Thanks for the valuable feedback
The changes as you suggested in #8 have been addressed in this patch.
I would be very thankful if you can review this patch.
Comment #11
daffie CreditAttribution: daffie commented@pavnish: That is better, only now will the view be loaded every time there are tokens when the type is 'node' even when there are no tokens with the name: 'total-count', 'day-count' or 'last-view'. Therefore the view will be loaded lots of times when it is not necessary. The code change I was looking for is something like:
The same for 'day-count' and 'last-view'. Also, we need testing for this change.
Comment #12
pavnish CreditAttribution: pavnish as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedWorking on #11
Comment #13
pavnish CreditAttribution: pavnish as a volunteer and at Srijan | A Material+ Company for Drupal India Association commented@daffie #11 has been addressed in this patch.
Comment #14
daffie CreditAttribution: daffie commentedNow we only need the testing to be added.
Comment #15
daffie CreditAttribution: daffie commentedThis change does not help fixing the bug, therefore it can be removed.
Comment #16
ayushmishra206 CreditAttribution: ayushmishra206 at OpenSense Labs commentedReverted the change suggested in #15. Please review.
Comment #17
daffie CreditAttribution: daffie commentedNow we only need the testing to be added.
Comment #18
WalkingDexter CreditAttribution: WalkingDexter at Initlab commentedAdded tests. I think we should also fix the error for the 'last-view' token.
Comment #19
daffie CreditAttribution: daffie commented@WalkingDexter: Thak you for adding the tests!
The patch is for me almost RTBC.
I like this change. Now the view gets only loaded once.
There is no testing for this change.
Comment #20
WalkingDexter CreditAttribution: WalkingDexter at Initlab commentedImproved tests.
Comment #21
daffie CreditAttribution: daffie commented@walkingdexter: Could you create an interdiff file. It makes reviewing a lot easier. See: https://www.drupal.org/docs/develop/git/using-git-to-contribute-to-drupa...
Comment #22
WalkingDexter CreditAttribution: WalkingDexter at Initlab commentedSure.
Comment #24
WalkingDexter CreditAttribution: WalkingDexter at Initlab commentedHmm...strange error. Running a new test.
Comment #25
WalkingDexter CreditAttribution: WalkingDexter at Initlab commentedThe test was successful, that's better :)
Comment #26
daffie CreditAttribution: daffie commentedThe bug is fixed in the patch.
There is also testing for the fix.
All code changes look good to me.
For me it is RTBC.
@walkingdexter: Thank you for working on the patch!
Comment #29
catchCommitted/pushed to 9.3.x, cherry-picked to 9.2.x and 9.1.x, thanks!