Commit 43127e72 authored by jrockowitz's avatar jrockowitz Committed by jrockowitz

Issue #3000202 by jrockowitz, frankdesign: Conditional Logic and Regular Patterns

parent 9fc13fd6
......@@ -181,62 +181,47 @@ abstract class WebformOtherBase extends FormElement {
* Validates an other element.
*/
public static function validateWebformOther(&$element, FormStateInterface $form_state, &$complete_form) {
$type = static::getElementType();
// Determine if the element is visible. (#access !== FALSE)
$has_access = (!isset($element['#access']) || $element['#access'] === TRUE);
// Remove 'webform_' prefix from type.
$type = str_replace('webform_', '', static::$type);
// Determine if the element has mulitple values.
$is_multiple = static::isMultiple($element);
// Get value.
$value = NestedArray::getValue($form_state->getValues(), $element['#parents']);
// Get return value.
$return_value = [];
$element_value = $value[$type];
$other_value = $value['other'];
$required_error_title = (isset($element['#title'])) ? $element['#title'] : NULL;
$other_is_empty = FALSE;
if (static::isMultiple($element)) {
$element_value = array_filter($element_value);
$element_value = array_combine($element_value, $element_value);
$return_value += $element_value;
if (isset($return_value[static::OTHER_OPTION])) {
unset($return_value[static::OTHER_OPTION]);
if ($has_access && $other_value === '') {
WebformElementHelper::setRequiredError($element['other'], $form_state, $required_error_title);
$other_is_empty = TRUE;
}
else {
$return_value += [$other_value => $other_value];
}
}
}
else {
$return_value = $element_value;
if ($element_value == static::OTHER_OPTION) {
if ($has_access && $other_value === '') {
WebformElementHelper::setRequiredError($element['other'], $form_state, $required_error_title);
$other_is_empty = TRUE;
$return_value = '';
}
else {
$return_value = $other_value;
}
}
}
$return_value = static::processValue($element, $value);
// Determine if the return value is empty.
if (static::isMultiple($element)) {
if ($is_multiple) {
$is_empty = (empty($return_value)) ? TRUE : FALSE;
}
else {
$is_empty = ($return_value === '' || $return_value === NULL) ? TRUE : FALSE;
}
// Handler required validation.
if ($has_access && $element['#required'] && $is_empty && !$other_is_empty) {
WebformElementHelper::setRequiredError($element, $form_state, $required_error_title);
$element['other']['#error_no_message'] = TRUE;
// Determine if there is an other value and is the other value empty.
$element_value = (array) $value[$type];
$other_value = $value['other'];
if ($element_value) {
$element_value = array_filter($element_value);
$element_value = array_combine($element_value, $element_value);
}
$other_is_empty = (isset($element_value[static::OTHER_OPTION]) && $other_value === '');
// Display missing other or missing value error.
if ($has_access) {
$required_error_title = (isset($element['#title'])) ? $element['#title'] : NULL;
if ($other_is_empty) {
WebformElementHelper::setRequiredError($element['other'], $form_state, $required_error_title);
}
elseif ($element['#required'] && $is_empty) {
WebformElementHelper::setRequiredError($element, $form_state, $required_error_title);
$element['other']['#error_no_message'] = TRUE;
}
}
$form_state->setValueForElement($element[$type], NULL);
......@@ -246,10 +231,54 @@ abstract class WebformOtherBase extends FormElement {
$form_state->setValueForElement($element, $return_value);
}
/**
* Processed other element's submitted value.
*
* @param array $element
* The element.
* @param array $value
* The submitted value.
*
* @return array|string
* An array of values or a string.
*/
public static function processValue(array $element, array $value) {
$type = static::getElementType();
$element_value = $value[$type];
$other_value = $value['other'];
if (static::isMultiple($element)) {
$return_value = array_filter($element_value);
$return_value = array_combine($return_value, $return_value);
if (isset($return_value[static::OTHER_OPTION])) {
unset($return_value[static::OTHER_OPTION]);
if ($other_value !== '') {
$return_value += [$other_value => $other_value];
}
}
return $return_value;
}
else {
return ($element_value === static::OTHER_OPTION) ? $other_value : $element_value;
}
}
/****************************************************************************/
// Helper functions.
/****************************************************************************/
/**
* Get the element type.
*
* @return string
* The element type.
*/
public static function getElementType() {
// Remove 'webform_' prefix from type.
return str_replace('webform_', '', static::$type);
}
/**
* Determine if the webform element contains multiple values.
*
......
......@@ -554,6 +554,17 @@ abstract class OptionsBase extends WebformElementBase {
$input_name = WebformSubmissionConditionsValidator::getSelectorInputName($selector);
$other_type = WebformSubmissionConditionsValidator::getInputNameAsArray($input_name, 1);
$value = $this->getRawValue($element, $webform_submission);
// Handle edge case where the other element's value has
// not been processed.
// @see https://www.drupal.org/project/webform/issues/3000202
/** @var \Drupal\webform\Element\WebformOtherBase $class */
$class = $this->getFormElementClassDefinition();
$type = $class::getElementType();
if (is_array($value) && count($value) === 2 && isset($value[$type]) && isset($value['other'])) {
$value = $class::processValue($element, $value);
}
$options = OptGroup::flattenOptions($element['#options']);
if ($other_type === 'other') {
if ($this->hasMultipleValues($element)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment