function Mapping::getPossibleTypes

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Config/Schema/Mapping.php \Drupal\Core\Config\Schema\Mapping::getPossibleTypes()

Returns all possible types for the type with the given name.

Parameters

string $name: Configuration name or key.

Return value

string[] All possible types for a given type. For example, `core_date_format_pattern.[%parent.locked]` will return:

  • `core_date_format_pattern.0`
  • `core_date_format_pattern.1`

If a fallback name is available, that will be returned too. In this example, that would be `core_date_format_pattern.*`.

1 call to Mapping::getPossibleTypes()
Mapping::getDynamicallyValidKeys in core/lib/Drupal/Core/Config/Schema/Mapping.php
Gets all dynamically valid keys.

File

core/lib/Drupal/Core/Config/Schema/Mapping.php, line 257

Class

Mapping
Defines a mapping configuration element.

Namespace

Drupal\Core\Config\Schema

Code

protected function getPossibleTypes(string $name) : array {
  // First, parse from e.g.
  // `module.something.foo_[%parent.locked]`
  // this:
  // `[%parent.locked]`
  // or from
  // `[%parent.%parent.%type].third_party.[%key]`
  // this:
  // `[%parent.%parent.%type]` and `[%key]`.
  // And collapse all these to just `[]`.
  // @see \Drupal\Core\Config\TypedConfigManager::replaceVariable()
  $matches = [];
  if (preg_match_all('/(\\[[^\\]]+\\])/', $name, $matches) >= 1) {
    $name = str_replace($matches[0], '[]', $name);
  }
  // Then, replace all `[]` occurrences with `.*` and escape all periods for
  // use in a regex. So:
  // `module\.something\.foo_.*`
  // or
  // `.*\.third_party\..*`
  $regex = str_replace([
    '.',
    '[]',
  ], [
    '\\.',
    '.*',
  ], $name);
  // Now find all possible types:
  // 1. `module.something.foo_foo`, `module.something.foo_bar`, etc.
  $possible_types = array_filter(array_keys($this->getTypedDataManager()
    ->getDefinitions()), fn(string $type) => preg_match("/^{$regex}\$/", $type) === 1);
  // 2. The fallback: `module.something.*` — if no concrete definition for it
  // exists.
  $fallback_type = $this->getTypedDataManager()
    ->findFallback($name);
  if ($fallback_type && !in_array($fallback_type, $possible_types, TRUE)) {
    $possible_types[] = $fallback_type;
  }
  return $possible_types;
}

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