function ConfigTarget::__construct

Same name in other branches
  1. 10 core/lib/Drupal/Core/Form/ConfigTarget.php \Drupal\Core\Form\ConfigTarget::__construct()

Constructs a ConfigTarget object.

Parameters

string $configName: The name of the config object being read from or written to, e.g. `system.site`.

string|array $propertyPath: The property path(s) being read or written, e.g., `page.front`.

callable|null $fromConfig: (optional) A callback which should transform the value loaded from config before it gets displayed by the form. If NULL, no transformation will be done. The callback will receive all of the values loaded from config as separate arguments, in the order specified by $this->propertyPaths. Defaults to NULL.

callable|null $toConfig: (optional) A callback which should transform the value submitted by the form before it is set in the config object. If NULL, no transformation will be done. The callback will receive the value submitted through the form; if this object is targeting multiple property paths, the value will be an array of the submitted values, keyed by property path, and must return an array with the transformed values, also keyed by property path. The callback will receive the form state object as its second argument. The callback may return a special values:

  • ToConfig::NoMapping, to indicate that the given form value does not need to be mapped onto the Config object
  • ToConfig::DeleteKey to indicate that the targeted property path should be deleted from config.

Defaults to NULL.

File

core/lib/Drupal/Core/Form/ConfigTarget.php, line 83

Class

ConfigTarget
Represents the mapping of a config property to a form element.

Namespace

Drupal\Core\Form

Code

public function __construct(string $configName, string|array $propertyPath, ?callable $fromConfig = NULL, ?callable $toConfig = NULL) {
    $this->fromConfig = $fromConfig;
    $this->toConfig = $toConfig;
    if (is_string($propertyPath)) {
        $propertyPath = [
            $propertyPath,
        ];
    }
    elseif (count($propertyPath) > 1 && (empty($fromConfig) || empty($toConfig))) {
        throw new \LogicException('The $fromConfig and $toConfig arguments must be passed to ' . __METHOD__ . '() if multiple property paths are targeted.');
    }
    $this->propertyPaths = array_values($propertyPath);
}

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