/Drupal 8

protected function 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.


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.


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


Manages config schema type plugins.




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

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.