You are here

protected static function WorkflowTypeBase::labelWeightMultisort in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workflows/src/Plugin/WorkflowTypeBase.php \Drupal\workflows\Plugin\WorkflowTypeBase::labelWeightMultisort()
  2. 10 core/modules/workflows/src/Plugin/WorkflowTypeBase.php \Drupal\workflows\Plugin\WorkflowTypeBase::labelWeightMultisort()

Sort states or transitions by weight, label, and key.

Parameters

\Drupal\workflows\StateInterface[]|\Drupal\workflows\TransitionInterface[] $objects: An array of state or transition objects to multi-sort, keyed by the state or transition ID.

Return value

\Drupal\workflows\StateInterface[]|\Drupal\workflows\TransitionInterface[] An array of sorted transitions or states, keyed by the state or transition ID.

2 calls to WorkflowTypeBase::labelWeightMultisort()
WorkflowTypeBase::getStates in core/modules/workflows/src/Plugin/WorkflowTypeBase.php
Gets state objects for the provided state IDs.
WorkflowTypeBase::getTransitions in core/modules/workflows/src/Plugin/WorkflowTypeBase.php
Gets transition objects for the provided transition IDs.

File

core/modules/workflows/src/Plugin/WorkflowTypeBase.php, line 281

Class

WorkflowTypeBase
A base class for Workflow type plugins.

Namespace

Drupal\workflows\Plugin

Code

protected static function labelWeightMultisort($objects) {
  if (count($objects) > 1) {

    // Separate weights, labels, and keys into arrays.
    $weights = $labels = [];
    $keys = array_keys($objects);
    foreach ($objects as $id => $object) {
      $weights[$id] = $object
        ->weight();
      $labels[$id] = $object
        ->label();
    }

    // Sort weights, labels, and keys in the same order as each other.
    array_multisort($weights, SORT_NUMERIC, SORT_ASC, $labels, SORT_NATURAL, SORT_ASC, $keys);

    // Combine keys and weights to make sure the weights are keyed with the
    // correct keys.
    $weights = array_combine($keys, $weights);

    // Return the objects sorted by weight.
    return array_replace($weights, $objects);
  }
  return $objects;
}