From fdc08aabed05cbf89e4440bc4a3ed251bcd9bc2f Mon Sep 17 00:00:00 2001 From: Pat Dunlavey Date: Wed, 13 Apr 2022 12:47:11 -0400 Subject: [PATCH] issues/191--metadatadate-validation-error --- implemented override of getElementSelectorInputValue() Here we create a special purpose override of \Drupal\webform\Plugin\WebformElementBase::getElementSelectorInputValue at \Drupal\webform_strawberryfield\Plugin\WebformElement\MetadataDateBase::getElementSelectorInputValue It attempts to apply the selector to the MetadataDateBase form element and get the value. We handle both single and multiple value versions of the field. If the selector results in an array, we return it unless it is empty. This will trigger a array to string conversion notice downstream in \Drupal\webform\WebformSubmissionConditionsValidator::checkConditionTrigger, but that's ok/appropriate. --- .../WebformElement/MetadataDateBase.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Plugin/WebformElement/MetadataDateBase.php b/src/Plugin/WebformElement/MetadataDateBase.php index f285d2f..ca4f050 100644 --- a/src/Plugin/WebformElement/MetadataDateBase.php +++ b/src/Plugin/WebformElement/MetadataDateBase.php @@ -14,6 +14,7 @@ use Drupal\webform\Plugin\WebformElementBase; use Drupal\webform\Utility\WebformArrayHelper; use Drupal\webform\Utility\WebformDateHelper; +use Drupal\webform\WebformSubmissionConditionsValidator; use Drupal\webform\WebformSubmissionInterface; use Drupal\webform\WebformInterface; @@ -268,6 +269,43 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form parent::validateConfigurationForm($form, $form_state); } + /** + * {@inheritdoc} + */ + public function getElementSelectorInputValue($selector, $trigger, array $element, WebformSubmissionInterface $webform_submission) { + $input_name = WebformSubmissionConditionsValidator::getSelectorInputName($selector); + $input_keys = WebformSubmissionConditionsValidator::getInputNameAsArray($input_name, NULL); + // Multi-value fields utilize input paths in the form 'webform_example_element_multiple[items][0][_item_]' => '{Test 01}', + // but ::getRawValue does not work with 'items' and '_item_' in the path. So we strip them out here. + $input_keys = array_diff($input_keys, ['items', '_item_']); + // After doing so, we expect the input keys to be in the form: + // ['webform field name', 'the composite element key'] OR ['webform field name', 'multiple-value delta', 'the composite element key'] + // If neither case applies, return NULL; + if(!(count($input_keys) == 2 || count($input_keys) == 3)) { + return NULL; + } + else { + // Prepare options for ::getRawValue(). + $options = [ + 'webform_key' => reset($input_keys), + 'composite_key' => end($input_keys), + ]; + // Set delta for multiple value elements. + if(!empty($element['#multiple']) ) { + $options['delta'] = (count($input_keys) == 3 && is_numeric($input_keys[1])) ? $input_keys[1] : 0; + } + + $input_value = $this->getRawValue($element, $webform_submission, $options); + // getRawValue can return an array, which \Drupal\webform\WebformSubmissionConditionsValidator::checkConditionTrigger + // tries to cast to a string and therefore generates a php array to string conversion warning. + // If the array is empty, we replace it with an empty string. Otherwise we let checkConditionTrigger complain. + if(is_array($input_value) && empty($input_value)) { + $input_value = ""; + } + return $input_value; + } + } + /** * Get the type of date/time element. *