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

Issue fork drupal-3191456

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kporras07 created an issue. See original summary.

cilefen’s picture

kporras07’s picture

Hmmm, not 100% sure but it looks related. I guess we could concentrate discussion only in one of the issues and then test the other one when fixed.

I'm not sure which issue is the best to keep the discussion.

kporras07’s picture

I'm using this patch in the site where I found this issue. Hopefully it will help to trigger the discussion

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.

pcambra’s picture

Status: Active » Needs review

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

needs-review-queue-bot’s picture

Status: Needs review » Needs work

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.)

arisen made their first commit to this issue’s fork.

arisen’s picture

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.

pcambra’s picture

Issue tags: +Needs tests

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

pcambra’s picture

Status: Needs work » Needs review
arisen’s picture

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

smustgrave’s picture

Status: Needs review » Needs work

For the tests.

arisen’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
smustgrave’s picture

Status: Needs review » Needs work

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.

arisen’s picture

Status: Needs work » Needs review

@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, $element['#options'] is an empty array so the issue is no longer occurring, whereas on D10 its having the params info same as $element['#url']->getOptions().

smustgrave’s picture

Status: Needs review » Needs work

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

arisen’s picture

Status: Needs work » Needs review

@smustgrave found the issue https://www.drupal.org/project/drupal/issues/2885351
It's already been added to 10.2 also.

smustgrave’s picture

Then this should be closed.

smustgrave’s picture

Status: Needs review » Closed (duplicate)

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