You are here

private function WidgetBase::sortOptions in Select (or other) 4.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/WidgetBase.php \Drupal\select_or_other\Plugin\Field\FieldWidget\WidgetBase::sortOptions()

Adds the available options to the select or other element.

Parameters

$options: The options to sort.

1 call to WidgetBase::sortOptions()
WidgetBase::formElement in src/Plugin/Field/FieldWidget/WidgetBase.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/WidgetBase.php, line 137

Class

WidgetBase
Base class for the 'select_or_other_*' widgets.

Namespace

Drupal\select_or_other\Plugin\Field\FieldWidget

Code

private function sortOptions($options) {
  if ($direction = $this
    ->getSetting('sort_options')) {
    if ($direction === 'ASC') {
      uasort($options, 'strcasecmp');
    }
    elseif ($direction === 'DESC') {
      uasort($options, function ($a, $b) {
        return -1 * strcasecmp($a, $b);
      });
    }
  }
  return $options;
}