You are here

protected static function CshsElement::formatOptions in Client-side Hierarchical Select 8.2

Same name and namespace in other branches
  1. 8 src/Element/CshsElement.php \Drupal\cshs\Element\CshsElement::formatOptions()

See also

\form_select_options()

1 call to CshsElement::formatOptions()
CshsElement::preRender in src/Element/CshsElement.php

File

src/Element/CshsElement.php, line 166

Class

CshsElement
Defines the CSHS element.

Namespace

Drupal\cshs\Element

Code

protected static function formatOptions(array $element) : array {
  $options = [];
  if (!empty($element['#options'])) {
    $parent = isset($element['#parent']) ? (string) $element['#parent'] : NULL;
    $is_value_valid = \array_key_exists('#value', $element);
    $is_value_array = $is_value_valid && \is_array($element['#value']);
    foreach ($element['#options'] as $value => $data) {
      $value = (string) $value;
      if ($is_value_valid) {
        $selected = $is_value_array ? \in_array($value, $element['#value']) : $value === (string) $element['#value'];
      }
      else {
        $selected = FALSE;
      }

      // The default `All` option coming from Views has `$data` as a string.
      if (\is_string($data)) {
        $data = static::option($data);
      }
      $options[] = [
        'value' => $value,
        'label' => $data['name'],
        'parent' => (string) $data['parent_tid'] === $parent ? 0 : $data['parent_tid'],
        'selected' => $selected,
      ];
    }
  }
  return $options;
}