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 zenimagine on
The following code is used to display whether the users are online, absent or offline.
My problem is that the current date "timestamp" is cached, so there is no change in user status unless I manually clear the cache with "drush cr".
How can I fix it ?
user--full.html.twig :
{% if (date().timestamp - user.access.value) < 900 %}
<div class="mt-2 text-center font-weight-bold font-italic text-success">
<i class="fa fa-circle fa-lg"></i> Online
</div>
{% elseif (date().timestamp - user.access.value) < 1800 %}
<div class="mt-2 text-center font-weight-bold font-italic text-warning">
<i class="fa fa-circle fa-lg"></i> Absent
</div>
{% else %}
<div class="mt-2 text-center font-weight-bold font-italic text-danger">
<i class="fa fa-circle fa-lg"></i> Offline
</div>
{% endif %}
I found this code, but how do I use it with the user template to target only the user status (the code above) ?
bootstrap_subtheme_front_office_old.theme :
/**
* User online status.
*/
function bootstrap_subtheme_front_office_old_preprocess_block(&$variables) {
if ($variables['plugin_id'] == 'block_id') {
$variables['#cache']['max-age'] = 0;
}
}
Comments
=-=
unsure it can be accomplished on a 'part of the theme' via the theme files. https://www.drupal.org/forum/support/theme-development/2016-05-21/disable-twig-template-cache toward the end of the post is a suggestion for a setting within the file cache: false which may aid.
@VM I'm not sure it works, I
@VM I'm not sure it works, I want to target the status. I have updated my question.
=-=
IIRC preprocesses go into your mytheme.theme file. If you don't have one, you'd need to create it. Taken from https://www.drupal.org/docs/8/theming-drupal-8/modifying-attributes-in-a...
If I understood correctly, it
If I understood correctly, it is not possible to exclude part of the TWIG code from the cache?
=-=
I don't see a way of handling 'part of the code' within a single file.
I have a site like a social
I have a site like a social network, with regularly updated content. Should I activate the cache or uninstall the kernel cache module? What is Bigpipe for? Thank you
=-=
https://www.drupal.org/docs/8/core/modules/big-pipe/overview - explains bagpipe and dynamic cache
https://www.qed42.com/blog/lazy-builders-drupal-8-caching-ftw may be more helpful.