If the visited path has query parameters the language switchers ignores them when generating the links to the other languages. Patch passes query parameters to theme('links').

CommentFileSizeAuthor
#9 local_854104_9.patch474 bytesfprevos2
#2 locale_854104_2.patch450 bytesmartin_q
locale.patch460 bytesjax

Comments

Status: Needs review » Needs work

The last submitted patch, locale.patch, failed testing.

martin_q’s picture

StatusFileSize
new450 bytes

Doesn't the query need to be an array? If so, I'd suggest changing

'query'      => drupal_query_string_encode($_GET, array('q')),

to

'query'      => array_diff_key($_GET, array('q' => '')),

as in the attached patch.

jax’s picture

No it doesn't. It can be or a string or an array. drupal_query_string_encode() is used in a lot of other places in the same way.

xamount’s picture

Status: Needs work » Reviewed & tested by the community

I have applied the original patch and it worked for me. It would be nice if this patch could be submitted...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, locale_854104_2.patch, failed testing.

xamount’s picture

Status: Needs work » Needs review

locale.patch queued for re-testing.

dddave’s picture

Status: Needs review » Needs work

#825264: Language switcher does not preserve query string marked as duplicate.
Also setting to needs works as the patch seems to fail (is testing for D6 back?) and #3 dissents with the direction of the patch.

fprevos2’s picture

Any update on this issue?

fprevos2’s picture

StatusFileSize
new474 bytes

Try to fix the patch.

dddave’s picture

Status: Needs work » Needs review
pawel_r’s picture

thx, it solved my problems with language switching while using FAQ ASK module. Will it go to main branch?

itamair’s picture

Issue summary: View changes

all this is easily solved using hook_language_switch_links_alter ... and forcing the link/url to keep the 'query' attributes,
as described here https://www.drupal.org/node/1483412#comment-6747320

martin_q’s picture

Yes, that is a reasonable workaround - doesn't "solve" the bug in core, but it fixes the symptoms... :)

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.

Stevel’s picture

Version: 6.x-dev » 7.x-dev
Status: Closed (outdated) » Needs work

This seems to be still happening with 7.x. Patch needs to be changed for 7.x using drupal_get_query_parameters and drupal_http_build_query

mvc’s picture

Version: 7.x-dev » 8.3.x-dev

In fact, this still happens with 8.x as well.

As a followup to #12 above, a similar workaround is possible in D8:

/**
 * Implementation of hook_language_switch_links_alter().
 *
 * Retain query parameters; works around core bug, see https://www.drupal.org/node/854104
 */
function MYMODULE_language_switch_links_alter(&$links, $type, $path) {
  $request = \Drupal::request();
  $params = $request->query->all();

  foreach ($links as &$link) {
    $link['query'] = $params;
  }
}

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kbrinner’s picture

I too could fix the issue by using the hook_language_switch_links_alter ... and forcing the link/url to keep the 'query' attributes per this comment, but was looking for a fix to Drupal 7 core perhaps? When I tried to look into patching the locale.module for Drupal 7, I made a little progress but ran into another roadblock.

In locale.module, if you do something like the following:

function locale_block_view($type) {
  if (drupal_multilingual()) {

    // Get query parameters
    $params = drupal_get_query_parameters();
    $query = drupal_http_build_query($params);

   // Add query parameters as needed
    if (drupal_is_front_page()) {
      $path = '<front>';
    } else if ($params) {
      $path = current_path() . '?' . $query;
    } else {
      $path = current_path();
    }

    $links = language_negotiation_get_switch_links($type, $path);

    if (isset($links->links)) {
      drupal_add_css(drupal_get_path('module', 'locale') . '/locale.css');
      $class = "language-switcher-{$links->provider}";
      $variables = array('links' => $links->links, 'attributes' => array('class' => array($class)));
      $block['content'] = theme('links__locale_block', $variables);
      $block['subject'] = t('Languages');
      return $block;
    }
  }
}

You still don't get the desired result. While the $path variable is set correctly initially, once it is passed through the theme('links__locale_block', $variables); per the $block['content'], the paths are re-written from the desired path ie [my-page]?term=[tid] to [my-page]%3Fterm%3D[tid]?term=[tid]. In my case this was a Views exposed filter that is filtering per taxonomy terms. The ?term=[tid] seems to get duplicated - replacing the ? and = with the escaped hexadecimal ASCII representations for URL Character codes for the path. It also leaves duplicates the correct query parameter and puts it at the end of the path. At this point the workaround with the hook_language_switch_links_alter works so I won't investigate further, but if anyone knows how to correct the issue with the theme() function rewriting the path incorrectly, I'm sure it would be helpful to many.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joseph.olstad’s picture

for ideas on how to get a working language switcher that does get parameters properly and handles the various entity types and multiple languages enabled or disabled, see this language switcher for D7, works perfectly.

https://cgit.drupalcode.org/wetkit_language/tree/wetkit_language.module?...

I'm not sure if this is still an issue in D8 core, but apparently it is if this issue is still open.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Closed (cannot reproduce)
Issue tags: +Bug Smash Initiative

I tested this on Drupal 11.x today and find that the language switcher does retain the query parameters.

Therefore, closing as cannot reproduce. If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

Thanks!