Skip to content
Snippets Groups Projects

Break circular dependencies in EntityReferenceRevisionsFieldItemList::hasAffectingChanges()

1 file
+ 30
0
Compare changes
  • Side-by-side
  • Inline
@@ -15,6 +15,12 @@ use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
*/
class EntityReferenceRevisionsFieldItemList extends EntityReferenceFieldItemList implements EntityReferenceFieldItemListInterface {
/**
* Static cache that remembers if an entity has affecting changes or not.
* This is needed to break a circular dependencies.
*/
static $cache = [];
/**
* {@inheritdoc}
*/
@@ -108,19 +114,36 @@ class EntityReferenceRevisionsFieldItemList extends EntityReferenceFieldItemList
* {@inheritdoc}
*/
public function hasAffectingChanges(FieldItemListInterface $original_items, $langcode) {
// Create a cache key based on entity type, entity id, and langcode.
$cache_key = "{$this->getEntity()->getEntityTypeId()}:{$this->getEntity()->id()}:$langcode";
// If entity exists in cache, return cached value.
if (isset(self::$cache[$cache_key])) {
return self::$cache[$cache_key];
}
// Set cached value to FALSE until proven otherwise.
self::$cache[$cache_key] = FALSE;
// If there are fewer items, then it is a change.
if (count($this) < count($original_items)) {
// We were wrong, the entity has been changed. Update cached value to TRUE and return.
self::$cache[$cache_key] = TRUE;
return TRUE;
}
foreach ($this as $delta => $item) {
// If this is a different entity, then it is an affecting change.
if (!$original_items->offsetExists($delta) || $item->target_id != $original_items[$delta]->target_id) {
// We were wrong, the entity has been changed. Update cached value to TRUE and return.
self::$cache[$cache_key] = TRUE;
return TRUE;
}
// If it is the same entity, only consider it as having affecting changes
// if the target entity itself has changes.
if ($item->entity && $item->entity->hasTranslation($langcode) && $item->entity->getTranslation($langcode)->hasTranslationChanges()) {
// We were wrong, the entity has been changed. Update cached value to TRUE and return.
self::$cache[$cache_key] = TRUE;
return TRUE;
}
}
@@ -128,4 +151,11 @@ class EntityReferenceRevisionsFieldItemList extends EntityReferenceFieldItemList
return FALSE;
}
/**
* {@inheritdoc }
*/
public function postSave($update) {
// Reset static cache to break circuclar dependency after completing a save.
self::$cache = [];
}
}
Loading