Score:1

Wrong user with currentUser() on prod

kn flag

I'm getting the name of my user that I load from the profile. It's works fine on my local, but on my prod, the firstname displayed is showing the name from the previous user. How do I prevent that ? This is how I get my data.

This is where I don't understand, I'm loading data by current user, but the user isn't the good one...

I have my "new" firstname only after clearing my website cache with drush

/**
 * Implements hook_preprocess_HOOK().
 */
function frr_menu_link_preprocess_menu__account(&$variables)
{
    $current_user = \Drupal::currentUser();
    $variables['firstname'] = FALSE;
    if($current_user->isAuthenticated()) {
        $entity_manager = \Drupal::entityTypeManager();
        $profileStorage = $entity_manager->getStorage('profile');
        $profileCustomerFound = $profileStorage->loadByProperties([
            'uid' => $current_user->id(),
            'type' => 'customer',
            'is_default' => 1,
            'status' => 1,
        ]);
        if($profileCustomerFound > 0) {
            $customer = end($profileCustomerFound);
            $firstname = $customer->get('field_firstname')->value;
            $variables['firstname'] = !empty($firstname) ? ucfirst($firstname) : FALSE;
        }
    }
}
id flag
What do you mean by the "previous" user?
Score:3
id flag

This is likely because you must add cacheability metadata so Drupal knows to cache the content separately by user.

cn flag
Obligatory mention - varying cache per user is not a great idea as the number of cache entries can get large. Lazy builders and placeholders can be used in this sort of scenario. There's a good post with examples here: https://www.qed42.com/insights/coe/drupal/lazy-builders-drupal-8-caching-ftw, and if you search this site for [lazy_builder](https://drupal.stackexchange.com/search?q=lazy_builder) there are plenty of other examples and explanations
kn flag
My website has about 160000 user for now, with the "dev" amount of users I have no problem... So can I add cacheability metadata ?
kn flag
If so, how do I do this ?
4uk4 avatar
cn flag
If you have this amount of users then add `$variables['#cache'] = ['max-age' => 0];` to prevent the menu to be cached for every user visiting the page. In this case a lazy builder is not absolutely necessary because menus are usually rendered in blocks and blocks have already a lazy builder.
4uk4 avatar
cn flag
Although a separate lazy builder only for $variables['firstname'] would be nice. This makes the page load more fluid when the dynamic parts are restricted to the absolute minimum. See https://drupal.stackexchange.com/questions/305479/can-bigpipe-exclude-certain-blocks
kn flag
`$variables['#cache'] = ['max-age' => 0];` worked fine, thanks !
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.