protected function getGroupedOptions(array $options, array $selected) {
    $grouped_options = [];

    foreach ($options as $tid => $label) {
      // Start at the top for the current element.
      $group = &$grouped_options;

      // Find the position for this element based on its parent group. If it
      // contains a dash (-) then it's a child and we find its parent.
      while (strpos($label, '-') === 0) {
        // Remove the dash denoting this is a child.
        $label = substr($label, 1);
        // Move into the last parent added.
        end($group);
        $group = &$group[key($group)]['children'];
      }

      // This is now a new child to the currently selected parent.
      $group[$tid] = [
        'label' => $label,
        'selected' => in_array($tid, $selected),
      ];
    }

    return $grouped_options;
  }