You are here

protected function AjaxController::retrieveOrderFromRequest in Sortableviews 8

Returns the entity order adjusted for the view pager.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

Return value

array An array with the adjusted sort order where the key is the entity weight and the value, the entity id.

1 call to AjaxController::retrieveOrderFromRequest()
AjaxController::ajaxSave in src/Controller/AjaxController.php
Saves new weights.

File

src/Controller/AjaxController.php, line 57

Class

AjaxController
Controller for sortableviews ajax calls.

Namespace

Drupal\sortableviews\Controller

Code

protected function retrieveOrderFromRequest(Request $request) {

  // Adjust order for pager in asc fashion.
  if ($request
    ->get('items_per_page') && $request
    ->get('sort_order') == 'asc') {
    $adjusted_order = [];
    foreach ($request
      ->get('current_order') as $index => $value) {
      $new_index = $index + $request
        ->get('page_number') * $request
        ->get('items_per_page');
      $adjusted_order[$new_index] = $value;
    }
    return $adjusted_order;
  }

  // Adjust order for pager in desc fashion.
  if ($request
    ->get('items_per_page') && $request
    ->get('sort_order') == 'desc') {
    $complete_pages = (int) ($request
      ->get('total_rows') / $request
      ->get('items_per_page'));
    if ($complete_pages == $request
      ->get('page_number')) {

      // This is the last page of the view.
      return $request
        ->get('current_order');
    }
    $adjusted_order = [];
    $mod = (int) $request
      ->get('total_rows') % $request
      ->get('items_per_page');
    foreach ($request
      ->get('current_order') as $index => $value) {
      $new_index = $index + ($complete_pages - ($request
        ->get('page_number') + 1)) * $request
        ->get('items_per_page') + $mod;
      $adjusted_order[$new_index] = $value;
    }
    return $adjusted_order;
  }
  return $request
    ->get('current_order');
}