function delay_cache_tags_invalidation_entity_test_insert

Same name and namespace in other branches
  1. 10 core/modules/system/tests/modules/delay_cache_tags_invalidation/delay_cache_tags_invalidation.module \delay_cache_tags_invalidation_entity_test_insert()
  2. 8.9.x core/modules/system/tests/modules/delay_cache_tags_invalidation/delay_cache_tags_invalidation.module \delay_cache_tags_invalidation_entity_test_insert()

Implements hook_ENTITY_TYPE_insert().

File

core/modules/system/tests/modules/delay_cache_tags_invalidation/delay_cache_tags_invalidation.module, line 16

Code

function delay_cache_tags_invalidation_entity_test_insert(EntityTest $entity) {
  if (\Drupal::state()->get('delay_cache_tags_invalidation_exception')) {
    throw new \Exception('Abort entity save to trigger transaction rollback.');
  }
  // Read the pre-transaction cache writes.
  // @see \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::testEntitySave()
  \Drupal::state()->set(__FUNCTION__ . '__pretransaction_foobar', \Drupal::cache()->get('test_cache_pretransaction_foobar'));
  \Drupal::state()->set(__FUNCTION__ . '__pretransaction_entity_test_list', \Drupal::cache()->get('test_cache_pretransaction_entity_test_list'));
  // Write during the transaction.
  \Drupal::cache()->set(__FUNCTION__ . '__during_transaction_foobar', 'something', Cache::PERMANENT, [
    'foobar',
  ]);
  \Drupal::cache()->set(__FUNCTION__ . '__during_transaction_entity_test_list', 'something', Cache::PERMANENT, [
    'entity_test_list',
  ]);
  // Trigger a nested entity save and hence a nested transaction.
  User::create([
    'name' => 'johndoe',
    'status' => 1,
  ])->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.