You are here

public function OptionsBase::getElementSelectorInputValue in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/OptionsBase.php \Drupal\webform\Plugin\WebformElement\OptionsBase::getElementSelectorInputValue()

Get an element's (sub)input selector value.

Parameters

string $selector: CSS :input selector.

string $trigger: Trigger from #states.

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

Return value

mixed The element input's value.

Overrides WebformElementBase::getElementSelectorInputValue

1 method overrides OptionsBase::getElementSelectorInputValue()
Checkboxes::getElementSelectorInputValue in src/Plugin/WebformElement/Checkboxes.php
Get an element's (sub)input selector value.

File

src/Plugin/WebformElement/OptionsBase.php, line 787

Class

OptionsBase
Provides a base 'options' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function getElementSelectorInputValue($selector, $trigger, array $element, WebformSubmissionInterface $webform_submission) {
  if ($this
    ->isOptionsOther()) {
    $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)) {
        $other_value = array_diff($value, array_keys($options));
        return $other_value ? implode(', ', $other_value) : NULL;
      }
      else {

        // Make sure other value is not valid option.
        return $value && !isset($options[$value]) ? $value : NULL;
      }
    }
    else {

      // If the trigger is 'filled or 'empty' then return the value.
      if ($trigger === 'filled' || $trigger === 'empty') {
        return $value;
      }
      if ($this
        ->hasMultipleValues($element)) {

        // Return array of valid #options.
        return array_intersect($value, array_keys($options));
      }
      else {

        // Return valid #option.
        return isset($options[$value]) ? $value : NULL;
      }
    }
  }
  else {
    return parent::getElementSelectorInputValue($selector, $trigger, $element, $webform_submission);
  }
}