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

VM’s picture

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.

zenimagine’s picture

@VM I'm not sure it works, I want to target the status. I have updated my question.

VM’s picture

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...

zenimagine’s picture

If I understood correctly, it is not possible to exclude part of the TWIG code from the cache?

VM’s picture

I don't see a way of handling 'part of the code' within a single file.

zenimagine’s picture

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

VM’s picture