Language switcher block highlights all languages as active on the front page. From: Damien Tournoud --- includes/common.inc | 5 ++- includes/theme.inc | 4 ++ modules/locale/locale.test | 76 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git includes/common.inc includes/common.inc index 98db3fe..3110165 100644 --- includes/common.inc +++ includes/common.inc @@ -1601,6 +1601,8 @@ function drupal_attributes($attributes = array()) { * an HTML string containing a link to the given path. */ function l($text, $path, array $options = array()) { + global $language; + // Merge in defaults. $options += array( 'attributes' => array(), @@ -1608,7 +1610,8 @@ function l($text, $path, array $options = array()) { ); // Append active class. - if ($path == $_GET['q'] || ($path == '' && drupal_is_front_page())) { + if (($path == $_GET['q'] || ($path == '' && drupal_is_front_page())) && + (empty($options['language']) || $options['language']->language == $language->language)) { if (isset($options['attributes']['class'])) { $options['attributes']['class'] .= ' active'; } diff --git includes/theme.inc includes/theme.inc index 68998ca..214dae7 100644 --- includes/theme.inc +++ includes/theme.inc @@ -1134,6 +1134,7 @@ function theme_status_messages($display = NULL) { * A string containing an unordered list of links. */ function theme_links($links, $attributes = array('class' => 'links')) { + global $language; $output = ''; if (count($links) > 0) { @@ -1152,7 +1153,8 @@ function theme_links($links, $attributes = array('class' => 'links')) { if ($i == $num_links) { $class .= ' last'; } - if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '' && drupal_is_front_page()))) { + if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '' && drupal_is_front_page())) + && (empty($link['language']) || $link['language']->language == $language->language)) { $class .= ' active'; } $output .= ' $class)) . '>'; diff --git modules/locale/locale.test modules/locale/locale.test index 3532da5..531ea4b 100644 --- modules/locale/locale.test +++ modules/locale/locale.test @@ -196,3 +196,79 @@ msgstr "dimanche" EOF; } } + +/** + * Functional tests for the language switching feature. + */ +class LanguageSwitchingFunctionalTest extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('Language switching'), + 'description' => t('Tests for the language switching feature.'), + 'group' => t('Locale'), + ); + } + + function setUp() { + parent::setUp('locale'); + + // Create and login user + $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'translate interface', 'access administration pages')); + $this->drupalLogin($admin_user); + } + + function testLanguageBlock() { + // Enable the language switching block. + $edit = array( + 'locale_language-switcher[region]' => 'left', + ); + $this->drupalPost('admin/build/block', $edit, t('Save blocks')); + + // Add language. + $edit = array( + 'langcode' => 'fr', + ); + $this->drupalPost('admin/settings/language/add', $edit, t('Add language')); + + // Set language negotiation. + $edit = array( + 'language_negotiation' => LANGUAGE_NEGOTIATION_PATH_DEFAULT, + ); + $this->drupalPost('admin/settings/language/configure', $edit, t('Save settings')); + + // Assert that the language switching block is displayed on the frontpage. + $this->drupalGet(''); + $this->assertText(t('Languages')); + + // Assert that only the current language is marked as active. + list($language_switcher) = $this->xpath('//div[@id="block-locale-language-switcher"]'); + $links = array( + 'active' => array(), + 'inactive' => array(), + ); + $anchors = array( + 'active' => array(), + 'inactive' => array(), + ); + foreach ($language_switcher->div->ul->li as $link) { + $classes = explode(" ", (string) $link['class']); + list($language) = array_intersect($classes, array('en', 'fr')); + if (in_array('active', $classes)) { + $links['active'][] = $language; + } + else { + $links['inactive'][] = $language; + } + $anchor_classes = explode(" ", (string) $link->a['class']); + if (in_array('active', $anchor_classes)) { + $anchors['active'][] = $language; + } + else { + $anchors['inactive'][] = $language; + } + } + $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), t('Only the current language list item is marked as active on the language switcher block')); + $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), t('Only the current language anchor is marked as active on the language switcher block')); + } +}