From 8d307c4c989534971f3f699c1bd7ab0b44bf6b57 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Mon, 29 Jul 2024 15:13:22 -0400 Subject: [PATCH] DDST-382: Handle errors while deleting orphaned embargo --- src/Entity/Embargo.php | 55 ++++++++++++------------------------------ 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/src/Entity/Embargo.php b/src/Entity/Embargo.php index 89349b8..4496592 100644 --- a/src/Entity/Embargo.php +++ b/src/Entity/Embargo.php @@ -12,7 +12,6 @@ use Drupal\Core\TypedData\Exception\MissingDataException; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\embargo\EmbargoInterface; -use Drupal\embargo\EmbargoStorageInterface; use Drupal\embargo\IpRangeInterface; use Drupal\node\NodeInterface; use Drupal\user\UserInterface; @@ -33,7 +32,6 @@ * handlers = { * "storage" = "Drupal\embargo\EmbargoStorage", * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", - * "views_data" = "Drupal\embargo\EmbargoViewsData", * "list_builder" = "Drupal\embargo\EmbargoListBuilder", * "form" = { * "add" = "Drupal\embargo\Form\EmbargoForm", @@ -44,6 +42,7 @@ * "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider" * }, * }, + * list_cache_tags = { "node_list", "media_list", "file_list" }, * base_table = "embargo", * admin_permission = "administer embargo", * entity_keys = { @@ -379,47 +378,35 @@ public function setEmbargoedNode(NodeInterface $node): EmbargoInterface { } /** - * {@inheritDoc} + * The maximum age for which this object may be cached. + * + * @return int + * The maximum time in seconds that this object may be cached. */ public function getCacheMaxAge() { - $max_age = parent::getCacheMaxAge(); - $now = time(); // Invalidate cache after a scheduled embargo expires. if ($this->getExpirationType() === static::EXPIRATION_TYPE_SCHEDULED && !$this->expiresBefore($now)) { - $max_age = Cache::mergeMaxAges($max_age, $this->getExpirationDate()->getTimestamp() - $now); + return $this->getExpirationDate()->getTimestamp() - $now; } - - return $max_age; + // Other properties of the embargo are not time dependent. + return parent::getCacheMaxAge(); } /** * {@inheritdoc} */ public function getCacheTags() { - $tags = Cache::mergeTags(parent::getCacheTags(), $this->getEmbargoedNode()->getCacheTags()); + $tags = parent::getCacheTags(); - if ($this->getExemptIps()) { - $tags = Cache::mergeTags($tags, $this->getExemptIps()->getCacheTags()); - } - return $tags; - } - - /** - * {@inheritDoc} - */ - public function getCacheContexts() { - $contexts = Cache::mergeContexts( - parent::getCacheContexts(), - $this->getEmbargoedNode()->getCacheContexts(), - ['user.embargo__has_exemption'], - ); - - if ($this->getExemptIps()) { - $contexts = Cache::mergeContexts($contexts, $this->getExemptIps()->getCacheContexts()); + if ($this->getEmbargoedNode() !== NULL) { + $tags[] = "node:{$this->getEmbargoedNode()->id()}"; + if ($this->getExemptIps()) { + $tags = Cache::mergeTags($tags, $this->getExemptIps()->getCacheTags()); + } } - return $contexts; + return $tags; } /** @@ -451,16 +438,4 @@ public function ipIsExempt(string $ip): bool { return $exempt_ips && $exempt_ips->withinRanges($ip); } - /** - * {@inheritdoc} - */ - protected function getListCacheTagsToInvalidate() : array { - return array_merge( - parent::getListCacheTagsToInvalidate(), - array_map(function (string $type) { - return "{$type}_list"; - }, EmbargoStorageInterface::APPLICABLE_ENTITY_TYPES), - ); - } - }