diff --git a/core/lib/Drupal/Core/Cache/RuntimeCacheContextsDependencyInterface.php b/core/lib/Drupal/Core/Cache/RuntimeCacheContextsDependencyInterface.php new file mode 100644 index 0000000..1971d59 --- /dev/null +++ b/core/lib/Drupal/Core/Cache/RuntimeCacheContextsDependencyInterface.php @@ -0,0 +1,27 @@ +isNew()) { + return []; + } return [$this->entityTypeId . ':' . $this->id()]; } diff --git a/core/lib/Drupal/Core/Entity/EntityForm.php b/core/lib/Drupal/Core/Entity/EntityForm.php index e216099..8fca582 100644 --- a/core/lib/Drupal/Core/Entity/EntityForm.php +++ b/core/lib/Drupal/Core/Entity/EntityForm.php @@ -101,9 +101,11 @@ public function buildForm(array $form, FormStateInterface $form_state) { $this->init($form_state); } - // Ensure that forms have the correct cacheable metadata so they can be + // Ensure that edit forms have the correct cacheable metadata so they can be // cached. - \Drupal::service('renderer')->addCacheableDependency($form, $this->entity); + if (!$this->entity->isNew()) { + \Drupal::service('renderer')->addCacheableDependency($form, $this->entity); + } // Retrieve the form array using the possibly updated entity in form state. $form = $this->form($form, $form_state); diff --git a/core/lib/Drupal/Core/Entity/EntityInterface.php b/core/lib/Drupal/Core/Entity/EntityInterface.php index 49c25a2..495ffaa 100644 --- a/core/lib/Drupal/Core/Entity/EntityInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityInterface.php @@ -9,14 +9,14 @@ use Drupal\Core\Access\AccessibleInterface; use Drupal\Core\Cache\CacheableDependencyInterface; -use Drupal\Core\Cache\RuntimeCacheableDependencyInterface; +use Drupal\Core\Cache\RuntimeCacheContextsDependencyInterface; /** * Defines a common interface for all entity objects. * * @ingroup entity_api */ -interface EntityInterface extends AccessibleInterface, CacheableDependencyInterface, RuntimeCacheableDependencyInterface { +interface EntityInterface extends AccessibleInterface, CacheableDependencyInterface, RuntimeCacheContextsDependencyInterface { /** * Gets the entity UUID (Universally Unique Identifier). diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index c0a0443..8281eb9 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -117,19 +117,10 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N * {@inheritdoc} */ public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) { - if (!isset($langcode)) { - $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(); - foreach ($entities as $entity) { - if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) { - $entity->addRuntimeCacheContexts(['languages:' . LanguageInterface::TYPE_CONTENT]); - } - } - } - $build_list = array( '#sorted' => TRUE, '#pre_render' => array(array($this, 'buildMultiple')), - '#langcode' => $langcode, + '#langcode' => $langcode ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(), ); $weight = 0; foreach ($entities as $key => $entity) { @@ -138,9 +129,10 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la $entity = $this->entityManager->getTranslationFromContext($entity, $langcode); // Set build defaults. - $build_list[$key] = $this->getBuildDefaults($entity, $view_mode, $langcode); + $entity_langcode = $entity->language()->getId(); + $build_list[$key] = $this->getBuildDefaults($entity, $view_mode, $entity_langcode); $entityType = $this->entityTypeId; - $this->moduleHandler()->alter(array($entityType . '_build_defaults', 'entity_build_defaults'), $build_list[$key], $entity, $view_mode, $langcode); + $this->moduleHandler()->alter(array($entityType . '_build_defaults', 'entity_build_defaults'), $build_list[$key], $entity, $view_mode, $entity_langcode); $build_list[$key]['#weight'] = $weight++; } diff --git a/core/modules/comment/src/Tests/CommentTranslationUITest.php b/core/modules/comment/src/Tests/CommentTranslationUITest.php index 7edf1af..1e6d716 100644 --- a/core/modules/comment/src/Tests/CommentTranslationUITest.php +++ b/core/modules/comment/src/Tests/CommentTranslationUITest.php @@ -37,7 +37,14 @@ class CommentTranslationUITest extends ContentTranslationUITestBase { * * @var array */ - protected $defaultCacheContexts = ['languages:language_interface', 'theme' , 'user.permissions', 'timezone', 'url.query_args.pagers:0', 'user.roles']; + protected $defaultCacheContexts = [ + 'languages:language_interface', + 'theme', + 'user.permissions', + 'timezone', + 'url.query_args.pagers:0', + 'user.roles' + ]; /** * Modules to install.