Problem/Motivation

EntityViewBuilder gets the translated entity (by calling EntityManager::getTranslationFromContext(), which uses the negotiated content language) and then renders it, but does not specify the 'languages:' . LanguageInterface::TYPE_CONTENT cache context.

(Discovered in #2429617-135: Make D8 2x as fast: Dynamic Page Cache: context-dependent page caching (for *all* users!).)

Proposed resolution

Fix it.

Remaining tasks

Fix test failures.

User interface changes

None.

API changes

None.

Data model changes

None.

CommentFileSizeAuthor
#1 2512712-1.patch868 byteswim leers

Comments

wim leers’s picture

Title: EntityViewBuilder » Entities rendered by EntityViewBuilder should vary by content language cache context
Status: Active » Needs review
StatusFileSize
new868 bytes

Status: Needs review » Needs work

The last submitted patch, 1: 2512712-1.patch, failed testing.

wim leers’s picture

Should be as simple as adding the 'languages:' . LanguageInterface::TYPE_CONTENT cache context to the failing tests' expectations.

wim leers’s picture

Issue tags: +php-novice
berdir’s picture

As just discussed with Wim, it's not always the content language that matters here, *only* when langcode is NULL.

We already cache the entity per $language which might or might not depend on the content language. We only know it does in the case where $langcode = NULL, so if we run that code, we need to add the cache context. But not if it langcode is already set, which could for example happen when displaying a list of nodes in their default language.

wim leers’s picture

  public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = array()) {
    $translation = $entity;

    if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
      if (empty($langcode)) {
        $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
      }

So only in that deepest nested "if", ONLY THEN, we need to add the content language cache context!