本文整理汇总了PHP中Drupal::keyValue方法的典型用法代码示例。如果您正苦于以下问题:PHP Drupal::keyValue方法的具体用法?PHP Drupal::keyValue怎么用?PHP Drupal::keyValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal
的用法示例。
在下文中一共展示了Drupal::keyValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testUninstallPostUpdateFunctions
/**
* Ensures that post update functions are removed on uninstall.
*/
public function testUninstallPostUpdateFunctions()
{
\Drupal::service('module_installer')->uninstall(['module_test']);
$post_update_key_value = \Drupal::keyValue('post_update');
$existing_updates = $post_update_key_value->get('existing_updates', []);
$this->assertFalse(in_array('module_test_post_update_test', $existing_updates));
}
示例2: testPostUpdate
/**
* Tests hook_post_update_NAME().
*/
public function testPostUpdate()
{
$this->runUpdates();
$this->assertRaw('<h3>Update first</h3>');
$this->assertRaw('First update');
$this->assertRaw('<h3>Update second</h3>');
$this->assertRaw('Second update');
$this->assertRaw('<h3>Update test1</h3>');
$this->assertRaw('Test1 update');
$this->assertRaw('<h3>Update test0</h3>');
$this->assertRaw('Test0 update');
$this->assertRaw('<h3>Update test_batch</h3>');
$this->assertRaw('Test post update batches');
// Test state value set by each post update.
$updates = ['update_test_postupdate_post_update_first', 'update_test_postupdate_post_update_second', 'update_test_postupdate_post_update_test1', 'update_test_postupdate_post_update_test0', 'update_test_postupdate_post_update_test_batch-1', 'update_test_postupdate_post_update_test_batch-2', 'update_test_postupdate_post_update_test_batch-3'];
$this->assertIdentical($updates, \Drupal::state()->get('post_update_test_execution', []));
// Test post_update key value stores contains a list of the update functions
// that have run.
$existing_updates = array_count_values(\Drupal::keyValue('post_update')->get('existing_updates'));
$expected_updates = ['update_test_postupdate_post_update_first', 'update_test_postupdate_post_update_second', 'update_test_postupdate_post_update_test1', 'update_test_postupdate_post_update_test0', 'update_test_postupdate_post_update_test_batch'];
foreach ($expected_updates as $expected_update) {
$this->assertEqual($existing_updates[$expected_update], 1, new FormattableMarkup("@expected_update exists in 'existing_updates' key and only appears once.", ['@expected_update' => $expected_update]));
}
$this->drupalGet('update.php/selection');
$this->assertText('No pending updates.');
}
示例3: testPostUpdate
/**
* Tests hook_post_update_NAME().
*/
public function testPostUpdate()
{
// There are expected to be failed updates.
$this->checkFailedUpdates = FALSE;
$this->runUpdates();
// There should be no post update hooks registered as being run.
$this->assertIdentical([], \Drupal::state()->get('post_update_test_execution', []));
$key_value = \Drupal::keyValue('update__post_update');
$this->assertEqual([], $key_value->get('existing_updates'));
}
示例4: block_post_update_disable_blocks_with_missing_contexts
/**
* Disable all blocks with missing context IDs in block_update_8001().
*/
function block_post_update_disable_blocks_with_missing_contexts()
{
// Don't execute the function if block_update_8002() got executed already,
// which used to do the same. Note: Its okay to check here, because
// update_do_one() does not update the installed schema version until the
// batch is finished.
$module_schema = drupal_get_installed_schema_version('block');
// The state entry 'block_update_8002_placeholder' is used in order to
// indicate that the placeholder block_update_8002() function has been
// executed, so this function needs to be executed as well. If the non
// placeholder version of block_update_8002() got executed already, the state
// won't be set and we skip this update.
if ($module_schema >= 8002 && !\Drupal::state()->get('block_update_8002_placeholder', FALSE)) {
return;
}
// Cleanup the state entry as its no longer needed.
\Drupal::state()->delete('block_update_8002');
$block_update_8001 = \Drupal::keyValue('update_backup')->get('block_update_8001', []);
$block_ids = array_keys($block_update_8001);
$block_storage = \Drupal::entityManager()->getStorage('block');
$blocks = $block_storage->loadMultiple($block_ids);
/** @var $blocks \Drupal\block\BlockInterface[] */
foreach ($blocks as $block) {
// This block has had conditions removed due to an inability to resolve
// contexts in block_update_8001() so disable it.
// Disable currently enabled blocks.
if ($block_update_8001[$block->id()]['status']) {
$block->setStatus(FALSE);
$block->save();
}
}
// Provides a list of plugin labels, keyed by plugin ID.
$condition_plugin_id_label_map = array_column(\Drupal::service('plugin.manager.condition')->getDefinitions(), 'label', 'id');
// Override with the UI labels we are aware of. Sadly they are not machine
// accessible, see
// \Drupal\node\Plugin\Condition\NodeType::buildConfigurationForm().
$condition_plugin_id_label_map['node_type'] = t('Content types');
$condition_plugin_id_label_map['request_path'] = t('Pages');
$condition_plugin_id_label_map['user_role'] = t('Roles');
if (count($block_ids) > 0) {
$message = t('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:');
$message .= '<ul>';
foreach ($blocks as $disabled_block_id => $disabled_block) {
$message .= '<li>' . t('@label (Visibility: @plugin_ids)', array('@label' => $disabled_block->get('settings')['label'], '@plugin_ids' => implode(', ', array_intersect_key($condition_plugin_id_label_map, array_flip(array_keys($block_update_8001[$disabled_block_id]['missing_context_ids'])))))) . '</li>';
}
$message .= '</ul>';
return $message;
}
}
示例5: testColumnUpdate
/**
* Tests that column-level schema changes are detected for fields with data.
*/
public function testColumnUpdate()
{
// Change the field type in the stored schema.
$schema = \Drupal::keyValue('entity.storage_schema.sql')->get('entity_test_rev.field_schema_data.test');
$schema['entity_test_rev__test']['fields']['test_value']['type'] = 'varchar_ascii';
\Drupal::keyValue('entity.storage_schema.sql')->set('entity_test_rev.field_schema_data.test', $schema);
// Now attempt to run automatic updates. An exception should be thrown
// since there is data in the table.
try {
\Drupal::service('entity.definition_update_manager')->applyUpdates();
$this->fail('Failed to detect a schema change in a field with data.');
} catch (FieldStorageDefinitionUpdateForbiddenException $e) {
$this->pass('Detected a schema change in a field with data.');
}
}
示例6: testStateListing
/**
* Tests state listing.
*/
public function testStateListing()
{
// Ensure that state listing page is accessible only by users with the
// adequate permissions.
$this->drupalGet('devel/state');
$this->assertResponse(403);
$this->drupalLogin($this->develUser);
$this->drupalGet('devel/state');
$this->assertResponse(200);
$this->assertText(t('State editor'));
// Ensure that the state variables table is visible.
$table = $this->xpath('//table[contains(@class, "devel-state-list")]');
$this->assertTrue($table, 'State list table found.');
// Ensure that all state variables are listed in the table.
$states = \Drupal::keyValue('state')->getAll();
$rows = $this->xpath('//table[contains(@class, "devel-state-list")]//tbody//tr');
$this->assertEqual(count($rows), count($states), 'All states are listed in the table.');
// Ensure that the added state variables are listed in the table.
$this->state->set('devel.simple', 'Hello!');
$this->drupalGet('devel/state');
$this->assertFieldByXpath('//table[contains(@class, "devel-state-list")]//tbody//td', 'devel.simple', 'Label found for the added state.');
$thead_xpath = '//table[contains(@class, "devel-state-list")]/thead/tr/th';
$action_xpath = '//table[contains(@class, "devel-state-list")]//ul[@class="dropbutton"]/li/a';
// Ensure that the operations column and the actions buttons are not
// available for user without 'administer site configuration' permission.
$elements = $this->xpath($thead_xpath);
$this->assertEqual(count($elements), 2, 'Correct number of table header cells found.');
$expected_items = ['Name', 'Value'];
foreach ($elements as $key => $element) {
$this->assertIdentical((string) $element[0], $expected_items[$key]);
}
$this->assertFalse($this->xpath($action_xpath), 'Action buttons are not visible.');
// Ensure that the operations column and the actions buttons are
// available for user with 'administer site configuration' permission.
$this->drupalLogin($this->adminUser);
$this->drupalGet('devel/state');
$elements = $this->xpath($thead_xpath);
$this->assertEqual(count($elements), 3, 'Correct number of table header cells found.');
$expected_items = ['Name', 'Value', 'Operations'];
foreach ($elements as $key => $element) {
$this->assertIdentical((string) $element[0], $expected_items[$key]);
}
$this->assertTrue($this->xpath($action_xpath), 'Action buttons are visible.');
// Test that the edit button works properly.
$this->clickLink(t('Edit'));
$this->assertResponse(200);
}
示例7: testUpdateHookN
/**
* Tests that block context mapping is updated properly.
*/
public function testUpdateHookN()
{
$this->runUpdates();
$this->assertRaw('Encountered an unknown context mapping key coming probably from a contributed or custom module: One or more mappings could not be updated. Please manually review your visibility settings for the following blocks, which are disabled now:<ul><li>User login (Visibility: Baloney spam)</li></ul>');
// Disable maintenance mode.
\Drupal::state()->set('system.maintenance_mode', FALSE);
// We finished updating so we can login the user now.
$this->drupalLogin($this->rootUser);
// The block that we are testing has the following visibility rules:
// - only visible on node pages
// - only visible to authenticated users.
$block_title = 'Test for 2354889';
// Create two nodes, a page and an article.
$page = Node::create(['type' => 'page', 'title' => 'Page node']);
$page->save();
$article = Node::create(['type' => 'article', 'title' => 'Article node']);
$article->save();
// Check that the block appears only on Page nodes for authenticated users.
$this->drupalGet('node/' . $page->id());
$this->assertRaw($block_title, 'Test block is visible on a Page node as an authenticated user.');
$this->drupalGet('node/' . $article->id());
$this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an authenticated user.');
$this->drupalLogout();
// Check that the block does not appear on any page for anonymous users.
$this->drupalGet('node/' . $page->id());
$this->assertNoRaw($block_title, 'Test block is not visible on a Page node as an anonymous user.');
$this->drupalGet('node/' . $article->id());
$this->assertNoRaw($block_title, 'Test block is not visible on a Article node as an anonymous user.');
// Ensure that all the context mappings got updated properly.
$block = Block::load('testfor2354889');
$visibility = $block->get('visibility');
$this->assertEqual('@node.node_route_context:node', $visibility['node_type']['context_mapping']['node']);
$this->assertEqual('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
$this->assertEqual('@language.current_language_context:language_interface', $visibility['language']['context_mapping']['language']);
// Check that a block with invalid context is being disabled and that it can
// still be edited afterward.
$disabled_block = Block::load('thirdtestfor2354889');
$this->assertFalse($disabled_block->status(), 'Block with invalid context is disabled');
$this->assertEqual(['thirdtestfor2354889' => ['missing_context_ids' => ['baloney.spam' => ['node_type']], 'status' => TRUE]], \Drupal::keyValue('update_backup')->get('block_update_8001'));
$disabled_block_visibility = $disabled_block->get('visibility');
$this->assertTrue(!isset($disabled_block_visibility['node_type']), 'The problematic visibility condition has been removed.');
$admin_user = $this->drupalCreateUser(['administer blocks']);
$this->drupalLogin($admin_user);
$this->drupalGet('admin/structure/block/manage/thirdtestfor2354889');
$this->assertResponse('200');
}
示例8: testDatabaseLoaded
/**
* Tests that the database was properly loaded.
*/
public function testDatabaseLoaded()
{
$hook_updates = ['user' => '8000', 'node' => '8003', 'system' => '8013'];
foreach ($hook_updates as $module => $schema) {
$this->assertEqual(drupal_get_installed_schema_version($module), $schema, new FormattableMarkup('Module @module schema is @schema', ['@module' => $module, '@schema' => $schema]));
}
// Test post_update key value stores contains a list of the update functions
// that have run.
$existing_updates = array_count_values(\Drupal::keyValue('post_update')->get('existing_updates'));
$expected_updates = ['system_post_update_recalculate_configuration_entity_dependencies', 'field_post_update_save_custom_storage_property', 'field_post_update_entity_reference_handler_setting', 'block_post_update_disable_blocks_with_missing_contexts', 'views_post_update_update_cacheability_metadata'];
foreach ($expected_updates as $expected_update) {
$this->assertEqual($existing_updates[$expected_update], 1, new FormattableMarkup("@expected_update exists in 'existing_updates' key and only appears once.", ['@expected_update' => $expected_update]));
}
// @todo there are no updates to run.
// $this->runUpdates();
$this->assertEqual(\Drupal::config('system.site')->get('name'), 'Site-Install');
$this->drupalGet('<front>');
$this->assertText('Site-Install');
}
示例9: testPostUpdate
/**
* Tests hook_post_update_NAME().
*/
public function testPostUpdate()
{
$this->runUpdates();
$this->assertRaw('<h3>Update first</h3>');
$this->assertRaw('First update');
$this->assertRaw('<h3>Update second</h3>');
$this->assertRaw('Second update');
$this->assertRaw('<h3>Update test1</h3>');
$this->assertRaw('Test1 update');
$this->assertRaw('<h3>Update test0</h3>');
$this->assertRaw('Test0 update');
$updates = ['update_test_postupdate_post_update_first', 'update_test_postupdate_post_update_second', 'update_test_postupdate_post_update_test1', 'update_test_postupdate_post_update_test0'];
$this->assertIdentical($updates, \Drupal::state()->get('post_update_test_execution', []));
$key_value = \Drupal::keyValue('post_update');
$updates = array_merge(['block_post_update_disable_blocks_with_missing_contexts', 'field_post_update_save_custom_storage_property', 'field_post_update_entity_reference_handler_setting', 'system_post_update_recalculate_configuration_entity_dependencies', 'views_post_update_update_cacheability_metadata'], $updates);
$this->assertEqual($updates, $key_value->get('existing_updates'));
$this->drupalGet('update.php/selection');
$this->assertText('No pending updates.');
}
示例10: testPostUpdate
/**
* Tests hook_post_update_NAME().
*/
public function testPostUpdate()
{
$this->runUpdates();
$this->assertRaw('<h3>Update first</h3>');
$this->assertRaw('First update');
$this->assertRaw('<h3>Update second</h3>');
$this->assertRaw('Second update');
$this->assertRaw('<h3>Update test1</h3>');
$this->assertRaw('Test1 update');
$this->assertRaw('<h3>Update test0</h3>');
$this->assertRaw('Test0 update');
$updates = ['update_test_postupdate_post_update_first', 'update_test_postupdate_post_update_second', 'update_test_postupdate_post_update_test1', 'update_test_postupdate_post_update_test0'];
$this->assertIdentical($updates, \Drupal::state()->get('post_update_test_execution', []));
$key_value = \Drupal::keyValue('post_update');
array_unshift($updates, 'block_post_update_disable_blocks_with_missing_contexts');
$this->assertEqual($updates, $key_value->get('existing_updates'));
$this->drupalGet('update.php/selection');
$this->assertText('No pending updates.');
}
示例11: testDatabaseLoaded
/**
* Tests that the database was properly loaded.
*/
public function testDatabaseLoaded()
{
$extensions = \Drupal::service('config.storage')->read('core.extension');
$this->assertFalse(isset($extensions['theme']['stable']), 'Stable is not installed before updating.');
$hook_updates = ['user' => '8000', 'node' => '8003', 'system' => '8013'];
foreach ($hook_updates as $module => $schema) {
$this->assertEqual(drupal_get_installed_schema_version($module), $schema, new FormattableMarkup('Module @module schema is @schema', ['@module' => $module, '@schema' => $schema]));
}
// Test post_update key value stores contains a list of the update functions
// that have run.
$existing_updates = array_count_values(\Drupal::keyValue('post_update')->get('existing_updates'));
$expected_updates = ['system_post_update_recalculate_configuration_entity_dependencies', 'field_post_update_save_custom_storage_property', 'field_post_update_entity_reference_handler_setting', 'block_post_update_disable_blocks_with_missing_contexts', 'views_post_update_update_cacheability_metadata'];
foreach ($expected_updates as $expected_update) {
$this->assertEqual($existing_updates[$expected_update], 1, new FormattableMarkup("@expected_update exists in 'existing_updates' key and only appears once.", ['@expected_update' => $expected_update]));
}
$this->runUpdates();
$this->assertEqual(\Drupal::config('system.site')->get('name'), 'Site-Install');
$this->drupalGet('<front>');
$this->assertText('Site-Install');
$extensions = \Drupal::service('config.storage')->read('core.extension');
$this->assertTrue(isset($extensions['theme']['stable']), 'Stable is installed after updating.');
$blocks = \Drupal::entityManager()->getStorage('block')->loadByProperties(['theme' => 'stable']);
$this->assertTrue(empty($blocks), 'No blocks have been placed for Stable.');
}
示例12: buildRow
/**
* Builds a row for an entity in the entity listing.
*
* @param \Drupal\migrate\Entity\EntityInterface $migration
* The entity for which to build the row.
*
* @return array
* A render array of the table row for displaying the entity.
*
* @see Drupal\Core\Entity\EntityListController::render()
*/
public function buildRow(EntityInterface $migration)
{
$row['label'] = $migration->label();
$row['machine_name'] = $migration->id();
$row['status'] = $migration->getStatusLabel();
// Derive the stats
$source_plugin = $migration->getSourcePlugin();
$row['total'] = $source_plugin->count();
$map = $migration->getIdMap();
$row['imported'] = $map->importedCount();
// -1 indicates uncountable sources.
if ($row['total'] == -1) {
$row['total'] = $this->t('N/A');
$row['unprocessed'] = $this->t('N/A');
} else {
$row['unprocessed'] = $row['total'] - $map->processedCount();
}
$migration_group = $migration->getThirdPartySetting('migrate_plus', 'migration_group');
if (!$migration_group) {
$migration_group = 'default';
}
$route_parameters = array('migration_group' => $migration_group, 'migration' => $migration->id());
$row['messages'] = array('data' => array('#type' => 'link', '#title' => $map->messageCount(), '#url' => Url::fromRoute("migrate_tools.messages", $route_parameters)));
$migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported');
$last_imported = $migrate_last_imported_store->get($migration->id(), FALSE);
if ($last_imported) {
/** @var DateFormatter $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
$row['last_imported'] = $date_formatter->format($last_imported / 1000, 'custom', 'Y-m-d H:i:s');
} else {
$row['last_imported'] = '';
}
return $row;
// + parent::buildRow($migration);
}
示例13: interruptMigration
/**
* {@inheritdoc}
*/
public function interruptMigration($result)
{
$this->setStatus(MigrationInterface::STATUS_STOPPING);
\Drupal::keyValue('migrate_interruption_result')->set($this->id(), $result);
}
示例14: getIndexDbInfo
/**
* Retrieves the database information for the test index.
*
* @return array
* The database information stored by the backend for the test index.
*/
protected function getIndexDbInfo()
{
return \Drupal::keyValue(BackendDatabase::INDEXES_KEY_VALUE_STORE_ID)->get($this->indexId);
}
示例15: processEntityAutocomplete
/**
* Adds entity autocomplete functionality to a form element.
*
* @param array $element
* The form element to process. Properties used:
* - #target_type: The ID of the target entity type.
* - #selection_handler: The plugin ID of the entity reference selection
* handler.
* - #selection_settings: An array of settings that will be passed to the
* selection handler.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The form element.
*
* @throws \InvalidArgumentException
* Exception thrown when the #target_type or #autocreate['bundle'] are
* missing.
*/
public static function processEntityAutocomplete(array &$element, FormStateInterface $form_state, array &$complete_form)
{
// Nothing to do if there is no target entity type.
if (empty($element['#target_type'])) {
throw new \InvalidArgumentException('Missing required #target_type parameter.');
}
// Provide default values and sanity checks for the #autocreate parameter.
if ($element['#autocreate']) {
if (!isset($element['#autocreate']['bundle'])) {
throw new \InvalidArgumentException("Missing required #autocreate['bundle'] parameter.");
}
// Default the autocreate user ID to the current user.
$element['#autocreate']['uid'] = isset($element['#autocreate']['uid']) ? $element['#autocreate']['uid'] : \Drupal::currentUser()->id();
}
// Store the selection settings in the key/value store and pass a hashed key
// in the route parameters.
$selection_settings = isset($element['#selection_settings']) ? $element['#selection_settings'] : [];
$data = serialize($selection_settings) . $element['#target_type'] . $element['#selection_handler'];
$selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());
$key_value_storage = \Drupal::keyValue('entity_autocomplete');
if (!$key_value_storage->has($selection_settings_key)) {
$key_value_storage->set($selection_settings_key, $selection_settings);
}
$element['#autocomplete_route_name'] = 'system.entity_autocomplete';
$element['#autocomplete_route_parameters'] = array('target_type' => $element['#target_type'], 'selection_handler' => $element['#selection_handler'], 'selection_settings_key' => $selection_settings_key);
return $element;
}