Unverified Commit c0881f91 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #109493 by drunken monkey, gnuget, robertDouglass, dhirendra.mishra,...

Issue #109493 by drunken monkey, gnuget, robertDouglass, dhirendra.mishra, jhodgdon, jibran, chaby, aleevas, alexpott, DanChadwick, Dries, catch: tablesort should allow rendered tables to have columns that default to DESC when their header is clicked
parent 41a9b825
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -844,6 +844,8 @@ function template_preprocess_image(&$variables) {
 *     - sort: A default sort order for this column ("asc" or "desc"). Only
 *       one column should be given a default sort order because table sorting
 *       only applies to one column at a time.
 *     - initial_click_sort: Set the initial sort of the column when clicked.
 *       Defaults to "asc".
 *     - class: An array of values for the 'class' attribute. In particular,
 *       the least important columns that can be hidden on narrow and medium
 *       width screens should have a 'priority-low' class, referenced with the
@@ -997,7 +999,8 @@ function template_preprocess_table(&$variables) {

        TableSort::header($cell_content, $cell, $variables['header'], $ts);

        // TableSort::header() removes the 'sort' and 'field' keys.
        // TableSort::header() removes the 'sort', 'initial_click_sort' and
        // 'field' keys.
        $cell_attributes = new Attribute($cell);
      }
      $variables['header'][$col_key] = [];
+13 −6
Original line number Diff line number Diff line
@@ -73,9 +73,11 @@ public static function header(&$cell_content, array &$cell_attributes, array $he
        $image = \Drupal::service('renderer')->render($tablesort_indicator);
      }
      else {
        // If the user clicks a different header, we want to sort ascending
        // initially.
        $context['sort'] = self::ASC;
        // This determines the sort order when the column gets first clicked by
        // the user. It is "asc" by default but the sort can be changed if
        // $cell['initial_click_sort'] is defined. The possible values are "asc"
        // or "desc".
        $context['sort'] = $cell_attributes['initial_click_sort'] ?? self::ASC;
        $image = '';
      }
      $cell_content = Link::createFromRoute(new FormattableMarkup('@cell_content@image', ['@cell_content' => $cell_content, '@image' => $image]), '<current>', [], [
@@ -86,7 +88,7 @@ public static function header(&$cell_content, array &$cell_attributes, array $he
        ]),
      ]);

      unset($cell_attributes['field'], $cell_attributes['sort']);
      unset($cell_attributes['field'], $cell_attributes['sort'], $cell_attributes['initial_click_sort']);
    }
  }

@@ -150,9 +152,14 @@ public static function getSort(array $headers, Request $request) {
    // Find out which header is currently being sorted.
    $order = static::getOrder($headers, $request);
    foreach ($headers as $header) {
      if (is_array($header) && isset($header['data']) && $header['data'] == $order['name'] && isset($header['sort'])) {
      if (is_array($header) && isset($header['data']) && $header['data'] == $order['name']) {
        if (isset($header['sort'])) {
          return $header['sort'];
        }
        if (isset($header['initial_click_sort'])) {
          return $header['initial_click_sort'];
        }
      }
    }
    return self::ASC;
  }
+115 −1
Original line number Diff line number Diff line
@@ -136,7 +136,121 @@ public function testTableSortInit() {
    ];
    $ts = TableSort::getContextFromRequest($headers, $request);
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $this->assertEqual($ts, $expected_ts, 'Complex table headers plus $_GET parameters sorted correctly.');
    $this->assertEquals($expected_ts, $ts, 'Complex table headers plus $_GET parameters sorted correctly.');

    // Test the initial_click_sort parameter.
    $headers = [
      'foo',
      [
        'data' => '1',
        'field' => 'one',
        'initial_click_sort' => 'desc',
        'colspan' => 1,
      ],
      [
        'data' => '2',
        'field' => 'two',
      ],
      [
        'data' => '3',
        'field' => 'three',
        'initial_click_sort' => 'desc',
        'sort' => 'asc',
      ],
      [
        'data' => '4',
        'field' => 'four',
        'initial_click_sort' => 'asc',
      ],
      [
        'data' => '5',
        'field' => 'five',
        'initial_click_sort' => 'foo',
      ],
    ];
    $request = Request::createFromGlobals();
    $request->query->replace([
      'order' => '1',
    ]);
    \Drupal::getContainer()->get('request_stack')->push($request);
    $ts = TableSort::getContextFromRequest($headers, $request);
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $expected_ts = [
      'name' => '1',
      'sql' => 'one',
      'sort' => 'desc',
      'query' => [],
    ];
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $this->assertEquals($expected_ts, $ts, 'Complex table headers using the initial_click_sort parameter are sorted correctly.');

    // Test that if the initial_click_sort parameter is not defined, the default
    // must be used instead (which is "asc").
    $request = Request::createFromGlobals();
    $request->query->replace([
      'order' => '2',
    ]);
    \Drupal::getContainer()->get('request_stack')->push($request);
    $ts = TableSort::getContextFromRequest($headers, $request);
    $expected_ts = [
      'name' => '2',
      'sql' => 'two',
      'sort' => 'asc',
      'query' => [],
    ];
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $this->assertEquals($expected_ts, $ts, 'Complex table headers without using the initial_click_sort parameter are sorted correctly.');

    // Test that if the initial_click_sort parameter is defined, and the sort
    // parameter is defined as well, the sort parameter has precedence.
    $request = Request::createFromGlobals();
    $request->query->replace([
      'order' => '3',
    ]);
    \Drupal::getContainer()->get('request_stack')->push($request);
    $ts = TableSort::getContextFromRequest($headers, $request);
    $expected_ts = [
      'name' => '3',
      'sql' => 'three',
      'sort' => 'asc',
      'query' => [],
    ];
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $this->assertEquals($expected_ts, $ts, 'Complex table headers using the initial_click_sort and sort parameters are sorted correctly.');

    // Test that if the initial_click_sort parameter is defined and the value
    // is "asc" it should be sorted correctly.
    $request = Request::createFromGlobals();
    $request->query->replace([
      'order' => '4',
    ]);
    \Drupal::getContainer()->get('request_stack')->push($request);
    $ts = TableSort::getContextFromRequest($headers, $request);
    $expected_ts = [
      'name' => '4',
      'sql' => 'four',
      'sort' => 'asc',
      'query' => [],
    ];
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $this->assertEquals($expected_ts, $ts, 'Complex table headers with the initial_click_sort set as ASC are sorted correctly.');

    // Tests that if the initial_click_sort is defined with a non expected value
    // that value will be passed as the "sort" value.
    $request = Request::createFromGlobals();
    $request->query->replace([
      'order' => '5',
    ]);
    \Drupal::getContainer()->get('request_stack')->push($request);
    $ts = TableSort::getContextFromRequest($headers, $request);
    $expected_ts = [
      'name' => '5',
      'sql' => 'five',
      'sort' => 'foo',
      'query' => [],
    ];
    $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
    $this->assertEquals($expected_ts, $ts, 'Complex table headers with the initial_click_sort set as foo are sorted correctly.');
  }

}