The function getAvailableGlobalTokens in class DataExport (src/Plugins/views/display/DataExport.php) contains an array union operator.

  public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) {
    $types += ['date'];
    return parent::getAvailableGlobalTokens($prepared, $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 (ie original) array where they have a different key. Therefore if the argument $types passed into the function above has the value "['site', 'view']", the resulting $types array will be "['site', 'view']". This is not what is required.

The function should use an array_merge as follows:

  public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) {
    $types = array_unique(array_merge($types, ['date']));
    return parent::getAvailableGlobalTokens($prepared, $types);
  }

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

Comments

jlscott created an issue. See original summary.

jayelless’s picture

Issue summary: View changes
jayelless’s picture

Status: Active » Needs review
StatusFileSize
new542 bytes

Patch attached.

jayelless’s picture

StatusFileSize
new558 bytes

It is useful to add an additional set of tokens relating to "current-user" to the available global tokens so I have prepared a revised patch to achieve that.

andreasderijcke’s picture

The array_unique in the patch isn't really required, because in the base class array_flip($types) is called which will eliminate any duplicates.
In anyway, the patch works.

This aside, having an event to be able to attach other tokens types would be nice, so you don't need patches to add your own.

andreasderijcke’s picture

Status: Needs review » Reviewed & tested by the community
andreasderijcke’s picture

andreasderijcke’s picture

steven jones’s picture

I think we should change the line to something like this:

$types = array_merge($types, ['date']);

I.e. just fix the original problem. We can introduce other tokens in other issues etc.

I note that until #3152267: Invalid use of array union operator is sorted this won't make much of a difference anyway.

  • Steven Jones committed 1f65a7f2 on 8.x-1.x
    Issue #3152272 by jlscott, andreasderijcke, Steven Jones: Invalid use of...
steven jones’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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