diff --git a/core/includes/theme.inc b/core/includes/theme.inc index c027b5f..a7ad66f 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2518,7 +2518,7 @@ function template_preprocess_pager(&$variables) { '#title' => $tags[0], '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, 0), + 'query' => Pager::getQuery($parameters, $element, 0), 'attributes' => array( 'title' => t('Go to first page'), 'rel' => 'first', @@ -2536,7 +2536,7 @@ function template_preprocess_pager(&$variables) { '#title' => $tags[1], '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, Pager::getCurrentPage($element) - 1), + 'query' => Pager::getQuery($parameters, $element, Pager::getCurrentPage($element) - 1), 'attributes' => array( 'title' => t('Go to previous page'), 'rel' => 'prev', @@ -2558,7 +2558,7 @@ function template_preprocess_pager(&$variables) { '#title' => $tags[3], '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, Pager::getCurrentPage($element) + 1), + 'query' => Pager::getQuery($parameters, $element, Pager::getCurrentPage($element) + 1), 'attributes' => array( 'title' => t('Go to next page'), 'rel' => 'next', @@ -2576,7 +2576,7 @@ function template_preprocess_pager(&$variables) { '#title' => $tags[4], '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, Pager::getTotalPages($element) - 1), + 'query' => Pager::getQuery($parameters, $element, Pager::getTotalPages($element) - 1), 'attributes' => array( 'title' => t('Go to last page'), 'rel' => 'last', @@ -2624,7 +2624,7 @@ function template_preprocess_pager(&$variables) { '#title' => $i, '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, $i - 1), + 'query' => Pager::getQuery($parameters, $element, $i - 1), 'attributes' => array( 'title' => t('Go to page @number', array('@number' => $i)), ), @@ -2653,7 +2653,7 @@ function template_preprocess_pager(&$variables) { '#title' => $i, '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, $i - 1), + 'query' => Pager::getQuery($parameters, $element, $i - 1), 'attributes' => array( 'title' => t('Go to page @number', array('@number' => $i)), ), diff --git a/core/lib/Drupal/Component/Pager/Pager.php b/core/lib/Drupal/Component/Pager/Pager.php index 1725e5e..83994b7 100644 --- a/core/lib/Drupal/Component/Pager/Pager.php +++ b/core/lib/Drupal/Component/Pager/Pager.php @@ -12,6 +12,16 @@ */ class Pager { + /** + * An array of pager elements, keyed by element id. + * + * Each key in the array is itself an associative array with the following + * properties: + * 'totalItems' - total number of items in the pager + * 'totalPages' - total number of pages in the pager + * 'limit' - items per page + * 'current' - current page + */ static protected $elements = array(); /** @@ -32,7 +42,7 @@ class Pager { * * @see Pager::defaultInitialize() */ - static public function findPage($element = 0) { + static public function requestedPage($element = 0) { $page = isset($_GET['page']) ? $_GET['page'] : ''; $page_array = explode(',', $page); if (!isset($page_array[$element])) { @@ -42,7 +52,7 @@ static public function findPage($element = 0) { } /** - * Initializes a pager for theme('pager'). + * Initializes a pager. * * This function sets up the necessary global variables so that future calls * to theme('pager') will render a pager that correctly corresponds to the @@ -59,7 +69,11 @@ static public function findPage($element = 0) { * @endcode * * However, if you are using a different method for generating the items to be - * paged through, then you should call this function in preparation. + * paged through, then you should call this function in preparation. Mind to + * include a use statement in your code to access the Pager component: + * @code + * use Drupal\Component\Pager\Pager; + * @endcode * * The following example shows how this function can be used in a page callback * that invokes an external datastore with an SQL-like syntax: @@ -82,15 +96,15 @@ static public function findPage($element = 0) { * * A second example involves a page callback that invokes an external search * service where the total number of matching results is provided as part of - * the returned set (so that we do not need a separate query in order to obtain - * this information). Here, we call Pager::findPage() to calculate the desired - * offset before the search is invoked: + * the returned set (so that we do not need a separate query in order to + * obtain this information). Here, we call Pager::requestedPage() to + * calculate the desired offset before the search is invoked: * @code - * // Perform the query, using the requested offset from Pager::findPage(). + * // Perform the query, using the requested offset from Pager::requestedPage(). * // This comes from a URL parameter, so here we are assuming that the URL * // parameter corresponds to an actual page of results that will exist * // within the set. - * $page = Pager::findPage(); + * $page = Pager::requestedPage(); * $num_per_page = variable_get('mymodule_num_per_page', 10); * $offset = $num_per_page * $page; * $result = mymodule_remote_search($keywords, $offset, $num_per_page); @@ -121,66 +135,28 @@ static public function findPage($element = 0) { * page actually within the result set. */ static public function defaultInitialize($total, $limit, $element = 0) { - // Set total number of items. - self::setTotalItems($total, $element); - - // We calculate the total of pages as ceil(items / limit). - $total_pages = (int) self::setTotalPages(ceil($total / $limit), $element); - - // Current page is taken from current request, capped against total. - $current_page = self::setCurrentPage(max(0, min(self::findPage($element), $total_pages - 1)), $element); + self::set('totalItems', $total, $element); // Set items per page. - self::setLimit($limit, $element); + self::set('limit', $limit, $element); - return $current_page; - } + // We calculate the total of pages as ceil(items / limit). + $total_pages = (int) self::set('totalPages', ceil($total / $limit), $element); - static protected function set($key, $value, $element = 0) { - static::$elements[$element][$key] = $value; - return $value; - } - static protected function get($key, $element = 0) { - if (isset(static::$elements[$element])) { - return isset(static::$elements[$element][$key]) ? static::$elements[$element][$key] : NULL; - } - return NULL; - } - static public function setTotalPages($value, $element = 0) { - return self::set('totalPages', $value, $element); - } - static public function getTotalPages($element = 0) { - return self::get('totalPages', $element); - } - static public function setTotalItems($value, $element = 0) { - return self::set('totalItems', $value, $element); - } - static public function getTotalItems($element = 0) { - return self::get('totalItems', $element); - } - static public function setLimit($value, $element = 0) { - return self::set('limit', $value, $element); - } - static public function getLimit($element = 0) { - return self::get('limit', $element); - } - static public function setCurrentPage($value, $element = 0) { - return self::set('current', $value, $element); - } - static public function getCurrentPage($element = 0) { - return self::get('current', $element); + // Current page is taken from current request, capped against total. + return self::setCurrentPage(max(0, min(self::requestedPage($element), $total_pages - 1)), $element); } /** - * Compose a URL query parameter array for pager links. + * Get query parameter array of current request. * * @return * A URL query parameter array that consists of all components of the current * page request except for those pertaining to paging. */ - static protected function getQueryParameters() { - $query = &drupal_static(__FUNCTION__); + static protected function getCurrentRequestQueryParameters() { + $query = &drupal_static(__CLASS__ . '::' . __FUNCTION__); if (!isset($query)) { $query = drupal_get_query_parameters($_GET, array('page')); } @@ -188,41 +164,147 @@ static protected function getQueryParameters() { } /** - * Adds the 'page' parameter to the query parameter array of a pager link. + * Get the query parameter array of a pager link. * * @param array $parameters * An associative array of query parameters to add to. * @param integer $element * An integer to distinguish between multiple pagers on one page. - * @param integer $index - * The index of the target page in the pager array. + * @param integer $page + * The index of the link's target page. * * @return array * The altered $query parameter array. */ - static public function queryAddPage(array $parameters, $element, $index) { + static public function getQuery(array $parameters, $element, $page) { - // Build 'page' query parameter fragment. - // This is built based on current page per each element (or NULL if the - // element is not set), with the exception of the requested page index + // Build the 'page' query parameter fragment. + // This is built based on the current page of each pager element (or NULL + // if the pager is not set), with the exception of the requested page index // for the current element. $max_element = max(array_keys(static::$elements)); $element_pages = array(); for ($i = 0; $i <= $max_element; $i++) { - $element_pages[] = ($i == $element) ? $index: self::getCurrentPage($i); + $element_pages[] = ($i == $element) ? $page : self::getCurrentPage($i); } - // Update query with the 'page' fragment. + // Update query parameters with the newly built 'page' fragment. $parameters['page'] = implode(',', $element_pages); - // Merge the pager query parameters specified in Pager variables, with - // any parameters coming from the current request. In case of collision, - // parameters specified in Pager variables take precedence. - if ($current_request_query = self::getQueryParameters()) { + // Merge the updated pager query parameters, with any parameters coming + // from the current request. In case of collision, current parameters + // take precedence over the request ones. + if ($current_request_query = self::getCurrentRequestQueryParameters()) { $parameters = array_merge($current_request_query, $parameters); } return $parameters; } + /** + * Set a pager element property. + * + * @param string $key + * The property to be set for the specified element. + * @param integer $value + * The property value to be set. + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The new value of the property. + */ + static protected function set($key, $value, $element = 0) { + $value = abs(intval($value)); + $element = abs(intval($element)); + static::$elements[$element][$key] = $value; + return $value; + } + + /** + * Set the current page in an a pager element. + * + * @param integer $value + * The current page to set the pager to. + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The page set as current. + */ + static public function setCurrentPage($value, $element = 0) { + $element = abs(intval($element)); + return self::set('current', $value, $element); + } + + /** + * Get a pager element property. + * + * @param string $key + * The property to be got for the specified element. + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The property value. + */ + static protected function get($key, $element = 0) { + if (isset(static::$elements[$element])) { + return isset(static::$elements[$element][$key]) ? static::$elements[$element][$key] : NULL; + } + return NULL; + } + + /** + * Get total pages in an a pager element. + * + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The total number of pages managed by the pager. + */ + static public function getTotalPages($element = 0) { + return self::get('totalPages', $element); + } + + /** + * Get total items in an a pager element. + * + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The total number of items (records) managed by the pager. + */ + static public function getTotalItems($element = 0) { + return self::get('totalItems', $element); + } + + /** + * Get the items per page in an a pager element. + * + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The number of items (records) in each page. + */ + static public function getLimit($element = 0) { + return self::get('limit', $element); + } + + /** + * Get the current page in an a pager element. + * + * @param integer $element + * An integer to distinguish between multiple pagers on one page. + * + * @return integer + * The page to which the pager is currently positioned to. + */ + static public function getCurrentPage($element = 0) { + return self::get('current', $element); + } + } diff --git a/core/lib/Drupal/Core/Entity/Query/QueryBase.php b/core/lib/Drupal/Core/Entity/Query/QueryBase.php index cf7f3c7..d4f5277 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryBase.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryBase.php @@ -249,7 +249,7 @@ public function pager($limit = 10, $element = NULL) { */ protected function initializePager() { if ($this->pager && !empty($this->pager['limit']) && !$this->count) { - $page = Pager::findPage($this->pager['element']); + $page = Pager::requestedPage($this->pager['element']); $count_query = clone $this; $this->pager['total'] = $count_query->count()->execute(); $this->pager['start'] = $page * $this->pager['limit']; diff --git a/core/modules/taxonomy/taxonomy.admin.inc b/core/modules/taxonomy/taxonomy.admin.inc index 9c57fe4..f3edf19 100644 --- a/core/modules/taxonomy/taxonomy.admin.inc +++ b/core/modules/taxonomy/taxonomy.admin.inc @@ -122,10 +122,10 @@ function taxonomy_overview_terms($form, &$form_state, Vocabulary $vocabulary) { $current_page[$key] = $term; } while ($term = next($tree)); - // Because we didn't use a pager query, set the necessary pager variables. + // Because we didn't use a pager query, initialize the pager and set + // current page. $total_entries = $before_entries + $page_entries + $after_entries; - Pager::setTotalItems($total_entries, 0); - Pager::setTotalPages(ceil($total_entries / $page_increment), 0); + Pager::defaultInitialize($total_entries, $page_increment, 0); Pager::setCurrentPage($page, 0); // If this form was already submitted once, it's probably hit a validation diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/pager/SqlBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/pager/SqlBase.php index 58699ad..26b541b 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/pager/SqlBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/pager/SqlBase.php @@ -266,15 +266,6 @@ public function setCurrentPage($number = NULL) { $this->current_page = max(0, intval($request_page)); } - public function getPagerTotal() { - if ($items_per_page = intval($this->getItemsPerPage())) { - return ceil($this->total_items / $items_per_page); - } - else { - return 1; - } - } - /** * Update global paging info. * @@ -292,13 +283,8 @@ public function updatePageInfo() { // Don't set pager settings for items per page = 0. $items_per_page = $this->getItemsPerPage(); if (!empty($items_per_page)) { - // Dump information about what we already know into the Pager variables. - // Set the limit. - Pager::setLimit($this->options['items_per_page'], $this->options['id']); - // Set the item count for the pager. - Pager::setTotalItems($this->total_items, $this->options['id']); - // Calculate and set the count of available pages. - Pager::setTotalPages($this->getPagerTotal(), $this->options['id']); + // Initialize the pager. + Pager::defaultInitialize($this->total_items, $items_per_page, $this->options['id']); // See if the requested page was within range: if ($this->current_page >= Pager::getTotalPages($this->options['id'])) { diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 894261e..3182e3e 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -1146,7 +1146,7 @@ function theme_views_mini_pager($variables) { '#title' => $tags[1], '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, Pager::getCurrentPage($element) - 1), + 'query' => Pager::getQuery($parameters, $element, Pager::getCurrentPage($element) - 1), 'attributes' => array( 'title' => t('Go to previous page'), 'rel' => 'prev', @@ -1169,7 +1169,7 @@ function theme_views_mini_pager($variables) { '#title' => $tags[3], '#href' => $current_path, '#options' => array( - 'query' => Pager::queryAddPage($parameters, $element, Pager::getCurrentPage($element) + 1), + 'query' => Pager::getQuery($parameters, $element, Pager::getCurrentPage($element) + 1), 'attributes' => array( 'title' => t('Go to next page'), 'rel' => 'next', @@ -1177,7 +1177,7 @@ function theme_views_mini_pager($variables) { // Below is ignored by default, supplied to support hook_link_alter // implementations. 'pager_context' => array( - 'link_type' => 'previous', + 'link_type' => 'next', 'element' => $element, 'interval' => 1, ),