You are here

protected function Kanban::sortStates in Content Planner 8

Sorts the given states.

Parameters

array $states: An array with the states to sort.

Return value

array Returns the sorted array of states.

1 call to Kanban::sortStates()
Kanban::__construct in modules/content_kanban/src/Component/Kanban.php
Constructor for the Kanban class.

File

modules/content_kanban/src/Component/Kanban.php, line 124

Class

Kanban
The main Kanban class.

Namespace

Drupal\content_kanban\Component

Code

protected function sortStates(array $states) {

  // Make a copy of the states.
  $sorted_states = $states;

  // Add the state id, so it does not get lost during the custom sort
  // function.
  foreach ($sorted_states as $state_id => &$state) {
    $state['state_id'] = $state_id;
  }

  // Sort for weight.
  usort($sorted_states, function ($a, $b) {
    if ($a['weight'] == $b['weight']) {
      return 0;
    }
    elseif ($a['weight'] < $b['weight']) {
      return -1;
    }
    else {
      return 1;
    }
  });

  // Build a new return array.
  $return = [];
  foreach ($sorted_states as $sorted_state) {
    $return[$sorted_state['state_id']] = $sorted_state;
  }
  return $return;
}