function CreateForEachBundle::replacePlaceholders

Replaces placeholders recursively.

Parameters

mixed $data: The data to process. If this is an array, it'll be processed recursively.

array $replacements: An array whose keys are the placeholders to replace in the data, and whose values are the the replacements. Normally this will only mention the `%bundle` and `%label` placeholders. If $data is an array, the only placeholder that is replaced in the array's keys is `%bundle`.

Return value

mixed The given $data, with the `%bundle` and `%label` placeholders replaced.

1 call to CreateForEachBundle::replacePlaceholders()
CreateForEachBundle::apply in core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/CreateForEachBundle.php
Applies the config action.

File

core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/CreateForEachBundle.php, line 117

Class

CreateForEachBundle
Creates config entities for each bundle of a particular entity type.

Namespace

Drupal\Core\Config\Action\Plugin\ConfigAction

Code

private static function replacePlaceholders(mixed $data, array $replacements) : mixed {
    assert(array_key_exists(static::BUNDLE_PLACEHOLDER, $replacements));
    if (is_string($data)) {
        $data = str_replace(array_keys($replacements), $replacements, $data);
    }
    elseif (is_array($data)) {
        foreach ($data as $old_key => $value) {
            $value = static::replacePlaceholders($value, $replacements);
            // Only replace the `%bundle` placeholder in array keys.
            $new_key = str_replace(static::BUNDLE_PLACEHOLDER, $replacements[static::BUNDLE_PLACEHOLDER], $old_key);
            if ($old_key !== $new_key) {
                unset($data[$old_key]);
            }
            $data[$new_key] = $value;
        }
    }
    return $data;
}

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