Chris@0: query->get('page'), or 0 by default. Note that this Chris@0: * number may differ from the actual page being displayed. For example, if a Chris@0: * search for "example text" brings up three pages of results, but a user Chris@0: * visits search/node/example+text?page=10, this function will return 10, Chris@0: * even though the default pager implementation adjusts for this and still Chris@0: * displays the third page of search results at that URL. Chris@0: * Chris@0: * @see pager_default_initialize() Chris@0: */ Chris@0: function pager_find_page($element = 0) { Chris@0: $page = \Drupal::request()->query->get('page', ''); Chris@0: $page_array = explode(',', $page); Chris@0: if (!isset($page_array[$element])) { Chris@0: $page_array[$element] = 0; Chris@0: } Chris@0: return (int) $page_array[$element]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes a pager. Chris@0: * Chris@0: * This function sets up the necessary global variables so that the render Chris@0: * system will correctly process #type 'pager' render arrays to output pagers Chris@0: * that correspond to the items being displayed. Chris@0: * Chris@0: * If the items being displayed result from a database query performed using Chris@0: * Drupal's database API, and if you have control over the construction of the Chris@0: * database query, you do not need to call this function directly; instead, you Chris@0: * can simply extend the query object with the 'PagerSelectExtender' extender Chris@0: * before executing it. For example: Chris@0: * @code Chris@0: * $query = db_select('some_table') Chris@0: * ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); Chris@0: * @endcode Chris@0: * Chris@0: * However, if you are using a different method for generating the items to be Chris@0: * paged through, then you should call this function in preparation. Chris@0: * Chris@0: * The following example shows how this function can be used in a controller Chris@0: * that invokes an external datastore with an SQL-like syntax: Chris@0: * @code Chris@0: * // First find the total number of items and initialize the pager. Chris@0: * $where = "status = 1"; Chris@0: * $total = mymodule_select("SELECT COUNT(*) FROM data " . $where)->result(); Chris@0: * $num_per_page = \Drupal::config('mymodule.settings')->get('num_per_page'); Chris@0: * $page = pager_default_initialize($total, $num_per_page); Chris@0: * Chris@0: * // Next, retrieve the items for the current page and put them into a Chris@0: * // render array. Chris@0: * $offset = $num_per_page * $page; Chris@0: * $result = mymodule_select("SELECT * FROM data " . $where . " LIMIT %d, %d", $offset, $num_per_page)->fetchAll(); Chris@0: * $render = []; Chris@0: * $render[] = [ Chris@0: * '#theme' => 'mymodule_results', Chris@0: * '#result' => $result, Chris@0: * ]; Chris@0: * Chris@0: * // Finally, add the pager to the render array, and return. Chris@0: * $render[] = ['#type' => 'pager']; Chris@0: * return $render; Chris@0: * @endcode Chris@0: * Chris@0: * A second example involves a controller that invokes an external search Chris@0: * service where the total number of matching results is provided as part of Chris@0: * the returned set (so that we do not need a separate query in order to obtain Chris@0: * this information). Here, we call pager_find_page() to calculate the desired Chris@0: * offset before the search is invoked: Chris@0: * @code Chris@0: * // Perform the query, using the requested offset from pager_find_page(). Chris@0: * // This comes from a URL parameter, so here we are assuming that the URL Chris@0: * // parameter corresponds to an actual page of results that will exist Chris@0: * // within the set. Chris@0: * $page = pager_find_page(); Chris@0: * $num_per_page = \Drupal::config('mymodule.settings')->get('num_per_page'); Chris@0: * $offset = $num_per_page * $page; Chris@0: * $result = mymodule_remote_search($keywords, $offset, $num_per_page); Chris@0: * Chris@0: * // Now that we have the total number of results, initialize the pager. Chris@0: * pager_default_initialize($result->total, $num_per_page); Chris@0: * Chris@0: * // Create a render array with the search results. Chris@0: * $render = []; Chris@0: * $render[] = [ Chris@0: * '#theme' => 'search_results', Chris@0: * '#results' => $result->data, Chris@0: * '#type' => 'remote', Chris@0: * ]; Chris@0: * Chris@0: * // Finally, add the pager to the render array, and return. Chris@0: * $render[] = ['#type' => 'pager']; Chris@0: * return $render; Chris@0: * @endcode Chris@0: * Chris@0: * @param int $total Chris@0: * The total number of items to be paged. Chris@0: * @param int $limit Chris@0: * The number of items the calling code will display per page. Chris@0: * @param int $element Chris@0: * (optional) An integer to distinguish between multiple pagers on one page. Chris@0: * Chris@0: * @return int Chris@0: * The number of the current page, within the pager represented by $element. Chris@0: * This is determined from the URL query parameter Chris@0: * \Drupal::request()->query->get('page), or 0 by default. However, if a page Chris@0: * that does not correspond to the actual range of the result set was Chris@0: * requested, this function will return the closest page actually within the Chris@0: * result set. Chris@0: */ Chris@0: function pager_default_initialize($total, $limit, $element = 0) { Chris@0: global $pager_page_array, $pager_total, $pager_total_items, $pager_limits; Chris@0: Chris@0: $page = pager_find_page($element); Chris@0: Chris@0: // We calculate the total of pages as ceil(items / limit). Chris@0: $pager_total_items[$element] = $total; Chris@0: $pager_total[$element] = ceil($pager_total_items[$element] / $limit); Chris@0: $pager_page_array[$element] = max(0, min($page, ((int) $pager_total[$element]) - 1)); Chris@0: $pager_limits[$element] = $limit; Chris@0: return $pager_page_array[$element]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Compose a URL query parameter array for pager links. Chris@0: * Chris@0: * @return array Chris@0: * A URL query parameter array that consists of all components of the current Chris@0: * page request except for those pertaining to paging. Chris@0: */ Chris@0: function pager_get_query_parameters() { Chris@0: $query = &drupal_static(__FUNCTION__); Chris@0: if (!isset($query)) { Chris@0: $query = UrlHelper::filterQueryParameters(\Drupal::request()->query->all(), ['page']); Chris@0: } Chris@0: return $query; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prepares variables for pager templates. Chris@0: * Chris@0: * Default template: pager.html.twig. Chris@0: * Chris@0: * Menu callbacks that display paged query results should use #type => pager Chris@0: * to retrieve a pager control so that users can view other results. Format a Chris@0: * list of nearby pages with additional query results. Chris@0: * Chris@0: * @param array $variables Chris@0: * An associative array containing: Chris@0: * - pager: A render element containing: Chris@0: * - #tags: An array of labels for the controls in the pager. Chris@0: * - #element: An optional integer to distinguish between multiple pagers on Chris@0: * one page. Chris@0: * - #parameters: An associative array of query string parameters to append Chris@0: * to the pager links. Chris@0: * - #route_parameters: An associative array of the route parameters. Chris@0: * - #quantity: The number of pages in the list. Chris@0: */ Chris@0: function template_preprocess_pager(&$variables) { Chris@0: $element = $variables['pager']['#element']; Chris@0: $parameters = $variables['pager']['#parameters']; Chris@0: $quantity = $variables['pager']['#quantity']; Chris@0: $route_name = $variables['pager']['#route_name']; Chris@0: $route_parameters = isset($variables['pager']['#route_parameters']) ? $variables['pager']['#route_parameters'] : []; Chris@0: global $pager_page_array, $pager_total; Chris@0: Chris@0: // Nothing to do if there is only one page. Chris@0: if ($pager_total[$element] <= 1) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $tags = $variables['pager']['#tags']; Chris@0: Chris@0: // Calculate various markers within this pager piece: Chris@0: // Middle is used to "center" pages around the current page. Chris@0: $pager_middle = ceil($quantity / 2); Chris@0: // current is the page we are currently paged to. Chris@0: $pager_current = $pager_page_array[$element] + 1; Chris@0: // first is the first page listed by this pager piece (re quantity). Chris@0: $pager_first = $pager_current - $pager_middle + 1; Chris@0: // last is the last page listed by this pager piece (re quantity). Chris@0: $pager_last = $pager_current + $quantity - $pager_middle; Chris@0: // max is the maximum page number. Chris@0: $pager_max = $pager_total[$element]; Chris@0: // End of marker calculations. Chris@0: Chris@0: // Prepare for generation loop. Chris@0: $i = $pager_first; Chris@0: if ($pager_last > $pager_max) { Chris@0: // Adjust "center" if at end of query. Chris@0: $i = $i + ($pager_max - $pager_last); Chris@0: $pager_last = $pager_max; Chris@0: } Chris@0: if ($i <= 0) { Chris@0: // Adjust "center" if at start of query. Chris@0: $pager_last = $pager_last + (1 - $i); Chris@0: $i = 1; Chris@0: } Chris@0: // End of generation loop preparation. Chris@0: Chris@0: // Create the "first" and "previous" links if we are not on the first page. Chris@0: if ($pager_page_array[$element] > 0) { Chris@0: $items['first'] = []; Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, 0), Chris@0: ]; Chris@18: $items['first']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); Chris@0: if (isset($tags[0])) { Chris@0: $items['first']['text'] = $tags[0]; Chris@0: } Chris@0: Chris@0: $items['previous'] = []; Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1), Chris@0: ]; Chris@18: $items['previous']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); Chris@0: if (isset($tags[1])) { Chris@0: $items['previous']['text'] = $tags[1]; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($i != $pager_max) { Chris@0: // Add an ellipsis if there are further previous pages. Chris@0: if ($i > 1) { Chris@0: $variables['ellipses']['previous'] = TRUE; Chris@0: } Chris@0: // Now generate the actual pager piece. Chris@0: for (; $i <= $pager_last && $i <= $pager_max; $i++) { Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, $i - 1), Chris@0: ]; Chris@18: $items['pages'][$i]['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); Chris@0: if ($i == $pager_current) { Chris@0: $variables['current'] = $i; Chris@0: } Chris@0: } Chris@0: // Add an ellipsis if there are further next pages. Chris@0: if ($i < $pager_max + 1) { Chris@0: $variables['ellipses']['next'] = TRUE; Chris@0: } Chris@0: } Chris@0: Chris@0: // Create the "next" and "last" links if we are not on the last page. Chris@0: if ($pager_page_array[$element] < ($pager_max - 1)) { Chris@0: $items['next'] = []; Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1), Chris@0: ]; Chris@18: $items['next']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); Chris@0: if (isset($tags[3])) { Chris@0: $items['next']['text'] = $tags[3]; Chris@0: } Chris@0: Chris@0: $items['last'] = []; Chris@0: $options = [ Chris@0: 'query' => pager_query_add_page($parameters, $element, $pager_max - 1), Chris@0: ]; Chris@18: $items['last']['href'] = Url::fromRoute($route_name, $route_parameters, $options)->toString(); Chris@0: if (isset($tags[4])) { Chris@0: $items['last']['text'] = $tags[4]; Chris@0: } Chris@0: } Chris@0: Chris@0: $variables['items'] = $items; Chris@18: $variables['heading_id'] = Html::getUniqueId('pagination-heading'); Chris@0: Chris@0: // The rendered link needs to play well with any other query parameter used Chris@0: // on the page, like exposed filters, so for the cacheability all query Chris@0: // parameters matter. Chris@0: $variables['#cache']['contexts'][] = 'url.query_args'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the URL query parameter array of a pager link. Chris@0: * Chris@0: * Adds to or adjusts the 'page' URL query parameter so that if you follow the Chris@0: * link, you'll get page $index for pager $element on the page. Chris@0: * Chris@0: * The 'page' URL query parameter is a comma-delimited string, where each value Chris@0: * is the target content page for the corresponding pager $element. For Chris@0: * instance, if we have 5 pagers on a single page, and we want to have a link Chris@0: * to a page that should display the 6th content page for the 3rd pager, and Chris@0: * the 1st content page for all the other pagers, then the URL query will look Chris@0: * like this: ?page=0,0,5,0,0 (page numbering starts at zero). Chris@0: * Chris@0: * @param array $query Chris@0: * An associative array of URL query parameters to add to. Chris@0: * @param int $element Chris@0: * An integer to distinguish between multiple pagers on one page. Chris@0: * @param int $index Chris@0: * The index of the target page, for the given element, in the pager array. Chris@0: * Chris@0: * @return array Chris@0: * The altered $query parameter array. Chris@0: */ Chris@0: function pager_query_add_page(array $query, $element, $index) { Chris@0: global $pager_page_array; Chris@0: Chris@0: // Build the 'page' query parameter. This is built based on the current Chris@0: // page of each pager element (or NULL if the pager is not set), with the Chris@0: // exception of the requested page index for the current element. Chris@0: $max_element = max(array_keys($pager_page_array)); Chris@0: $element_pages = []; Chris@0: for ($i = 0; $i <= $max_element; $i++) { Chris@0: $element_pages[] = ($i == $element) ? $index : (isset($pager_page_array[$i]) ? $pager_page_array[$i] : NULL); Chris@0: } Chris@0: $query['page'] = implode(',', $element_pages); Chris@0: Chris@0: // Merge the query parameters passed to this function with the parameters Chris@0: // from the current request. In case of collision, the parameters passed into Chris@0: // this function take precedence. Chris@0: if ($current_request_query = pager_get_query_parameters()) { Chris@0: $query = array_merge($current_request_query, $query); Chris@0: } Chris@0: return $query; Chris@0: }