function ConfigActionManager::applyAction

Applies a config action.

Parameters

string $action_id: The ID of the action to apply. This can be a complete configuration action plugin ID or a shorthand action ID that is available for the entity type of the provided configuration name.

string $configName: The configuration name. This may be the full name of a config object, or it may contain wildcards (to target all config entities of a specific type, or a subset thereof). See ConfigActionManager::getConfigNamesMatchingExpression() for more detail.

mixed $data: The data for the action.

Throws

\Drupal\Component\Plugin\Exception\PluginException Thrown when the config action cannot be found.

\Drupal\Core\Config\Action\ConfigActionException Thrown when the config action fails to apply.

See also

\Drupal\Core\Config\Action\ConfigActionManager::getConfigNamesMatchingExpression()

File

core/lib/Drupal/Core/Config/Action/ConfigActionManager.php, line 116

Class

ConfigActionManager

Namespace

Drupal\Core\Config\Action

Code

public function applyAction(string $action_id, string $configName, mixed $data) : void {
    if (!$this->hasDefinition($action_id)) {
        // Get the full plugin ID from the shorthand map, if it is available.
        $entity_type = $this->configManager
            ->getEntityTypeIdByName($configName);
        if ($entity_type) {
            $action_id = $this->getShorthandActionIdsForEntityType($entity_type)[$action_id] ?? $action_id;
        }
    }
    
    /** @var \Drupal\Core\Config\Action\ConfigActionPluginInterface $action */
    $action = $this->createInstance($action_id);
    foreach ($this->getConfigNamesMatchingExpression($configName) as $name) {
        $action->apply($name, $data);
        $typed_config = $this->typedConfig
            ->createFromNameAndData($name, $this->configFactory
            ->get($name)
            ->getRawData());
        // All config objects are mappings.
        assert($typed_config instanceof Mapping);
        foreach ($typed_config->getConstraints() as $constraint) {
            // Only validate the config if it has explicitly been marked as being
            // validatable.
            if ($constraint instanceof FullyValidatableConstraint) {
                
                /** @var \Symfony\Component\Validator\ConstraintViolationList $violations */
                $violations = $typed_config->validate();
                if (count($violations) > 0) {
                    throw new InvalidConfigException($violations, $typed_config);
                }
                break;
            }
        }
    }
}

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