You are here

protected function VariantRouteFilter::sortRoutes in Page Manager 8.4

Same name and namespace in other branches
  1. 8 src/Routing/VariantRouteFilter.php \Drupal\page_manager\Routing\VariantRouteFilter::sortRoutes()

Sorts routes based on the variant weight.

Parameters

\Symfony\Component\Routing\Route[] $unsorted_routes: An array of unsorted routes.

Return value

\Symfony\Component\Routing\Route[] An array of sorted routes.

1 call to VariantRouteFilter::sortRoutes()
VariantRouteFilter::filter in src/Routing/VariantRouteFilter.php
Ensures only one page manager route remains in the collection.

File

src/Routing/VariantRouteFilter.php, line 158

Class

VariantRouteFilter
Filters variant routes.

Namespace

Drupal\page_manager\Routing

Code

protected function sortRoutes(array $unsorted_routes) {

  // Create a mapping of route names to their weights.
  $weights_by_key = array_map(function (Route $route) {
    return $route
      ->getDefault('page_manager_page_variant_weight') ?: 0;
  }, $unsorted_routes);

  // Create an array holding the route names to be sorted.
  $keys = array_keys($unsorted_routes);

  // Sort $keys first by the weights and then by the original order.
  array_multisort($weights_by_key, array_keys($keys), $keys);

  // Return the routes using the sorted order of $keys.
  return array_replace(array_combine($keys, $keys), $unsorted_routes);
}