protected static function WebformMultiple::validateUniqueKeys in Webform 8.5
Same name and namespace in other branches
- 6.x src/Element/WebformMultiple.php \Drupal\webform\Element\WebformMultiple::validateUniqueKeys()
Validate composite element has unique keys.
Parameters
array $element: The multiple element.
array $values: An array containing of item and weight.
Return value
null|string NULL if element has unique keys, else an error message with the duplicate key.
1 call to WebformMultiple::validateUniqueKeys()
- WebformMultiple::validateWebformMultiple in src/
Element/ WebformMultiple.php - Validates webform multiple element.
File
- src/
Element/ WebformMultiple.php, line 1051
Class
- WebformMultiple
- Provides a webform element to assist in creation of multiple elements.
Namespace
Drupal\webform\ElementCode
protected static function validateUniqueKeys(array $element, array $values) {
// Only validate if the element's #key is defined.
if (!isset($element['#key'])) {
return NULL;
}
$unique_keys = [];
foreach ($values as $value) {
$item = static::convertValueToItem($value);
$key_name = $element['#key'];
$key_value = $item[$key_name];
// Skip empty key and item.
unset($item[$key_name]);
if (empty($key_value) && static::isEmpty($item)) {
continue;
}
if (isset($unique_keys[$key_value])) {
$elements = WebformElementHelper::getFlattened($element['#element']);
$key_title = isset($elements[$key_name]['#title']) ? $elements[$key_name]['#title'] : $key_name;
$t_args = [
'@key' => $key_value,
'%title' => $key_title,
];
return t("The %title '@key' is already in use. It must be unique.", $t_args);
}
$unique_keys[$key_value] = $key_value;
}
return NULL;
}