core/includes/theme.inc | 22 ++++---------- .../Drupal/language/Plugin/Block/LanguageBlock.php | 1 + .../language/Tests/LanguageSwitchingTest.php | 32 ++++++++++---------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f7b00bf..816016d 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1304,8 +1304,8 @@ function theme_links($variables) { // Add a "data-drupal-link-query" attribute to let the // drupal.active-link library know the query in a standardized // manner. - if (!empty($link['#options']['query'])) { - $query = $link['#options']['query']; + if (!empty($link['query'])) { + $query = $link['query']; ksort($query); $li_attributes['data-drupal-link-query'] = Json::encode($query); } @@ -1330,23 +1330,13 @@ function theme_links($variables) { } } elseif (isset($link['href'])) { - if (!isset($link['#options'])) { - $link['#options'] = array(); - } - if (!isset($link['#options']['language'])) { - $link['#options']['language'] = NULL; - } - if (!isset($link['#options']['query'])) { - $link['#options']['query'] = array(); - } - // @see l() if (\Drupal::currentUser()->isAuthenticated()) { // Add a "data-drupal-link-query" attribute to let the // drupal.active-link library know the query in a standardized // manner. - if (!empty($link['#options']['query'])) { - $query = $link['#options']['query']; + if (!empty($link['query'])) { + $query = $link['query']; ksort($query); $li_attributes['data-drupal-link-query'] = Json::encode($query); } @@ -1380,9 +1370,9 @@ function theme_links($variables) { // An active link's path is equal to the current path. $url_is_active = ($link['href'] == $active['path'] || ($link['href'] == '' && $active['front_page'])) // The language of an active link is equal to the current language. - && (empty($link['#options']['language']) || $link['#options']['language']->id == $active['language']) + && (empty($link['language']) || $link['language']->id == $active['language']) // The query parameters of an active link are equal to the current parameters. - && ($link['#options']['query'] == $active['query']); + && (empty($link['query']) || $link['query'] == $active['query']); // Add the "active" class if appropriate. if ($url_is_active) { diff --git a/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php b/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php index fae6081..e347de5 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php @@ -49,6 +49,7 @@ public function build() { "language-switcher-{$links->method_id}", ), ), + '#set_active_class' => TRUE, ); } return $build; diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php index e1ebfc0..826fcf9 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageSwitchingTest.php @@ -72,28 +72,28 @@ protected function doTestLanguageBlockAuthenticated($block_label) { $this->drupalGet(''); $this->assertText($block_label, 'Language switcher block found.'); - // Assert that only the current language is marked as active. + // Assert that each list item and anchor element has the appropriate data- + // attributes. list($language_switcher) = $this->xpath('//div[@id=:id]/div[contains(@class, "content")]', array(':id' => 'block-test-language-block')); - $links = array( - 'active' => array(), - 'inactive' => array(), - ); + $list_items = array(); $anchors = array(); - foreach ($language_switcher->ul->li as $link) { - $classes = explode(" ", (string) $link['class']); + foreach ($language_switcher->ul->li as $list_item) { + $classes = explode(" ", (string) $list_item['class']); list($langcode) = array_intersect($classes, array('en', 'fr')); - if (in_array('active', $classes)) { - $links['active'][] = $langcode; - } - else { - $links['inactive'][] = $langcode; - } + $list_items[] = array( + 'langcode_class' => $langcode, + 'data-drupal-link-system-path' => (string) $list_item['data-drupal-link-system-path'], + ); $anchors[] = array( - 'hreflang' => (string) $link->a['hreflang'], - 'data-drupal-link-system-path' => (string) $link->a['data-drupal-link-system-path'], + 'hreflang' => (string) $list_item->a['hreflang'], + 'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'], ); } - $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language list item is marked as active on the language switcher block.'); + $expected_list_items = array( + 0 => array('langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'), + 1 => array('langcode_class' => 'fr', 'data-drupal-link-system-path' => 'user/2'), + ); + $this->assertIdentical($list_items, $expected_list_items, 'The list items have the correct attributes that will allow the drupal.active-link library to mark them as active.'); $expected_anchors = array( 0 => array('hreflang' => 'en', 'data-drupal-link-system-path' => 'user/2'), 1 => array('hreflang' => 'fr', 'data-drupal-link-system-path' => 'user/2'),