diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
index 14df452a22..6e5d3449c2 100644
--- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
+++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php
@@ -4,6 +4,7 @@
 
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Cache\CacheableMetadata;
+use Drupal\Core\Entity\EntityInterface;
 use Drupal\views\Plugin\views\PluginBase;
 use Drupal\Core\Database\Query\Select;
 use Drupal\views\ResultRow;
@@ -280,6 +281,29 @@ protected function prepareViewResult(array $result) {
     return $return;
   }
 
+  /**
+   * Gets all the involved entities of the view row.
+   *
+   * @param \Drupal\views\ResultRow $row
+   *   Views result row.
+   *
+   * @return \Drupal\Core\Entity\EntityInterface[]
+   *   The row entities.
+   */
+  protected function getRowEntities(ResultRow $row) {
+    $entities = [];
+    if ($row->_entity instanceof EntityInterface) {
+      $entities[] = $row->_entity;
+    }
+    foreach ($row->_relationship_entities as $entity) {
+      if ($entity instanceof EntityInterface) {
+        $entities[] = $entity;
+      }
+    }
+
+    return $entities;
+  }
+
   /**
    * Alters the cache metadata of a display upon saving a view.
    *
@@ -299,12 +323,10 @@ public function alterCacheMetadata(CacheableMetadata $cache_metadata) {
    *   The row cache tags.
    */
   public function getRowCacheTags(ResultRow $row) {
-    $tags = !empty($row->_entity) ? $row->_entity->getCacheTags() : [];
+    $tags = [];
 
-    if (!empty($row->_relationship_entities)) {
-      foreach ($row->_relationship_entities as $entity) {
-        $tags = Cache::mergeTags($tags, $entity->getCacheTags());
-      }
+    foreach ($this->getRowEntities($row) as $entity) {
+      $tags = Cache::mergeTags($tags, $entity->getCacheTags());
     }
 
     return $tags;
diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/core/modules/views/src/Plugin/views/query/Sql.php
index 2771a3d7c0..7a3ddcb0f3 100644
--- a/core/modules/views/src/Plugin/views/query/Sql.php
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -6,6 +6,7 @@
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Database\Database;
 use Drupal\Core\Database\Query\Condition;
+use Drupal\Core\Entity\EntityInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\views\Plugin\views\display\DisplayPluginBase;
@@ -1661,15 +1662,18 @@ public function getCacheMaxAge() {
    * Gets all the involved entities of the view.
    *
    * @return \Drupal\Core\Entity\EntityInterface[]
+   *   The view entities.
    */
   protected function getAllEntities() {
     $entities = [];
     foreach ($this->view->result as $row) {
-      if ($row->_entity) {
+      if ($row->_entity instanceof EntityInterface) {
         $entities[] = $row->_entity;
       }
       foreach ($row->_relationship_entities as $entity) {
-        $entities[] = $entity;
+        if ($entity instanceof EntityInterface) {
+          $entities[] = $entity;
+        }
       }
     }
 
