本文整理汇总了PHP中Drupal::languageManager方法的典型用法代码示例。如果您正苦于以下问题:PHP Drupal::languageManager方法的具体用法?PHP Drupal::languageManager怎么用?PHP Drupal::languageManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal
的用法示例。
在下文中一共展示了Drupal::languageManager方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testForumUninstallWithField
/**
* Tests if forum module uninstallation properly deletes the field.
*/
function testForumUninstallWithField()
{
// Ensure that the field exists before uninstallation.
$field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
$this->assertNotNull($field_storage, 'The taxonomy_forums field storage exists.');
// Create a taxonomy term.
$term = entity_create('taxonomy_term', array('name' => t('A term'), 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->id, 'description' => '', 'parent' => array(0), 'vid' => 'forums', 'forum_container' => 0));
$term->save();
// Create a forum node.
$node = $this->drupalCreateNode(array('title' => 'A forum post', 'type' => 'forum', 'taxonomy_forums' => array(array('target_id' => $term->id()))));
// Create at least one comment against the forum node.
$comment = entity_create('comment', array('entity_id' => $node->nid->value, 'entity_type' => 'node', 'field_name' => 'comment_forum', 'pid' => 0, 'uid' => 0, 'status' => CommentInterface::PUBLISHED, 'subject' => $this->randomName(), 'hostname' => '127.0.0.1'));
$comment->save();
// Uninstall the forum module which should trigger field deletion.
$this->container->get('module_handler')->uninstall(array('forum'));
// We want to test the handling of removing the forum comment field, so we
// ensure there is at least one other comment field attached to a node type
// so that comment_entity_load() runs for nodes.
\Drupal::service('comment.manager')->addDefaultField('node', 'forum', 'another_comment_field', CommentItemInterface::OPEN, 'another_comment_field');
$this->drupalGet('node/' . $node->nid->value);
$this->assertResponse(200);
// Check that the field is now deleted.
$field_storage = FieldStorageConfig::loadByName('node', 'taxonomy_forums');
$this->assertNull($field_storage, 'The taxonomy_forums field storage has been deleted.');
}
示例2: testUpdatedSite
/**
* Tests that configuration has been updated.
*/
public function testUpdatedSite()
{
$key_to_be_removed = 'display.default.display_options.fields.nid';
/** @var \Drupal\Core\Config\Config $config_override */
$language_config_override = \Drupal::service('language.config_factory_override');
$config_override = $language_config_override->getOverride('es', 'views.view.content');
$this->assertEqual('Spanish ID', $config_override->get($key_to_be_removed)['label'], 'The spanish override for the missing field exists before updating.');
// Since the above view will be fixed by other updates that fix views
// configuration for example,
// views_post_update_update_cacheability_metadata(), also test configuration
// that has yet to be modified in an update path.
$config_override = $language_config_override->getOverride('es', 'system.cron');
$this->assertEqual('Should be cleaned by system_update_8200', $config_override->get('bogus_key'), 'The spanish override in system.cron exists before updating.');
$this->runUpdates();
/** @var \Drupal\Core\Config\Config $config_override */
$config_override = \Drupal::service('language.config_factory_override')->getOverride('es', 'views.view.content');
$this->assertNull($config_override->get($key_to_be_removed), 'The spanish override for the missing field has been removed.');
$config_override = $language_config_override->getOverride('es', 'system.cron');
$this->assertTrue($config_override->isNew(), 'After updating the system.cron spanish override does not exist.');
$this->assertTrue(empty($config_override->get()), 'After updating the system.cron spanish override has no data.');
// Test that the spanish overrides still work.
$this->drupalLogin($this->createUser(['access content overview']));
$this->drupalGet('admin/content', ['language' => \Drupal::languageManager()->getLanguage('es')]);
$this->assertText('Spanish Title');
$this->assertText('Spanish Author');
}
示例3: doGetActiveTrailIds
/**
* Helper method for ::getActiveTrailIds().
*/
protected function doGetActiveTrailIds($menu_name)
{
// Parent ids; used both as key and value to ensure uniqueness.
// We always want all the top-level links with parent == ''.
$active_trail = array('' => '');
// If a link in the given menu indeed matches the route, then use it to
// complete the active trail.
if ($active_link = $this->getActiveLink($menu_name)) {
if ($parents = $this->menuLinkManager->getParentIds($active_link->getPluginId())) {
$active_trail = $parents + $active_trail;
}
} else {
// No matching link, so check paths against current link.
$path = $this->getCurrentPathAlias();
$menu_parameters = new MenuTreeParameters();
$tree = \Drupal::menuTree()->load($menu_name, $menu_parameters);
foreach ($tree as $menu_link_route => $menu_link) {
$menu_url = $menu_link->link->getUrlObject();
$menu_path = $menu_url->toString();
// Check if this item's path exists in the current path.
// Also check if there is a langcode prefix.
$lang_prefix = '/' . \Drupal::languageManager()->getCurrentLanguage()->getId();
if (strpos($path, $menu_path) === 0 || strpos($lang_prefix . $path, $menu_path) === 0) {
if ($this->pathIsMoreSimilar($path, $menu_path)) {
$parents = array($menu_link_route => $menu_link_route);
$active_trail = $parents + $active_trail;
}
}
}
}
return $active_trail;
}
示例4: testDefaultTermLanguage
function testDefaultTermLanguage()
{
// Configure the vocabulary to not hide the language selector, and make the
// default language of the terms fixed.
$edit = array('default_language[langcode]' => 'bb', 'default_language[language_show]' => TRUE);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
$this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
$this->assertOptionSelected('edit-langcode', 'bb', 'The expected langcode was selected.');
// Make the default language of the terms to be the current interface.
$edit = array('default_language[langcode]' => 'current_interface', 'default_language[language_show]' => TRUE);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
$this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
$this->assertOptionSelected('edit-langcode', 'aa', "The expected langcode, 'aa', was selected.");
$this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
$this->assertOptionSelected('edit-langcode', 'bb', "The expected langcode, 'bb', was selected.");
// Change the default language of the site and check if the default terms
// language is still correctly selected.
$old_default = \Drupal::languageManager()->getDefaultLanguage();
$old_default->default = FALSE;
language_save($old_default);
$new_default = \Drupal::languageManager()->getLanguage('cc');
$new_default->default = TRUE;
language_save($new_default);
$edit = array('default_language[langcode]' => 'site_default', 'default_language[language_show]' => TRUE);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
$this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
$this->assertOptionSelected('edit-langcode', 'cc', "The expected langcode, 'cc', was selected.");
}
示例5: testFromAndReplyToHeader
/**
* Checks the From: and Reply-to: headers.
*/
public function testFromAndReplyToHeader()
{
$language = \Drupal::languageManager()->getCurrentLanguage();
// Use the state system collector mail backend.
$this->config('system.mail')->set('interface.default', 'test_mail_collector')->save();
// Reset the state variable that holds sent messages.
\Drupal::state()->set('system.test_mail_collector', array());
// Send an email with a reply-to address specified.
$from_email = 'Drupal <simpletest@example.com>';
$reply_email = 'someone_else@example.com';
\Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email);
// Test that the reply-to email is just the email and not the site name
// and default sender email.
$captured_emails = \Drupal::state()->get('system.test_mail_collector');
$sent_message = end($captured_emails);
$this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
$this->assertEqual($reply_email, $sent_message['headers']['Reply-to'], 'Message reply-to headers are set.');
$this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');
// Send an email and check that the From-header contains the site name.
\Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language);
$captured_emails = \Drupal::state()->get('system.test_mail_collector');
$sent_message = end($captured_emails);
$this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
$this->assertFalse(isset($sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.');
$this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');
}
示例6: testTranslatedBreadcrumbs
/**
* Test translated breadcrumbs.
*/
public function testTranslatedBreadcrumbs()
{
// Ensure non-translated breadcrumb is correct.
$breadcrumb = array(Url::fromRoute('<front>')->toString() => 'Home');
foreach ($this->terms as $term) {
$breadcrumb[$term->url()] = $term->label();
}
// The last item will not be in the breadcrumb.
array_pop($breadcrumb);
// Check the breadcrumb on the leaf term page.
$term = $this->getLeafTerm();
$this->assertBreadcrumb($term->urlInfo(), $breadcrumb, $term->label());
$languages = \Drupal::languageManager()->getLanguages();
// Construct the expected translated breadcrumb.
$breadcrumb = array(Url::fromRoute('<front>', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home');
foreach ($this->terms as $term) {
$translated = $term->getTranslation($this->translateToLangcode);
$url = $translated->url('canonical', ['language' => $languages[$this->translateToLangcode]]);
$breadcrumb[$url] = $translated->label();
}
array_pop($breadcrumb);
// Check for the translated breadcrumb on the translated leaf term page.
$term = $this->getLeafTerm();
$translated = $term->getTranslation($this->translateToLangcode);
$this->assertBreadcrumb($translated->urlInfo('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label());
}
示例7: testSiteNameTranslation
/**
* Tests translating the site name.
*/
function testSiteNameTranslation()
{
$adminUser = $this->drupalCreateUser(array('administer site configuration', 'administer languages'));
$this->drupalLogin($adminUser);
// Add a custom language.
$langcode = 'xx';
$name = $this->randomMachineName(16);
$edit = array('predefined_langcode' => 'custom', 'langcode' => $langcode, 'label' => $name, 'direction' => LanguageInterface::DIRECTION_LTR);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
\Drupal::languageManager()->getLanguageConfigOverride($langcode, 'system.site')->set('name', 'XX site name')->save();
$this->drupalLogout();
// The home page in English should not have the override.
$this->drupalGet('');
$this->assertNoText('XX site name');
// During path resolution the system.site configuration object is used to
// determine the front page. This occurs before language negotiation causing
// the configuration factory to cache an object without the correct
// overrides. We are testing that the configuration factory is
// re-initialised after language negotiation. Ensure that it applies when
// we access the XX front page.
// @see \Drupal\Core\PathProcessor::processInbound()
$this->drupalGet('xx');
$this->assertText('XX site name');
// Set the xx language to be the default language and delete the English
// language so the site is no longer multilingual and confirm configuration
// overrides still work.
$language_manager = \Drupal::languageManager()->reset();
$this->assertTrue($language_manager->isMultilingual(), 'The test site is multilingual.');
$this->config('system.site')->set('default_langcode', 'xx')->save();
ConfigurableLanguage::load('en')->delete();
$this->assertFalse($language_manager->isMultilingual(), 'The test site is monolingual.');
$this->drupalGet('xx');
$this->assertText('XX site name');
}
示例8: render
/**
* {@inheritdoc}
*/
public function render(ResultRow $values)
{
$value = $this->getValue($values);
$language = \Drupal::languageManager()->getLanguage($value);
$value = $language ? $language->getName() : '';
return $this->renderLink($this->sanitizeValue($value), $values);
}
示例9: testConfigOverrideImport
/**
* Tests that language can be enabled and overrides are created during a sync.
*/
public function testConfigOverrideImport()
{
ConfigurableLanguage::createFromLangcode('fr')->save();
/* @var \Drupal\Core\Config\StorageInterface $sync */
$sync = \Drupal::service('config.storage.sync');
$this->copyConfig(\Drupal::service('config.storage'), $sync);
// Uninstall the language module and its dependencies so we can test
// enabling the language module and creating overrides at the same time
// during a configuration synchronization.
\Drupal::service('module_installer')->uninstall(array('language'));
// Ensure that the current site has no overrides registered to the
// ConfigFactory.
$this->rebuildContainer();
/* @var \Drupal\Core\Config\StorageInterface $override_sync */
$override_sync = $sync->createCollection('language.fr');
// Create some overrides in sync.
$override_sync->write('system.site', array('name' => 'FR default site name'));
$override_sync->write('system.maintenance', array('message' => 'FR message: @site is currently under maintenance. We should be back shortly. Thank you for your patience'));
$this->configImporter()->import();
$this->rebuildContainer();
\Drupal::service('router.builder')->rebuild();
$override = \Drupal::languageManager()->getLanguageConfigOverride('fr', 'system.site');
$this->assertEqual('FR default site name', $override->get('name'));
$this->drupalGet('fr');
$this->assertText('FR default site name');
$this->drupalLogin($this->rootUser);
$this->drupalGet('admin/config/development/maintenance/translate/fr/edit');
$this->assertText('FR message: @site is currently under maintenance. We should be back shortly. Thank you for your patience');
}
示例10: testCommentTypeCreation
/**
* Tests creating a comment type programmatically and via a form.
*/
public function testCommentTypeCreation()
{
// Create a comment type programmatically.
$type = $this->createCommentType('other');
$comment_type = CommentType::load('other');
$this->assertTrue($comment_type, 'The new comment type has been created.');
// Login a test user.
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/structure/comment/manage/' . $type->id());
$this->assertResponse(200, 'The new comment type can be accessed at the edit form.');
// Create a comment type via the user interface.
$edit = array('id' => 'foo', 'label' => 'title for foo', 'description' => '', 'target_entity_type_id' => 'node');
$this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save'));
$comment_type = CommentType::load('foo');
$this->assertTrue($comment_type, 'The new comment type has been created.');
// Check that the comment type was created in site default language.
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
$this->assertEqual($comment_type->language()->getId(), $default_langcode);
// Edit the comment-type and ensure that we cannot change the entity-type.
$this->drupalGet('admin/structure/comment/manage/foo');
$this->assertNoField('target_entity_type_id', 'Entity type file not present');
$this->assertText(t('Target entity type'));
// Save the form and ensure the entity-type value is preserved even though
// the field isn't present.
$this->drupalPostForm(NULL, array(), t('Save'));
\Drupal::entityManager()->getStorage('comment_type')->resetCache(array('foo'));
$comment_type = CommentType::load('foo');
$this->assertEqual($comment_type->getTargetEntityTypeId(), 'node');
}
示例11: addLanguage
/**
* Adds a language.
*
* @param string $langcode
* The language code of the language to add.
*/
protected function addLanguage($langcode)
{
$edit = array('predefined_langcode' => $langcode);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$this->container->get('language_manager')->reset();
$this->assertTrue(\Drupal::languageManager()->getLanguage($langcode), SafeMarkup::format('Language %langcode added.', array('%langcode' => $langcode)));
}
示例12: setUp
protected function setUp()
{
parent::setUp();
// Create and login user.
$web_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration'));
$this->drupalLogin($web_user);
// Enable French language.
$edit = array();
$edit['predefined_langcode'] = 'fr';
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
// Make French the default language.
$edit = array('site_default_language' => 'fr');
$this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
// Delete English.
$this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
// Changing the default language causes a container rebuild. Therefore need
// to rebuild the container in the test environment.
$this->rebuildContainer();
// Verify that French is the only language.
$this->container->get('language_manager')->reset();
$this->assertFalse(\Drupal::languageManager()->isMultilingual(), 'Site is mono-lingual');
$this->assertEqual(\Drupal::languageManager()->getDefaultLanguage()->getId(), 'fr', 'French is the default language');
// Set language detection to URL.
$edit = array('language_interface[enabled][language-url]' => TRUE);
$this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
}
示例13: applyDefaultValue
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE)
{
// Default to the site's default language. When language module is enabled,
// this behavior is configurable, see language_field_info_alter().
$this->setValue(array('value' => \Drupal::languageManager()->getDefaultLanguage()->getId()), $notify);
return $this;
}
示例14: testEmailExampleBasic
/**
* Test our new email form.
*
* Tests for the following:
*
* - A link to the email_example in the Tools menu.
* - That you can successfully access the email_example page.
*/
public function testEmailExampleBasic()
{
// Test for a link to the email_example in the Tools menu.
$this->drupalGet('');
$this->assertResponse(200, 'The Home page is available.');
$this->assertLinkByHref('examples/email_example');
// Verify if we can successfully access the email_example page.
$this->drupalGet('examples/email_example');
$this->assertResponse(200, 'The Email Example description page is available.');
// Verifiy email form has email & message fields.
$this->assertFieldById('edit-email', NULL, 'The email field appears.');
$this->assertFieldById('edit-message', NULL, 'The message field appears.');
// Verifiy email form is submitted.
$edit = array('email' => 'example@example.com', 'message' => 'test');
$this->drupalPostForm('examples/email_example', $edit, t('Submit'));
$this->assertResponse(200);
// Verifiy comfirmation page.
$this->assertText(t('Your message has been sent.'), 'The text "Your message has been sent." appears on the email example page.', 'Form response with the right message.');
$this->assertMailString('to', $edit['email'], 1);
// Verifiy correct email recieved.
$from = \Drupal::config('system.site')->get('mail');
$t_options = array('langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId());
$this->assertMailString('subject', t('E-mail sent from @site-name', array('@site-name' => $from), $t_options), 1);
$this->assertMailString('body', $edit['message'], 1);
$this->assertMailString('body', t("\n--\nMail altered by email_example module.", array(), $t_options), 1);
}
示例15: testStatisticsTokenReplacement
/**
* Creates a node, then tests the statistics tokens generated from it.
*/
function testStatisticsTokenReplacement()
{
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
// Create user and node.
$user = $this->drupalCreateUser(array('create page content'));
$this->drupalLogin($user);
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->id()));
// Hit the node.
$this->drupalGet('node/' . $node->id());
// Manually calling statistics.php, simulating ajax behavior.
$nid = $node->id();
$post = http_build_query(array('nid' => $nid));
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$client = \Drupal::service('http_client_factory')->fromOptions(['config/curl' => [CURLOPT_TIMEOUT => 10]]);
$client->post($stats_path, array('headers' => $headers, 'body' => $post));
$statistics = statistics_get($node->id());
// Generate and test tokens.
$tests = array();
$tests['[node:total-count]'] = 1;
$tests['[node:day-count]'] = 1;
$tests['[node:last-view]'] = format_date($statistics['timestamp']);
$tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
// Test to make sure that we generated something for each token.
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$output = \Drupal::token()->replace($input, array('node' => $node), array('langcode' => $language_interface->getId()));
$this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', array('%token' => $input)));
}
}