public function HttpConfigRequestForm::jsonString in HTTP Client Manager 8.2
Value callback: converts strings to JSON array.
Parameters
array $element: The form element.
string $input: The input value.
\Drupal\Core\Form\FormStateInterface $form_state: The Form State instance.
Return value
array|null|string The computed value.
File
- src/
Form/ HttpConfigRequestForm.php, line 316
Class
- HttpConfigRequestForm
- Class HttpConfigRequestForm.
Namespace
Drupal\http_client_manager\FormCode
public function jsonString(array &$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && $input !== NULL) {
$input = trim($input);
if (empty($input)) {
return [];
}
/** @var \GuzzleHttp\Command\Guzzle\Parameter $param */
$param = $element['#command_param'];
$assoc = $param
->getType() == 'array' ? JSON_OBJECT_AS_ARRAY : JSON_FORCE_OBJECT;
$item = json_decode($input, $assoc);
if (json_last_error() !== JSON_ERROR_NONE) {
$message = $this
->t('There was an error parsing your JSON: @error', [
'@error' => json_last_error_msg(),
]);
$this
->messenger()
->addError($message);
return $input;
}
return $item;
}
if (empty($element['#default_value'])) {
return NULL;
}
$item = json_encode($element['#default_value'], JSON_PRETTY_PRINT);
if (json_last_error() !== JSON_ERROR_NONE) {
$this
->messenger()
->addError(json_last_error_msg());
return $input;
}
return $item;
}