function ConfigTarget::setValue
Same name in other branches
- 11.x core/lib/Drupal/Core/Form/ConfigTarget.php \Drupal\Core\Form\ConfigTarget::setValue()
Sets the submitted value from config.
Parameters
\Drupal\Core\Config\Config $config: The config object we're changing.
mixed $value: The value(s) to set. If this object is targeting multiple property paths, this must be an array with the values to set, keyed by property path.
\Drupal\Core\Form\FormStateInterface $form_state: The current form state.
Throws
\InvalidArgumentException Thrown if the given config object is not the one being targeted by $this->configName.
\LogicException Thrown if this object is targeting multiple property paths and $value does not contain a value for every targeted property path.
File
-
core/
lib/ Drupal/ Core/ Form/ ConfigTarget.php, line 211
Class
- ConfigTarget
- Represents the mapping of a config property to a form element.
Namespace
Drupal\Core\FormCode
public function setValue(Config $config, mixed $value, FormStateInterface $form_state) : void {
if ($config->getName() !== $this->configName) {
throw new \InvalidArgumentException(sprintf('Config target is associated with %s but %s given.', $this->configName, $config->getName()));
}
$is_multi_target = $this->isMultiTarget();
if ($this->toConfig) {
$value = ($this->toConfig)($value, $form_state);
if ($is_multi_target) {
// If we're targeting multiple property paths, $value needs to be an array
// with every targeted property path.
if (!is_array($value)) {
throw new \LogicException(sprintf('The toConfig callable returned a %s, but it must be an array with a key-value pair for each of the targeted property paths.', gettype($value)));
}
elseif ($missing_keys = array_diff($this->propertyPaths, array_keys($value))) {
throw new \LogicException(sprintf('The toConfig callable returned an array that is missing key-value pairs for the following targeted property paths: %s.', implode(', ', $missing_keys)));
}
elseif ($unknown_keys = array_diff(array_keys($value), $this->propertyPaths)) {
throw new \LogicException(sprintf('The toConfig callable returned an array that contains key-value pairs that do not match targeted property paths: %s.', implode(', ', $unknown_keys)));
}
}
}
// Match the structure expected for a multi-target ConfigTarget.
if (!$is_multi_target) {
$value = [
$this->propertyPaths[0] => $value,
];
}
// Set the returned value, or if a special value (one of the cases in the
// ConfigTargetValue enum): apply the appropriate action.
array_walk($value, fn(mixed $value, string $property) => match ($value) { ToConfig::NoOp => NULL,
ToConfig::DeleteKey => $config->clear($property),
default => $config->set($property, $value),
});
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.