Problem/Motivation

The function getAvailableGlobalTokens in class PluginBase (core/modules/views/src/Plugins/views/PluginBase.php) contains an array union operator.

  public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) {
    $info = \Drupal::token()->getInfo();
    // Site and view tokens should always be available.
    $types += ['site', 'view'];
    $available = array_intersect_key($info['tokens'], array_flip($types));

This does not deliver the expected array merge result (refer to https://www.php.net/manual/en/language.operators.array.php), but rather the elements from the right hand array are appended to the left hand array where they have a different key. Therefore if the argument $types passed into the function above has the value "['date']", the resulting $types array will be "['date', 'view']". This is not what is required.

There may well be other instances of incorrect use of the array union operator in the views module.

Steps to reproduce

Proposed resolution

The function should use an array_merge as follows:

  public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) {
    $info = \Drupal::token()->getInfo();
    // Site and view tokens should always be available.
    $types = array_unique(array_merge($types, ['site', 'view']));
    $available = array_intersect_key($info['tokens'], array_flip($types));

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3152267

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:

Comments

jlscott created an issue. See original summary.

jayelless’s picture

Status: Active » Needs review
StatusFileSize
new733 bytes

Patch attached. This issue is also present in 8.9.x, and the patch also fixes the problems in that version.

kevinvhengst’s picture

Hi James,

Good catch! Your patch seems to resolve the issue you mentioned.

Test result prior to your change When calling getAvailableGlobalTokes(FALSE, $types) Where $types is ['test-1', 'test-2']: $types after the union operator is equal to ['test-1', 'test-2'].

Test result with your patch applied using the same arguments as above:
$types using array_unique(array_merge($types, ['site', 'view'])); is equal to ['test-1', 'test-2', 'site', view'] as expected.

kevinvhengst’s picture

Status: Needs review » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 3152267-invalid-array-union-2.patch, failed testing. View results

s_bhandari’s picture

Assigned: Unassigned » s_bhandari
s_bhandari’s picture

StatusFileSize
new713 bytes

Added a patch to address the issue.

s_bhandari’s picture

Assigned: s_bhandari » Unassigned
Status: Needs work » Needs review

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

Drupal 9.0.10 was released on December 3, 2020 and is the final full bugfix release for the Drupal 9.0.x series. Drupal 9.0.x will not receive any further development aside from security fixes. Sites should update to Drupal 9.1.0 to continue receiving regular bugfixes.

Drupal-9-only bug reports should be targeted for the 9.1.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.2.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.1.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should 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.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

andreasderijcke’s picture

Status: Needs review » Reviewed & tested by the community

Issue is still present in 10.1.x: https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/modules/vie...

Patch still applies in 9.4.8.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

The fix in #7 looks great. It'd be nice to have some test coverage of this to prove we'd fixed it and also ensure we don't break it in the future.

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

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

quietone’s picture

Issue summary: View changes
quietone’s picture

quietone’s picture

Status: Needs work » Needs review

I converted to an MR and added a test. Did I miss any test cases?

quietone’s picture

Issue tags: -Needs tests
lendude’s picture

Status: Needs review » Needs work

Left some comments

quietone’s picture

Status: Needs work » Needs review

@lendude, thanks for the prompt review.

I've updated the test. I didn't see a way to create a test for duplicates but added the array_unique anyway. Then tests failed with an 'array to string conversion' because the array is multidimensional and "array_unique() is not intended to work on multi dimensional arrays".

lendude’s picture

Status: Needs review » Reviewed & tested by the community

Well that is a good argument against array_unique! Thanks for trying!

Then this looks ready

  • godotislate committed bb1b9c25 on main
    fix: #3152267 Invalid use of array union operator
    
    By: s_bhandari
    By:...

  • godotislate committed 02b66e09 on 11.x
    fix: #3152267 Invalid use of array union operator
    
    By: s_bhandari
    By:...

  • godotislate committed 8976ec40 on 11.4.x
    fix: #3152267 Invalid use of array union operator
    
    By: s_bhandari
    By:...
godotislate’s picture

Version: main » 11.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed bb1b9c2 to main, 02b66e0 to 11.x, and 8976ec4 to 11.4.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.