TypedConfigManager::replaceVariable

protected TypedConfigManager::replaceVariable($value, $data)

Replaces variable values in included names with configuration data.

Variable values are nested configuration keys that will be replaced by their value or some of these special strings:

  • '%key', will be replaced by the element's key.
  • '%parent', to reference the parent element.
  • '%type', to reference the schema definition type. Can only be used in combination with %parent.

There may be nested configuration keys separated by dots or more complex patterns like '%parent.name' which references the 'name' value of the parent element.

Example patterns:

  • 'name.subkey', indicates a nested value of the current element.
  • '%parent.name', will be replaced by the 'name' value of the parent.
  • '%parent.%key', will be replaced by the parent element's key.
  • '%parent.%type', will be replaced by the schema type of the parent.
  • '%parent.%parent.%type', will be replaced by the schema type of the parent's parent.

Parameters

string $value: Variable value to be replaced.

mixed $data: Configuration data for the element.

Return value

string The replaced value if a replacement found or the original value if not.

File

core/lib/Drupal/Core/Config/TypedConfigManager.php, line 309

Class

TypedConfigManager
Manages config schema type plugins.

Namespace

Drupal\Core\Config

Code

protected function replaceVariable($value, $data) {
  $parts = explode('.', $value);
  // Process each value part, one at a time.
  while ($name = array_shift($parts)) {
    if (!is_array($data) || !isset($data[$name])) {
      // Key not found, return original value
      return $value;
    }
    elseif (!$parts) {
      $value = $data[$name];
      if (is_bool($value)) {
        $value = (int) $value;
      }
      // If no more parts left, this is the final property.
      return (string) $value;
    }
    else {
      // Get nested value and continue processing.
      if ($name == '%parent') {
        /** @var \Drupal\Core\Config\Schema\ArrayElement $parent */
        // Switch replacement values with values from the parent.
        $parent = $data['%parent'];
        $data = $parent->getValue();
        $data['%type'] = $parent->getDataDefinition()->getDataType();
        // The special %parent and %key values now need to point one level up.
        if ($new_parent = $parent->getParent()) {
          $data['%parent'] = $new_parent;
          $data['%key'] = $new_parent->getName();
        }
      }
      else {
        $data = $data[$name];
      }
    }
  }
}
doc_Drupal
2016-10-29 09:49:56
Comments
Leave a Comment

Please login to continue.