Links with query params with integer keys reset the keys

Created on 7 January 2021, over 3 years ago
Updated 1 February 2024, 5 months ago

Problem/Motivation

Using the link field with a link like this: /my-page?myparam[2021]=2021 when printed looks like this: /my-page?myparam[0]=2021&myparam[1]=2021

This is a problem when you try to add links to filtered views because even though the filters get applied, the boxes don't get checked.

Steps to reproduce

- Create a link field
- Point to an internal view page with filters in the url like: /my-page?myparam[2021]=2021

Expected behavior

- Link will be printed as entered

Current behavior

- Link gets printed like this: /my-page?myparam[0]=2021&myparam[1]=2021

Proposed resolution

Issue seems to be in line https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C... that gets the options wicked up.

If we change that line to

$options = NestedArray::mergeDeepArray([$element['#url']->getOptions(), $element['#options']], TRUE);

it will work, but I'm unsure of the underlying effects of this.

Remaining tasks

- Analyze proposed solution
- Create patch and commit

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

🐛 Bug report
Status

Closed: duplicate

Version

11.0 🔥

Component
Render 

Last updated about 22 hours ago

Created by

🇨🇷Costa Rica kporras07

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇪🇸Spain pcambra Spain, 🇪🇺

    I've found this bug on a site today and patch in #4 works great.

  • Status changed to Needs work 6 months ago
  • The Needs Review Queue Bot tested this issue.

    While you are making the above changes, we recommend that you convert this patch to a merge request . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

  • First commit to issue fork.
  • 🇮🇳India arisen Goa

    The patch in #4 resolves the issue. Created a MR.
    mergeDeep is internally using mergeDeepArray with preserve integer keys set to FALSE by default.
    In the patch we are passing it as TRUE to keep the keys as it is. Should be fine IMO.

    This might need some tests.

  • 🇪🇸Spain pcambra Spain, 🇪🇺

    @arisen seems you've created the branch but not the actual MR?

  • Merge request !6212preserving integer parameter keys → (Open) created by arisen
  • Status changed to Needs review 5 months ago
  • 🇪🇸Spain pcambra Spain, 🇪🇺
  • 🇮🇳India arisen Goa

    What would be the ideal place to add a Kernel Test for this?

      /**
       * Test Link element with integer key parameters
       */
      public function testLinkWithIntegerKeyParams() {
        $link_array = [
          '#type' => 'link',
          '#title' => 'foo',
          '#url' => Url::fromUri('internal:/my-page?myparam[2021]=2021'),
        ];
        $link_rendered = (string) \Drupal::service('renderer')->renderRoot($link_array);
        $expected_link = '<a href="/my-page?myparam%5B2021%5D=2021">foo</a>';
        $this->assertSame($expected_link, $link_rendered);
      }

    Wondering if it can added as part of core/modules/system/tests/src/Kernel/Common/UrlTest.php or core/tests/Drupal/KernelTests/Core/Render/Element/RenderElementTypesTest.php

  • Status changed to Needs work 5 months ago
  • 🇺🇸United States smustgrave

    For the tests.

  • Pipeline finished with Failed
    5 months ago
    Total: 1012561s
    #78697
  • Pipeline finished with Failed
    5 months ago
    Total: 192s
    #84063
  • Pipeline finished with Success
    5 months ago
    Total: 477s
    #84066
  • Status changed to Needs review 5 months ago
  • Status changed to Needs work 5 months ago
  • 🇺🇸United States smustgrave

    Ran the test only feature and it seemed to pass without the fix https://git.drupalcode.org/issue/drupal-3191456/-/jobs/707698 so not testing what we think it is. While updating please add :void to any new test function.

  • Status changed to Needs review 5 months ago
  • 🇮🇳India arisen Goa

    @smustgrave The issue is no longer reproducible on D11 but still existing on D10(checked on 10.2.2).

    Seems like NestedArray::mergeDeep was not the root cause.
    Looks like there has been some changes to the link preprocessing logic.

    $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
    On D11, is an empty array so the issue is no longer occurring, whereas on D10 its having the params info same as $element['#url']->getOptions().

  • Status changed to Needs work 5 months ago
  • 🇺🇸United States smustgrave

    Then think we need some research about what issue was committed to 11 that didn’t get backported to 10 that fixed this issue.

  • Status changed to Needs review 5 months ago
  • 🇮🇳India arisen Goa

    @smustgrave found the issue 🐛 Query string duplications Fixed
    It's already been added to 10.2 also.

  • 🇺🇸United States smustgrave

    Then this should be closed.

  • Status changed to Closed: duplicate 5 months ago
  • 🇺🇸United States smustgrave

    Note the related issue was merged 2 days ago so not in a tagged release yet

Production build 0.69.0 2024