You are here

public function DatabaseTestController::testTablesortFirst in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php \Drupal\database_test\Controller\DatabaseTestController::testTablesortFirst()
  2. 9 core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php \Drupal\database_test\Controller\DatabaseTestController::testTablesortFirst()

Runs a tablesort query with a second order_by after and returns the results.

This function does care about the page GET parameter, as set by the test HTTP call.

Return value

\Symfony\Component\HttpFoundation\JsonResponse

1 string reference to 'DatabaseTestController::testTablesortFirst'
database_test.routing.yml in core/modules/system/tests/modules/database_test/database_test.routing.yml
core/modules/system/tests/modules/database_test/database_test.routing.yml

File

core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php, line 135

Class

DatabaseTestController
Controller routines for database_test routes.

Namespace

Drupal\database_test\Controller

Code

public function testTablesortFirst() {
  $header = [
    'tid' => [
      'data' => t('Task ID'),
      'field' => 'tid',
      'sort' => 'desc',
    ],
    'pid' => [
      'data' => t('Person ID'),
      'field' => 'pid',
    ],
    'task' => [
      'data' => t('Task'),
      'field' => 'task',
    ],
    'priority' => [
      'data' => t('Priority'),
      'field' => 'priority',
    ],
  ];
  $query = $this->connection
    ->select('test_task', 't');
  $query
    ->fields('t', [
    'tid',
    'pid',
    'task',
    'priority',
  ]);
  $query = $query
    ->extend(TableSortExtender::class)
    ->orderByHeader($header)
    ->orderBy('priority');

  // We need all the results at once to check the sort.
  $tasks = $query
    ->execute()
    ->fetchAll();
  return new JsonResponse([
    'tasks' => $tasks,
  ]);
}