function DatabaseTestController::testTablesort

Same name in other branches
  1. 8.9.x core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php \Drupal\database_test\Controller\DatabaseTestController::testTablesort()
  2. 10 core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php \Drupal\database_test\Controller\DatabaseTestController::testTablesort()
  3. 11.x core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php \Drupal\database_test\Controller\DatabaseTestController::testTablesort()

Runs a tablesort query and returns the results.

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

Return value

\Symfony\Component\HttpFoundation\JsonResponse

1 string reference to 'DatabaseTestController::testTablesort'
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 120

Class

DatabaseTestController
Controller routines for database_test routes.

Namespace

Drupal\database_test\Controller

Code

public function testTablesort() {
    $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);
    // We need all the results at once to check the sort.
    $tasks = $query->execute()
        ->fetchAll();
    return new JsonResponse([
        'tasks' => $tasks,
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.