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.
By Mojtaba Reyhani on
How can I show node Hits (statistics_counter) in Drupal 8 in specific place?
I want to show user hits of a node (article) in submitted area rather than the end of a node in node links area? I do this already in drupal 7 with this code:
<?php print (int) $content['links']['statistics']['#links']['statistics_counter']['title']; ?>
in node.tpl.php file
but i want to know how to convert this code to drupal 8 version.
Comments
Drupal 8 uses twig templates
Drupal 8 uses twig templates so variables are handled/printed in different way.
Node links sections is handled separately because of which we can not hide/remove statistics in node.html.twig file. So to print node statistics count in submitted area, you'll first need to add that to base level of node template like content, author_picture, etc. You can do this by implementing template_preprocess_node() in your theme. Below is sample code which adds new variable containing node statistics count.
Change THEME_NAME with theme you're using. This will add node_stats variable which you can use inside node.html.twig file for printing node statistics count. Below is how you can print it inside template file.
This will print the node statistics count in submitted area of content. Links section will still show the node statistics count, to remove this, you will need to create new template file (if not already created) named as links.html.twig. You can copy existing file from "Core => themes => classy => templates => navigation => links.html.twig". After which you can exclude the statistics count from printing. Make sure to rebuild cache after adding new template file. See sample output below:
Statistics count is removed from links while printing using without() function, links|without('statistics_counter')
Yogesh
http://yogeshchaugule.com/blog
\,,/
Good answer, perhaps $statistics should be cached, since it's a db hit every time?
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Thanks very much
Thanks very much for your help, Your answer was correct and complete, and my problem was solved.
Can you help me more, I want to show "Comment Count" also With this technique.
And thank you again for everything.
Need Comment Count
Me too need to display comment count for a particular node. Please, Help me
statistics_get is deprecated
statistics_get is deprecated as of 8.2.x
You should use:
Or even better because the above fails when there is no statistics for the node (yet):