public function YamlFormSubmissionGenerate::getTestValue in YAML Form 8
Get test value for a form element.
Parameters
\Drupal\yamlform\YamlFormInterface $yamlform: A form.
string $name: The name of the element.
array $element: The FAPI element.
Return value
array|int|null An array containing multiple values or a single value.
Overrides YamlFormSubmissionGenerateInterface::getTestValue
1 call to YamlFormSubmissionGenerate::getTestValue()
- YamlFormSubmissionGenerate::getData in src/
YamlFormSubmissionGenerate.php - Generate form submission data.
File
- src/
YamlFormSubmissionGenerate.php, line 90
Class
- YamlFormSubmissionGenerate
- Form submission generator.
Namespace
Drupal\yamlformCode
public function getTestValue(YamlFormInterface $yamlform, $name, array $element) {
/** @var \Drupal\yamlform\YamlFormElementInterface $element_handler */
$plugin_id = $this->elementManager
->getElementPluginId($element);
$element_handler = $this->elementManager
->createInstance($plugin_id);
// Exit if element does not have a value.
if (!$element_handler
->isInput($element)) {
return NULL;
}
// Exit if test values are null.
$values = $this
->getTestValues($yamlform, $name, $element);
if ($values === NULL) {
return NULL;
}
// Get random test value.
$value = is_array($values) ? $values[array_rand($values)] : $values;
// Replace tokens.
$token_data = [
'yamlform' => $yamlform,
];
$token_options = [
'clear' => TRUE,
];
if (is_string($value)) {
$value = $this->token
->replace($value, $token_data, $token_options);
}
elseif (is_array($value)) {
foreach (array_keys($value) as $value_key) {
if (is_string($value[$value_key])) {
$value[$value_key] = $this->token
->replace($value[$value_key], $token_data, $token_options);
}
}
}
// Elements that use multiple values require an array as the
// default value.
if ($element_handler
->hasMultipleValues($element) && !is_array($value)) {
return [
$value,
];
}
else {
return $value;
}
}