You are here

protected static function WebformOtherBase::convertDefaultValueToElementValue in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformOtherBase.php \Drupal\webform\Element\WebformOtherBase::convertDefaultValueToElementValue()

Convert default value to element value.

Parameters

array $element: A other form element.

Return value

array An associative array container (element) type and other value.

1 call to WebformOtherBase::convertDefaultValueToElementValue()
WebformOtherBase::valueCallback in src/Element/WebformOtherBase.php
Determines how user input is mapped to an element's #value property.

File

src/Element/WebformOtherBase.php, line 337

Class

WebformOtherBase
Base class for webform other element.

Namespace

Drupal\webform\Element

Code

protected static function convertDefaultValueToElementValue(array $element) {
  $type = str_replace('webform_', '', static::$type);
  $default_value = isset($element['#default_value']) && $element['#default_value'] !== '' ? $element['#default_value'] : NULL;
  if (static::isMultiple($element)) {

    // Handle edge case where $default_value is not an array.
    if (!is_array($default_value)) {
      return [
        $type => [],
        'other' => NULL,
      ];
    }
    $default_options = array_combine($default_value, $default_value);
    $flattened_options = OptGroup::flattenOptions($element['#options']);
    if ($other_options = array_diff_key($default_options, $flattened_options)) {
      return [
        $type => array_diff_key($default_options, $other_options) + [
          static::OTHER_OPTION => static::OTHER_OPTION,
        ],
        'other' => implode($element['#other__option_delimiter'], $other_options),
      ];
    }
    return [
      $type => $default_options,
      'other' => NULL,
    ];
  }
  else {
    if ($default_value !== NULL && !WebformOptionsHelper::hasOption($default_value, $element['#options'])) {
      return [
        $type => static::OTHER_OPTION,
        'other' => $default_value,
      ];
    }
    return [
      $type => $default_value,
      'other' => NULL,
    ];
  }
}