Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FieldPluginBase.php

function 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 1738

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal