function Container::getAlternatives

Same name in other branches
  1. 9 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::getAlternatives()
  2. 8.9.x core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::getAlternatives()
  3. 10 core/lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::getAlternatives()

Provides alternatives for a given array and key.

Parameters

string $search_key: The search key to get alternatives for.

array $keys: The search space to search for alternatives in.

Return value

string[] An array of strings with suitable alternatives.

2 calls to Container::getAlternatives()
Container::getParameterAlternatives in core/lib/Drupal/Component/DependencyInjection/Container.php
Provides alternatives in case a parameter was not found.
Container::getServiceAlternatives in core/lib/Drupal/Component/DependencyInjection/Container.php
Provides alternatives in case a service was not found.

File

core/lib/Drupal/Component/DependencyInjection/Container.php, line 501

Class

Container
Provides a container optimized for Drupal's needs.

Namespace

Drupal\Component\DependencyInjection

Code

protected function getAlternatives($search_key, array $keys) {
    $alternatives = [];
    foreach ($keys as $key) {
        $lev = levenshtein($search_key, $key);
        if ($lev <= strlen($search_key) / 3 || str_contains($key, $search_key)) {
            $alternatives[] = $key;
        }
    }
    return $alternatives;
}

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