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.
Hi there,
i have a custom block extending BlockBase that displays the username added to the navbar with bootstrap theme.
public function build() {
$currentUser = \Drupal::currentUser();
return array(
'usersNumber' => $currentUser->getAccountName(),
);
}
HTML output if this is:
<p class="name navbar-text">david</p>
and it sits nicely next to the Drupal branding.
When i add
public function getCacheContexts() {
return ['user'];
}
to the block code the HTML will change to:
<div>
<p class="name navbar-text">david</p>
</div>
which will place the name under the Drupal branding.
I'd expect it to be the same. Also adding the cache context array in the build-methods render array does not work anymore either, it did however in 8.3.4.
Comments
Comment #2
TrippleDave CreditAttribution: TrippleDave commentedComment #3
BerdirThat's because by adding the user cache context, you trigger big_pipe which will insert that snippet through JS after initial rendering.
The div wrapper is added by the ajax system, which makes this a duplicate of #736066: ajax.js insert command sometimes wraps content in a div, potentially producing invalid HTML and other bugs.