function EntityClone::apply

Overrides ConfigActionPluginInterface::apply

File

core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/EntityClone.php, line 45

Class

EntityClone
@internal This API is experimental.

Namespace

Drupal\Core\Config\Action\Plugin\ConfigAction

Code

public function apply(string $configName, mixed $value) : void {
    if (!is_array($value)) {
        $value = [
            'id' => $value,
        ];
    }
    assert(is_string($value['id']));
    $value += [
        'fail_if_exists' => FALSE,
    ];
    assert(is_bool($value['fail_if_exists']));
    // If the original doesn't exist, there's nothing to clone.
    $original = $this->configManager
        ->loadConfigEntityByName($configName);
    if (empty($original)) {
        throw new ConfigActionException("Cannot clone '{$configName}' because it does not exist.");
    }
    $clone = $original->createDuplicate();
    $clone->set($original->getEntityType()
        ->getKey('id'), $value['id']);
    $create_action = 'entity_create:' . ($value['fail_if_exists'] ? 'create' : 'createIfNotExists');
    // Use the config action manager to invoke the create action on the clone,
    // so that it will be validated.
    $this->configActionManager
        ->applyAction($create_action, $clone->getConfigDependencyName(), $clone->toArray());
}

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