function ConfigActionManager::applyAction

Same name in other branches
  1. 10 core/lib/Drupal/Core/Config/Action/ConfigActionManager.php \Drupal\Core\Config\Action\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 133

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;
        }
    }
    try {
        
        /** @var \Drupal\Core\Config\Action\ConfigActionPluginInterface $action */
        $action = $this->createInstance($action_id);
    } catch (PluginNotFoundException $e) {
        $entity_type = $this->configManager
            ->getEntityTypeIdByName($configName);
        if ($entity_type) {
            $action_ids = $this->getShorthandActionIdsForEntityType($entity_type);
            $valid_ids = implode(', ', array_keys($action_ids));
            throw new PluginNotFoundException($action_id, sprintf('The "%s" entity does not support the "%s" config action. Valid config actions for %s are: %s', $entity_type, $action_id, $entity_type, $valid_ids));
        }
        throw $e;
    }
    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.