function FieldPluginBase::getTokenValuesRecursive

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::getTokenValuesRecursive()
  2. 8.9.x core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::getTokenValuesRecursive()
  3. 10 core/modules/views/src/Plugin/views/field/FieldPluginBase.php \Drupal\views\Plugin\views\field\FieldPluginBase::getTokenValuesRecursive()

Recursive function to add replacements for nested query string parameters.

E.g. if you pass in the following array: [ 'foo' => [ 'a' => 'value', 'b' => 'value', 'c.d' => 'invalid value', '&invalid' => 'invalid value', ], 'bar' => [ 'a' => 'value', 'b' => [ 'c' => value, ], ], ];

Would yield the following array of tokens: [ '{{ arguments.foo.a }}' => 'value', '{{ arguments.foo.b }}' => 'value', '{{ arguments.bar.a }}' => 'value', '{{ arguments.bar.b.c }}' => 'value', ];

Parameters

$array: An array of values.

$parent_keys: An array of parent keys. This will represent the array depth.

Return value

array An array of available tokens, with nested keys representative of the array structure.

1 call to FieldPluginBase::getTokenValuesRecursive()
FieldPluginBase::getRenderTokens in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
Gets the 'render' tokens to use for advanced rendering.

File

core/modules/views/src/Plugin/views/field/FieldPluginBase.php, line 1736

Class

FieldPluginBase
Base class for views fields.

Namespace

Drupal\views\Plugin\views\field

Code

protected function getTokenValuesRecursive(array $array, array $parent_keys = []) {
    $tokens = [];
    foreach ($array as $param => $val) {
        if (!is_numeric($param) && preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $param) === 0) {
            // Skip as the parameter is not a valid Twig variable name.
            continue;
        }
        if (is_array($val)) {
            // Copy parent_keys array, so we don't affect other elements of this
            // iteration.
            $child_parent_keys = $parent_keys;
            $child_parent_keys[] = $param;
            // Get the child tokens.
            $child_tokens = $this->getTokenValuesRecursive($val, $child_parent_keys);
            // Add them to the current tokens array.
            $tokens += $child_tokens;
        }
        else {
            // Create a token key based on array element structure.
            $token_string = !empty($parent_keys) ? implode('.', $parent_keys) . '.' . $param : $param;
            $tokens['{{ arguments.' . $token_string . ' }}'] = strip_tags(Html::decodeEntities($val));
        }
    }
    return $tokens;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.