We are attempting to show a users first name on a profile menu for authenticated users. We added a lazy builder to the hook_preprocess_page
hook in our theme given this is highly dynamic content on heavily cached pages.
if ($account->isAuthenticated()) {
$vars['current_user_first_name'] = [
'#create_placeholder' => true,
'#lazy_builder' => [
'\Drupal\ourtheme\OurThemeLazyBuilders::getUserFirstName',
[$account->id()]
]
];
}
With this callback (inside of a class that implements RenderCallbackInterface
)
public static function getUserFirstName($account_id) {
$user = User::load($account_id);
return [
'#plain_text' => $user->field_first_name->value,
'#cache' => [
'max-age' => 0
],
];
}
However, when two of us (both administrator
accounts) log in, whoever logs in second will see the other persons first name. We have verified the caching issue by setting a breakpoint in the hook_preprocess_page
hook and see that the breakpoint never gets hit for the second users request. We tried moving the lazybuilder into it's own block and placing that block in a region of our theme, this didn't seem to make a difference, we saw the same caching behavior.
Interestingly, this only seems to happen when the users have the administrator
role, we have thousands of other active users who only have authenticated users
role and we've not heard anything about this issue from them (we would have heard immediately if there was an issue for general users).
Is there a way to debug the caching system in Drupal to know what parts of a page are cached by the dynamic cache and/or page cache?