本文整理汇总了PHP中Drupal\Core\Cache\Cache::invalidateTags方法的典型用法代码示例。如果您正苦于以下问题:PHP Cache::invalidateTags方法的具体用法?PHP Cache::invalidateTags怎么用?PHP Cache::invalidateTags使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Cache\Cache
的用法示例。
在下文中一共展示了Cache::invalidateTags方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testContactLink
/**
* Tests contact link.
*/
public function testContactLink()
{
$accounts = array();
$accounts['root'] = User::load(1);
// Create an account with access to all contact pages.
$admin_account = $this->drupalCreateUser(array('administer users'));
$accounts['admin'] = $admin_account;
// Create an account with no access to contact pages.
$no_contact_account = $this->drupalCreateUser();
$accounts['no_contact'] = $no_contact_account;
// Create an account with access to contact pages.
$contact_account = $this->drupalCreateUser(array('access user contact forms'));
$accounts['contact'] = $contact_account;
$this->drupalLogin($admin_account);
$this->drupalGet('test-contact-link');
// The admin user has access to all contact links beside his own.
$this->assertContactLinks($accounts, array('root', 'no_contact', 'contact'));
$this->drupalLogin($no_contact_account);
$this->drupalGet('test-contact-link');
// Ensure that the user without the permission doesn't see any link.
$this->assertContactLinks($accounts, array());
$this->drupalLogin($contact_account);
$this->drupalGet('test-contact-link');
$this->assertContactLinks($accounts, array('root', 'admin', 'no_contact'));
// Disable contact link for no_contact.
$this->userData->set('contact', $no_contact_account->id(), 'enabled', FALSE);
// @todo Remove cache invalidation in https://www.drupal.org/node/2477903.
Cache::invalidateTags($no_contact_account->getCacheTagsToInvalidate());
$this->drupalGet('test-contact-link');
$this->assertContactLinks($accounts, array('root', 'admin'));
}
示例2: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('dropdown_language.setting')
->set('wrapper', $form_state->getValue('wrapper'))
->save();
parent::submitForm($form, $form_state);
Cache::invalidateTags(['rendered']);
}
示例3: testPictureOnNodeComment
/**
* Tests embedded users on node pages.
*/
function testPictureOnNodeComment()
{
$this->drupalLogin($this->web_user);
// Save a new picture.
$image = current($this->drupalGetTestFiles('image'));
$file = $this->saveUserPicture($image);
$node = $this->drupalCreateNode(array('type' => 'article'));
// Enable user pictures on nodes.
$this->container->get('config.factory')->get('system.theme.global')->set('features.node_user_picture', TRUE)->save();
// Verify that the image is displayed on the user account page.
$this->drupalGet('node/' . $node->id());
$this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on node page.');
// Enable user pictures on comments, instead of nodes.
$this->container->get('config.factory')->get('system.theme.global')->set('features.node_user_picture', FALSE)->set('features.comment_user_picture', TRUE)->save();
// @todo Remove when https://www.drupal.org/node/2040135 lands.
Cache::invalidateTags(['rendered']);
$edit = array('comment_body[0][value]' => $this->randomString());
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Save'));
$this->assertRaw(file_uri_target($file->getFileUri()), 'User picture found on comment.');
// Disable user pictures on comments and nodes.
$this->container->get('config.factory')->get('system.theme.global')->set('features.node_user_picture', FALSE)->set('features.comment_user_picture', FALSE)->save();
// @todo Remove when https://www.drupal.org/node/2040135 lands.
Cache::invalidateTags(['rendered']);
$this->drupalGet('node/' . $node->id());
$this->assertNoRaw(file_uri_target($file->getFileUri()), 'User picture not found on node and comment.');
}
示例4: testPathCache
/**
* Tests the path cache.
*/
function testPathCache()
{
// Create test node.
$node1 = $this->drupalCreateNode();
// Create alias.
$edit = array();
$edit['source'] = '/node/' . $node1->id();
$edit['alias'] = '/' . $this->randomMachineName(8);
$this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
// Check the path alias whitelist cache.
$whitelist = \Drupal::cache()->get('path_alias_whitelist');
$this->assertTrue($whitelist->data['node']);
$this->assertFalse($whitelist->data['admin']);
// Visit the system path for the node and confirm a cache entry is
// created.
\Drupal::cache('data')->deleteAll();
// Make sure the path is not converted to the alias.
$this->drupalGet(trim($edit['source'], '/'), array('alias' => TRUE));
$this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
// Visit the alias for the node and confirm a cache entry is created.
\Drupal::cache('data')->deleteAll();
// @todo Remove this once https://www.drupal.org/node/2480077 lands.
Cache::invalidateTags(['rendered']);
$this->drupalGet(trim($edit['alias'], '/'));
$this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
}
示例5: testPrivateField
/**
* Tests private profile field access.
*/
public function testPrivateField()
{
$this->drupalLogin($this->adminUser);
// Create a private profile field.
$edit = ['new_storage_type' => 'string', 'label' => 'Secret', 'field_name' => 'secret'];
$this->drupalPostForm("admin/config/people/profiles/types/manage/{$this->type->id()}/fields/add-field", $edit, t('Save and continue'));
$this->drupalPostForm(NULL, [], t('Save field settings'));
$edit = ['profile_private' => 1];
$this->drupalPostForm(NULL, $edit, t('Save settings'));
// Fill in a field value.
$this->drupalLogin($this->webUser);
$uid = $this->webUser->id();
$secret = $this->randomMachineName();
$edit = ['field_secret[0][value]' => $secret];
$this->drupalPostForm("user/{$uid}/{$this->type->id()}", $edit, t('Save'));
// User cache page need to be cleared to see new profile.
Cache::invalidateTags(['user:' . $uid, 'user_view']);
// Verify that the private field value appears for the profile owner.
$this->drupalGet("user/{$uid}");
$this->assertText($secret);
// Verify that the private field value appears for the administrator.
$this->drupalLogin($this->adminUser);
$this->drupalGet("user/{$uid}");
$this->assertText($secret);
// Verify that the private field value does not appear for other users.
$this->drupalLogin($this->otherUser);
$this->drupalGet("user/{$uid}");
$this->assertNoText($secret);
}
示例6: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$this->config('juicebox.settings')->set('apply_markup_filter', $form_state->getvalue('apply_markup_filter'))->set('enable_cors', $form_state->getvalue('enable_cors'))->set('translate_interface', $form_state->getvalue('translate_interface'))->set('base_languagelist', $form_state->getvalue('base_languagelist'))->set('juicebox_multisize_small', $form_state->getvalue('juicebox_multisize_small'))->set('juicebox_multisize_medium', $form_state->getvalue('juicebox_multisize_medium'))->set('juicebox_multisize_large', $form_state->getvalue('juicebox_multisize_large'))->save();
// These settings are global and may affect any gallery embed or XML code,
// so we need to clear everything tagged with juicebox_gallery cache tag.
Cache::invalidateTags(array('juicebox_gallery'));
drupal_set_message(t('The Juicebox configuration options have been saved'));
}
示例7: delete
/**
* {@inheritdoc}
*/
public function delete()
{
$this->data = array();
$this->storage->delete($this->name);
Cache::invalidateTags($this->getCacheTags());
$this->isNew = TRUE;
$this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::DELETE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
$this->originalData = $this->data;
return $this;
}
示例8: submitForm
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$consumer_key = user_password(32);
$consumer_secret = user_password(32);
$key_hash = sha1($consumer_key);
$uid = $form_state->getValue('uid');
$consumer = array('consumer_secret' => $consumer_secret, 'key_hash' => $key_hash);
$this->user_data->set('oauth', $uid, $consumer_key, $consumer);
drupal_set_message($this->t('Added a new consumer.'));
Cache::invalidateTags(['oauth:' . $uid]);
$form_state->setRedirect('oauth.user_consumer', array('user' => $uid));
}
示例9: finishGeneration
/**
* Callback function called by the batch API when all operations are finished.
*
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function finishGeneration($success, $results, $operations)
{
if ($success) {
$remove_sitemap = empty($results['chunk_count']);
if (!empty($results['generate']) || $remove_sitemap) {
\Drupal::service('simple_sitemap.sitemap_generator')->generateSitemap($results['generate'], $remove_sitemap);
}
Cache::invalidateTags(['simple_sitemap']);
drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.", ['@url' => $GLOBALS['base_url'] . '/sitemap.xml']));
} else {
//todo: register error
}
}
示例10: testMetatag
/**
* Tests adding and editing values using metatag.
*/
public function testMetatag()
{
// Create a test entity.
$edit = ['name[0][value]' => 'Barfoo', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')', 'field_metatag[0][basic][metatag_test]' => 'Kilimanjaro'];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$entities = entity_load_multiple_by_properties('entity_test', ['name' => 'Barfoo']);
$this->assertEqual(1, count($entities), 'Entity was saved');
$entity = reset($entities);
// Make sure tags that have a field value but no default value still show
// up.
$this->drupalGet('entity_test/' . $entity->id());
$this->assertResponse(200);
$elements = $this->cssSelect('meta[name=metatag_test]');
$this->assertTrue(count($elements) === 1, 'Found keywords metatag_test from defaults');
$this->assertEqual((string) $elements[0]['content'], 'Kilimanjaro', 'Field value for metatag_test found when no default set.');
// @TODO: This should not be required, but metatags does not invalidate
// cache upon setting globals.
Cache::invalidateTags(array('entity_test:' . $entity->id()));
// Update the Global defaults and test them.
$values = array('keywords' => 'Purple monkey dishwasher');
$this->drupalPostForm('admin/config/search/metatag/global', $values, 'Save');
$this->assertText('Saved the Global Metatag defaults.');
$this->drupalGet('entity_test/' . $entity->id());
$this->assertResponse(200);
$elements = $this->cssSelect('meta[name=keywords]');
$this->assertTrue(count($elements) === 1, 'Found keywords metatag from defaults');
$this->assertEqual((string) $elements[0]['content'], $values['keywords'], 'Default keywords applied');
// Tests metatags with URLs work.
$edit = ['name[0][value]' => 'UrlTags', 'user_id[0][target_id]' => 'foo (' . $this->adminUser->id() . ')', 'field_metatag[0][advanced][original_source]' => 'http://example.com/foo.html'];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$entities = entity_load_multiple_by_properties('entity_test', ['name' => 'UrlTags']);
$this->assertEqual(1, count($entities), 'Entity was saved');
$entity = reset($entities);
$this->drupalGet('entity_test/' . $entity->id());
$this->assertResponse(200);
$elements = $this->cssSelect("meta[name='original-source']");
$this->assertTrue(count($elements) === 1, 'Found original source metatag from defaults');
$this->assertEqual((string) $elements[0]['content'], $edit['field_metatag[0][advanced][original_source]']);
// Test a route where the entity for that route does not implement
// ContentEntityInterface.
$controller = \Drupal::entityTypeManager()->getStorage('contact_form');
$controller->create(array('id' => 'test_contact_form'))->save();
$account = $this->drupalCreateUser(array('access site-wide contact form'));
$this->drupalLogin($account);
$this->drupalGet('contact/test_contact_form');
$this->assertResponse(200);
}
示例11: testTagInvalidations
public function testTagInvalidations()
{
// Create cache entry in multiple bins.
$tags = array('test_tag' => array(1, 2, 3));
$bins = array('data', 'bootstrap', 'render');
foreach ($bins as $bin) {
$bin = \Drupal::cache($bin);
$bin->set('test', 'value', Cache::PERMANENT, $tags);
$this->assertTrue($bin->get('test'), 'Cache item was set in bin.');
}
$invalidations_before = intval(db_select('cachetags')->fields('cachetags', array('invalidations'))->condition('tag', 'test_tag:2')->execute()->fetchField());
Cache::invalidateTags(array('test_tag' => array(2)));
// Test that cache entry has been invalidated in multiple bins.
foreach ($bins as $bin) {
$bin = \Drupal::cache($bin);
$this->assertFalse($bin->get('test'), 'Tag invalidation affected item in bin.');
}
// Test that only one tag invalidation has occurred.
$invalidations_after = intval(db_select('cachetags')->fields('cachetags', array('invalidations'))->condition('tag', 'test_tag:2')->execute()->fetchField());
$this->assertEqual($invalidations_after, $invalidations_before + 1, 'Only one addition cache tag invalidation has occurred after invalidating a tag used in multiple bins.');
}
示例12: testPageCacheTags
/**
* Test that cache tags are properly persisted.
*
* Since tag based invalidation works, we know that our tag properly
* persisted.
*/
function testPageCacheTags()
{
$config = $this->config('system.performance');
$config->set('cache.page.max_age', 300);
$config->save();
$path = 'system-test/cache_tags_page';
$tags = array('system_test_cache_tags_page');
$this->drupalGet($path);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$this->drupalGet($path);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$cid_parts = array(\Drupal::url('system_test.cache_tags_page', array(), array('absolute' => TRUE)), 'html');
$cid = implode(':', $cid_parts);
$cache_entry = \Drupal::cache('render')->get($cid);
sort($cache_entry->tags);
$expected_tags = array('pre_render', 'rendered', 'system_test_cache_tags_page');
$this->assertIdentical($cache_entry->tags, $expected_tags);
Cache::invalidateTags($tags);
$this->drupalGet($path);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
}
示例13: testMultipleProfileType
/**
* Tests the flow of a profile type that has multiple enabled.
*/
public function testMultipleProfileType()
{
$this->drupalLogin($this->adminUser);
$edit = ['multiple' => 1];
$this->drupalPostForm("admin/config/people/profiles/types/manage/{$this->type->id()}", $edit, t('Save'));
$this->assertRaw(new FormattableMarkup('%type profile type has been updated.', ['%type' => $this->type->label()]));
$web_user1 = $this->drupalCreateUser(["view own {$this->type->id()} profile", "add own {$this->type->id()} profile", "edit own {$this->type->id()} profile"]);
$this->drupalLogin($web_user1);
$value = $this->randomMachineName();
$edit = ["{$this->field->getName()}[0][value]" => $value];
$this->drupalPostForm("user/{$web_user1->id()}/{$this->type->id()}", $edit, t('Save'));
$this->assertRaw(new FormattableMarkup('%type profile has been created.', ['%type' => $this->type->label()]));
$this->drupalGet("user/{$web_user1->id()}/{$this->type->id()}");
$this->assertLinkByHref("user/{$web_user1->id()}/{$this->type->id()}/add");
$this->assertText($value);
$value2 = $this->randomMachineName();
$edit = ["{$this->field->getName()}[0][value]" => $value2];
$this->drupalPostForm("user/{$web_user1->id()}/{$this->type->id()}/add", $edit, t('Save'));
$this->assertRaw(new FormattableMarkup('%type profile has been created.', ['%type' => $this->type->label()]));
Cache::invalidateTags(['profile_view']);
$this->drupalGet("user/{$web_user1->id()}/{$this->type->id()}");
$this->assertText($value2);
}
示例14: testNodeViewModeChange
/**
* Create a "Basic page" node and verify its consistency in the database.
*/
function testNodeViewModeChange()
{
$web_user = $this->drupalCreateUser(array('create page content', 'edit own page content'));
$this->drupalLogin($web_user);
// Create a node.
$edit = array();
$edit['title[0][value]'] = $this->randomMachineName(8);
$edit['body[0][value]'] = t('Data that should appear only in the body for the node.');
$edit['body[0][summary]'] = t('Extra data that should appear only in the teaser for the node.');
$this->drupalPostForm('node/add/page', $edit, t('Save'));
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
// Set the flag to alter the view mode and view the node.
\Drupal::state()->set('node_test_change_view_mode', 'teaser');
Cache::invalidateTags(['rendered']);
$this->drupalGet('node/' . $node->id());
// Check that teaser mode is viewed.
$this->assertText('Extra data that should appear only in the teaser for the node.', 'Teaser text present');
// Make sure body text is not present.
$this->assertNoText('Data that should appear only in the body for the node.', 'Body text not present');
// Test that the correct build mode has been set.
$build = $this->drupalBuildEntityView($node);
$this->assertEqual($build['#view_mode'], 'teaser', 'The view mode has correctly been set to teaser.');
}
示例15: testCachePerRole
/**
* Test "user.roles" cache context.
*/
function testCachePerRole()
{
\Drupal::state()->set('block_test.cache_contexts', ['user.roles']);
// Enable our test block. Set some content for it to display.
$current_content = $this->randomMachineName();
\Drupal::state()->set('block_test.content', $current_content);
$this->drupalLogin($this->normalUser);
$this->drupalGet('');
$this->assertText($current_content, 'Block content displays.');
// Change the content, but the cached copy should still be served.
$old_content = $current_content;
$current_content = $this->randomMachineName();
\Drupal::state()->set('block_test.content', $current_content);
$this->drupalGet('');
$this->assertText($old_content, 'Block is served from the cache.');
// Clear the cache and verify that the stale data is no longer there.
Cache::invalidateTags(array('block_view'));
$this->drupalGet('');
$this->assertNoText($old_content, 'Block cache clear removes stale cache data.');
$this->assertText($current_content, 'Fresh block content is displayed after clearing the cache.');
// Test whether the cached data is served for the correct users.
$old_content = $current_content;
$current_content = $this->randomMachineName();
\Drupal::state()->set('block_test.content', $current_content);
$this->drupalLogout();
$this->drupalGet('');
$this->assertNoText($old_content, 'Anonymous user does not see content cached per-role for normal user.');
$this->drupalLogin($this->normalUserAlt);
$this->drupalGet('');
$this->assertText($old_content, 'User with the same roles sees per-role cached content.');
$this->drupalLogin($this->adminUser);
$this->drupalGet('');
$this->assertNoText($old_content, 'Admin user does not see content cached per-role for normal user.');
$this->drupalLogin($this->normalUser);
$this->drupalGet('');
$this->assertText($old_content, 'Block is served from the per-role cache.');
}