diff --git a/src/Plugin/views/ResultRow.php b/src/Plugin/views/ResultRow.php index 186e2dbe..d19737c3 100644 --- a/src/Plugin/views/ResultRow.php +++ b/src/Plugin/views/ResultRow.php @@ -2,7 +2,9 @@ namespace Drupal\search_api\Plugin\views; +use Drupal\Core\Entity\EntityInterface; use Drupal\views\ResultRow as ViewsResultRow; +use Drupal\views\ViewExecutable; /** * A class representing a result row of a Search API-based view. @@ -90,4 +92,26 @@ public function __get($name) { return NULL; } + /** + * Implements the magic __sleep() method to remove the view from the entity. + * + * The "view" property is added by the "Rendered entity" row plugin in + * \Drupal\search_api\Plugin\views\row\SearchApiRow::preRender() to make it + * available to entity viewing code. It should not be needed elsewhere, but + * can cause problems (including infinite loops) when Views results are + * serialized (as done, for instance, by the Metatag module in some setups). + * + * @return string[] + * An array with the names of all object properties. + */ + public function __sleep() { + if (!empty($this->_object)) { + $entity = $this->_object->getValue(); + if (($entity->view ?? NULL) instanceof ViewExecutable) { + unset($entity->view); + } + } + return array_keys(get_object_vars($this)); + } + }