"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Database/Query/TableSortExtender.php" (26 Nov 2020, 1383 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "TableSortExtender.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Database\Query;
    4 
    5 use Drupal\Core\Database\Connection;
    6 use Drupal\Core\Utility\TableSort;
    7 
    8 /**
    9  * Query extender class for tablesort queries.
   10  */
   11 class TableSortExtender extends SelectExtender {
   12 
   13   /**
   14    * {@inheritdoc}
   15    */
   16   public function __construct(SelectInterface $query, Connection $connection) {
   17     parent::__construct($query, $connection);
   18 
   19     // Add convenience tag to mark that this is an extended query. We have to
   20     // do this in the constructor to ensure that it is set before preExecute()
   21     // gets called.
   22     $this->addTag('tablesort');
   23   }
   24 
   25   /**
   26    * Order the query based on a header array.
   27    *
   28    * @param array $header
   29    *   Table header array.
   30    *
   31    * @return \Drupal\Core\Database\Query\SelectInterface
   32    *   The called object.
   33    *
   34    * @see table.html.twig
   35    */
   36   public function orderByHeader(array $header) {
   37     $context = TableSort::getContextFromRequest($header, \Drupal::request());
   38     if (!empty($context['sql'])) {
   39       // Based on code from \Drupal\Core\Database\Connection::escapeTable(),
   40       // but this can also contain a dot.
   41       $field = preg_replace('/[^A-Za-z0-9_.]+/', '', $context['sql']);
   42 
   43       // orderBy() will ensure that only ASC/DESC values are accepted, so we
   44       // don't need to sanitize that here.
   45       $this->orderBy($field, $context['sort']);
   46     }
   47     return $this;
   48   }
   49 
   50 }