Skip to content

Commit

Permalink
8.1.7
Browse files Browse the repository at this point in the history
D8CORE-4693 Invalidate events whose end dates have recently passed (#19)
  • Loading branch information
pookmish authored Oct 8, 2021
2 parents 202876d + 465df63 commit 20e7a62
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Stanford Fields


8.x-1.7
--------------------------------------------------------------------------------
_Release Date: 2021-10-08_

- D8CORE-4693 Invalidate events whose end dates have recently passed (#19)

8.x-1.5
--------------------------------------------------------------------------------
_Release Date: 2021-05-07_
Expand Down
28 changes: 23 additions & 5 deletions src/Service/FieldCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function invalidateDateFieldsCache() {
}

Cache::invalidateTags(array_unique($cache_tags));
$this->state->set('stanford_fields.dates_cleared', time() - 5);
}

/**
Expand Down Expand Up @@ -111,21 +112,38 @@ protected function getExpiredDateCacheTags($entity_type, $field_name, array $bun

// Query all entities for the given date field that is between the last ran
// time and the current time.
$query = $entity_storage->getQuery()
$start_query = $entity_storage->getQuery()
->accessCheck(FALSE)
->exists($field_name)
->exists($field_name);
$end_query = clone $start_query;
$condition_group = $start_query->andConditionGroup()
->condition($field_name, $now->format($field_date_format), '<=')
->condition($field_name, $last_ran->format($field_date_format), '>=');
$start_query->condition($condition_group);

// Some entity types don't have bundles, so don't add the condition if not
// applicable.
if ($bundle_key) {
$query->condition($bundle_key, $bundles, 'IN');
$start_query->condition($bundle_key, $bundles, 'IN');
$end_query->condition($bundle_key, $bundles, 'IN');
}
$tags = [];

$field_properties = $field_definition->getPropertyNames();
// If the field type has an end value, modify the end query's conditions to
// check for those values.
if (in_array('end_value', $field_properties)) {
$condition_group = $end_query->andConditionGroup()
->condition("$field_name.end_value", $now->format($field_date_format), '<=')
->condition("$field_name.end_value", $last_ran->format($field_date_format), '>=');
}
$end_query->condition($condition_group);

// Merge the start query ids with the end query ids.
$entity_ids = array_merge($start_query->execute(), $end_query->execute());

$tags = [];
// If no entity ids were found, no tags should be invalidated.
if ($entity_ids = $query->execute()) {
if (!empty($entity_ids)) {
$entities = $entity_storage->loadMultiple($entity_ids);
foreach ($entities as $entity) {
$tags = array_merge($tags, $entity->getCacheTagsToInvalidate());
Expand Down
2 changes: 1 addition & 1 deletion stanford_fields.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ type: module
description: 'Field types, widgets and formatters to enhance Drupal and Contrib.'
core_version_requirement: ^8 || ^9
package: Stanford
version: 8.x-1.6
version: 8.x-1.7
dependencies:
- drupal:field
1 change: 0 additions & 1 deletion stanford_fields.module
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ function stanford_fields_field_storage_add_validate(&$form, FormStateInterface $
*/
function stanford_fields_cron() {
\Drupal::service('stanford_fields.field_cache')->invalidateDateFieldsCache();
\Drupal::state()->set('stanford_fields.dates_cleared', time() - 5);
}
30 changes: 29 additions & 1 deletion tests/src/Kernel/Service/FieldCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class FieldCacheTest extends KernelTestBase {
'node',
'user',
'datetime',
'datetime_range',
'field',
];

Expand Down Expand Up @@ -68,6 +69,16 @@ protected function setUp(): void {
'bundle' => 'page',
'field_name' => 'field_date',
])->save();
FieldStorageConfig::create([
'entity_type' => 'node',
'type' => 'daterange',
'field_name' => 'field_daterange',
])->save();
FieldConfig::create([
'entity_type' => 'node',
'bundle' => 'page',
'field_name' => 'field_daterange',
])->save();
\Drupal::getContainer()->set('cache_tags.invalidator', $cache_invalidator);
}

Expand All @@ -91,12 +102,29 @@ public function testDateInvalidation() {
->invalidateDateFieldsCache();
$this->assertEmpty($this->invalidatedTags);

\Drupal::state()
->set('stanford_fields.dates_cleared', time() - 60 * 60 * 24 * 4);
$node->set('field_date', date($this->dateFieldFormat, time() - 60 * 60 * 24 * 3))
->save();
$this->invalidatedTags = [];
\Drupal::service('stanford_fields.field_cache')
->invalidateDateFieldsCache();
$this->assertTrue(in_array('node:' . $node->id(), $this->invalidatedTags));

\Drupal::state()
->set('stanford_fields.dates_cleared', time() - 60 * 60 * 24 * 4);
$daterange = [
'value' => date($this->dateFieldFormat, time() - 60 * 60 * 24 * 10),
'end_value' => date($this->dateFieldFormat, time() - 60 * 60 * 24),
];
$node->set('field_date', [])
->set('field_daterange', $daterange)
->save();

$this->invalidatedTags = [];
\Drupal::service('stanford_fields.field_cache')
->invalidateDateFieldsCache();
$this->assertTrue(in_array('node:' . $node->id(), $this->invalidatedTags));
}

/**
Expand All @@ -117,4 +145,4 @@ public function invalidateTagsCallback($tags) {
$this->invalidatedTags = $tags;
}

}
}

0 comments on commit 20e7a62

Please sign in to comment.