Issue #1898474 by mondrake, Cottser, azinoman: pager.inc - Convert theme_ functions to Twig.

8.0.x
Nathaniel Catchpole 2013-06-28 13:34:28 +01:00
parent 0f7247e9b2
commit 7794783062
8 changed files with 229 additions and 144 deletions

View File

@ -142,13 +142,15 @@ function pager_get_query_parameters() {
} }
/** /**
* Returns HTML for a query pager. * Prepares variables for pager templates.
*
* Default template: pager.html.twig.
* *
* Menu callbacks that display paged query results should call theme('pager') to * Menu callbacks that display paged query results should call theme('pager') to
* retrieve a pager control so that users can view other results. Format a list * retrieve a pager control so that users can view other results. Format a list
* of nearby pages with additional query results. * of nearby pages with additional query results.
* *
* @param $variables * @param array $variables
* An associative array containing: * An associative array containing:
* - tags: An array of labels for the controls in the pager. * - tags: An array of labels for the controls in the pager.
* - element: An optional integer to distinguish between multiple pagers on * - element: An optional integer to distinguish between multiple pagers on
@ -156,16 +158,29 @@ function pager_get_query_parameters() {
* - parameters: An associative array of query string parameters to append to * - parameters: An associative array of query string parameters to append to
* the pager links. * the pager links.
* - quantity: The number of pages in the list. * - quantity: The number of pages in the list.
*
* @ingroup themeable
*/ */
function theme_pager($variables) { function template_preprocess_pager(&$variables) {
$tags = $variables['tags']; $tags = $variables['tags'];
$element = $variables['element']; $element = $variables['element'];
$parameters = $variables['parameters']; $parameters = $variables['parameters'];
$quantity = $variables['quantity']; $quantity = $variables['quantity'];
global $pager_page_array, $pager_total; global $pager_page_array, $pager_total;
// Nothing to do if there is only one page.
if ($pager_total[$element] <= 1) {
return;
}
// Fill in default link labels.
$tags = &$variables['tags'];
$tags += array(
t('« first'),
t(' previous'),
'',
t('next '),
t('last »'),
);
// Calculate various markers within this pager piece: // Calculate various markers within this pager piece:
// Middle is used to "center" pages around the current page. // Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2); $pager_middle = ceil($quantity / 2);
@ -197,59 +212,105 @@ function theme_pager($variables) {
$li_previous = ''; $li_previous = '';
$li_next = ''; $li_next = '';
$li_last = ''; $li_last = '';
$current_path = current_path();
// Create the "first" and "previous" links if we are not on the first page. // Create the "first" and "previous" links if we are not on the first page.
if ($pager_page_array[$element] > 0) { if ($pager_page_array[$element] > 0) {
$li_first = theme('pager_link__first', array( $li_first = array(
'text' => (isset($tags[0]) ? $tags[0] : t('« first')), '#type' => 'link',
'page_new' => pager_load_array(0, $element, $pager_page_array), '#title' => $tags[0],
'#href' => $current_path,
'#options' => array(
'query' => pager_query_add_page($parameters, $element, 0),
'attributes' => array(
'title' => t('Go to first page'),
'rel' => 'first',
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'first',
'element' => $element, 'element' => $element,
'parameters' => $parameters, ),
'attributes' => array('rel' => 'first'), ),
)); );
$li_previous = theme('pager_link__previous', array( $li_previous = array(
'text' => isset($tags[1]) ? $tags[1] : t(' previous'), '#type' => 'link',
'page_new' => pager_load_array($pager_page_array[$element] - 1, $element, $pager_page_array), '#title' => $tags[1],
'#href' => $current_path,
'#options' => array(
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
'attributes' => array(
'title' => t('Go to previous page'),
'rel' => 'prev',
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'previous',
'element' => $element, 'element' => $element,
'parameters' => $parameters, ),
'attributes' => array('rel' => 'prev'), ),
)); );
} }
// Create the "last" and "next" links if we are not on the last page. // Create the "last" and "next" links if we are not on the last page.
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) { if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
$li_next = theme('pager_link__next', array( $li_next = array(
'text' => isset($tags[3]) ? $tags[3] : t('next '), '#type' => 'link',
'page_new' => pager_load_array($pager_page_array[$element] + 1, $element, $pager_page_array), '#title' => $tags[3],
'#href' => $current_path,
'#options' => array(
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
'attributes' => array(
'title' => t('Go to next page'),
'rel' => 'next',
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'next',
'element' => $element, 'element' => $element,
'parameters' => $parameters, ),
'attributes' => array('rel' => 'next'), ),
)); );
$li_last = theme('pager_link__last', array( $li_last = array(
'text' => (isset($tags[4]) ? $tags[4] : t('last »')), '#type' => 'link',
'page_new' => pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), '#title' => $tags[4],
'#href' => $current_path,
'#options' => array(
'query' => pager_query_add_page($parameters, $element, $pager_total[$element] - 1),
'attributes' => array(
'title' => t('Go to last page'),
'rel' => 'last',
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'last',
'element' => $element, 'element' => $element,
'parameters' => $parameters, ),
'attributes' => array('rel' => 'last'), ),
)); );
} }
if ($pager_total[$element] > 1) { if ($pager_total[$element] > 1) {
if ($li_first) { if ($li_first) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-first')), '#wrapper_attributes' => array('class' => array('pager-first')),
'#markup' => $li_first, 'link' => $li_first,
); );
} }
if ($li_previous) { if ($li_previous) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-previous')), '#wrapper_attributes' => array('class' => array('pager-previous')),
'#markup' => $li_previous, 'link' => $li_previous,
); );
} }
// When there is more than one page, create the pager list. // When there is more than one page, create the pager list.
if ($i != $pager_max) { if ($i != $pager_max) {
// Check whether there are further previous pages.
if ($i > 1) { if ($i > 1) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-ellipsis')), '#wrapper_attributes' => array('class' => array('pager-ellipsis')),
@ -261,13 +322,24 @@ function theme_pager($variables) {
if ($i < $pager_current) { if ($i < $pager_current) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-item')), '#wrapper_attributes' => array('class' => array('pager-item')),
'#markup' => theme('pager_link', array( 'link' => array(
'text' => $i, '#type' => 'link',
'page_new' => pager_load_array($i - 1, $element, $pager_page_array), '#title' => $i,
'#href' => $current_path,
'#options' => array(
'query' => pager_query_add_page($parameters, $element, $i - 1),
'attributes' => array(
'title' => t('Go to page @number', array('@number' => $i)),
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'item',
'element' => $element, 'element' => $element,
'interval' => ($pager_current - $i), 'interval' => ($pager_current - $i),
'parameters' => $parameters, ),
)), ),
),
); );
} }
if ($i == $pager_current) { if ($i == $pager_current) {
@ -279,16 +351,28 @@ function theme_pager($variables) {
if ($i > $pager_current) { if ($i > $pager_current) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-item')), '#wrapper_attributes' => array('class' => array('pager-item')),
'#markup' => theme('pager_link', array( 'link' => array(
'text' => $i, '#type' => 'link',
'page_new' => pager_load_array($i - 1, $element, $pager_page_array), '#title' => $i,
'#href' => $current_path,
'#options' => array(
'query' => pager_query_add_page($parameters, $element, $i - 1),
'attributes' => array(
'title' => t('Go to page @number', array('@number' => $i)),
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'item',
'element' => $element, 'element' => $element,
'interval' => ($i - $pager_current), 'interval' => ($i - $pager_current),
'parameters' => $parameters, ),
)), ),
),
); );
} }
} }
// Check whether there are further next pages.
if ($i < $pager_max) { if ($i < $pager_max) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-ellipsis')), '#wrapper_attributes' => array('class' => array('pager-ellipsis')),
@ -300,84 +384,55 @@ function theme_pager($variables) {
if ($li_next) { if ($li_next) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-next')), '#wrapper_attributes' => array('class' => array('pager-next')),
'#markup' => $li_next, 'link' => $li_next,
); );
} }
if ($li_last) { if ($li_last) {
$items[] = array( $items[] = array(
'#wrapper_attributes' => array('class' => array('pager-last')), '#wrapper_attributes' => array('class' => array('pager-last')),
'#markup' => $li_last, 'link' => $li_last,
); );
} }
return '<h2 class="visually-hidden">' . t('Pages') . '</h2>' . theme('item_list', array(
'items' => $items, $variables['items'] = array(
'attributes' => array('class' => array('pager')), '#theme' => 'item_list__pager',
)); '#items' => $items,
'#attributes' => array('class' => array('pager')),
);
} }
} }
/** /**
* Returns HTML for a link to a specific query result page. * Adds the 'page' parameter to the query parameter array of a pager link.
* *
* @param $variables * @param array $query
* An associative array containing: * An associative array of query parameters to add to.
* - text: The link text. Also used to figure out the title attribute of the * @param integer $element
* link, if it is not provided in $variables['attributes']['title']; in * An integer to distinguish between multiple pagers on one page.
* this case, $variables['text'] must be one of the standard pager link * @param integer $index
* text strings that would be generated by the pager theme functions, such * The index of the target page in the pager array.
* as a number or t('« first').
* - page_new: The first result to display on the linked page.
* - element: An optional integer to distinguish between multiple pagers on
* one page.
* - parameters: An associative array of query string parameters to append to
* the pager link.
* - attributes: An associative array of HTML attributes to apply to the
* pager link.
* *
* @see theme_pager() * @return array
* The altered $query parameter array.
* *
* @ingroup themeable * @todo Document the pager/element/index architecture and logic. It is not
* clear what is happening in this function as well as pager_load_array(),
* and whether this can be simplified in any way.
*/ */
function theme_pager_link($variables) { function pager_query_add_page(array $query, $element, $index) {
$text = $variables['text']; global $pager_page_array;
$page_new = $variables['page_new'];
$element = $variables['element']; // Determine the first result to display on the linked page.
$parameters = $variables['parameters']; $page_new = pager_load_array($index, $element, $pager_page_array);
$attributes = $variables['attributes'];
$page = isset($_GET['page']) ? $_GET['page'] : ''; $page = isset($_GET['page']) ? $_GET['page'] : '';
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) { if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
$parameters['page'] = $new_page; $query['page'] = $new_page;
}
$query = array();
if (count($parameters)) {
$query = drupal_get_query_parameters($parameters, array());
} }
if ($query_pager = pager_get_query_parameters()) { if ($query_pager = pager_get_query_parameters()) {
$query = array_merge($query, $query_pager); $query = array_merge($query, $query_pager);
} }
return $query;
// Set each pager link title
if (!isset($attributes['title'])) {
static $titles = NULL;
if (!isset($titles)) {
$titles = array(
t('« first') => t('Go to first page'),
t(' previous') => t('Go to previous page'),
t('next ') => t('Go to next page'),
t('last »') => t('Go to last page'),
);
}
if (isset($titles[$text])) {
$attributes['title'] = $titles[$text];
}
elseif (is_numeric($text)) {
$attributes['title'] = t('Go to page @number', array('@number' => $text));
}
}
return l($text, current_path(), array('query' => $query, 'attributes' => $attributes));
} }
/** /**

View File

@ -3154,21 +3154,7 @@ function drupal_common_theme() {
// From pager.inc. // From pager.inc.
'pager' => array( 'pager' => array(
'variables' => array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9), 'variables' => array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9),
), 'template' => 'pager',
'pager_first' => array(
'variables' => array('text' => NULL, 'element' => 0, 'parameters' => array()),
),
'pager_previous' => array(
'variables' => array('text' => NULL, 'element' => 0, 'interval' => 1, 'parameters' => array()),
),
'pager_next' => array(
'variables' => array('text' => NULL, 'element' => 0, 'interval' => 1, 'parameters' => array()),
),
'pager_last' => array(
'variables' => array('text' => NULL, 'element' => 0, 'parameters' => array()),
),
'pager_link' => array(
'variables' => array('text' => NULL, 'page_new' => NULL, 'element' => NULL, 'parameters' => array(), 'attributes' => array()),
), ),
// From menu.inc. // From menu.inc.
'menu_link' => array( 'menu_link' => array(

View File

@ -97,10 +97,7 @@ function template_preprocess_search_results(&$variables) {
'#module' => $variables['module'], '#module' => $variables['module'],
); );
} }
$variables['pager'] = array( $variables['pager'] = array('#theme' => 'pager');
'#theme' => 'pager',
'#tags' => NULL,
);
// @todo Revisit where this help text is added, see also // @todo Revisit where this help text is added, see also
// http://drupal.org/node/1918856. // http://drupal.org/node/1918856.
$variables['help'] = search_help('search#noresults', drupal_help_arg()); $variables['help'] = search_help('search#noresults', drupal_help_arg());

View File

@ -198,7 +198,7 @@ abbr.form-required, abbr.tabledrag-changed, abbr.ajax-changed {
} }
/** /**
* Markup generated by theme_pager(). * Markup generated by pager.html.twig.
*/ */
.item-list .pager { .item-list .pager {
clear: both; clear: both;

View File

@ -0,0 +1,17 @@
{#
/**
* @file
* Default theme implementation to display a pager.
*
* Available variables:
* - items: List of pager items.
*
* @see template_preprocess_pager()
*
* @ingroup themeable
*/
#}
{% if items %}
<h2 class="visually-hidden">{{ 'Pages'|t }}</h2>
{{ items }}
{% endif %}

View File

@ -82,7 +82,8 @@ class Full extends SqlBase {
*/ */
function render($input) { function render($input) {
$pager_theme = $this->view->buildThemeFunctions('pager'); $pager_theme = $this->view->buildThemeFunctions('pager');
// The 0, 1, 3, 4 index are correct. See theme_pager documentation. // The 0, 1, 3, 4 indexes are correct. See the template_preprocess_pager()
// documentation.
$tags = array( $tags = array(
0 => $this->options['tags']['first'], 0 => $this->options['tags']['first'],
1 => $this->options['tags']['previous'], 1 => $this->options['tags']['previous'],

View File

@ -96,7 +96,7 @@ class Mini extends SqlBase {
* Overrides \Drupal\views\Plugin\views\pager\PagerPluginBase::render(). * Overrides \Drupal\views\Plugin\views\pager\PagerPluginBase::render().
*/ */
function render($input) { function render($input) {
// The 1, 3 index are correct, see theme_pager(). // The 1, 3 indexes are correct, see template_preprocess_pager().
$tags = array( $tags = array(
1 => $this->options['tags']['previous'], 1 => $this->options['tags']['previous'],
3 => $this->options['tags']['next'], 3 => $this->options['tags']['next'],

View File

@ -1116,40 +1116,69 @@ function theme_views_form_views_form($variables) {
return drupal_render_children($form); return drupal_render_children($form);
} }
/**
* Theme function for the Mini pager.
*/
function theme_views_mini_pager($vars) { function theme_views_mini_pager($vars) {
global $pager_page_array, $pager_total; global $pager_page_array, $pager_total;
$tags = $vars['tags']; $tags = &$vars['tags'];
$element = $vars['element']; $element = $vars['element'];
$parameters = $vars['parameters']; $parameters = $vars['parameters'];
// Fill in default link labels.
$tags += array(
1 => t(''),
3 => t(''),
);
// Current is the page we are currently paged to. // Current is the page we are currently paged to.
$pager_current = $pager_page_array[$element] + 1; $pager_current = $pager_page_array[$element] + 1;
// End of marker calculations. $current_path = current_path();
$li_previous = array(); $li_previous = array();
if ($pager_total[$element] > 1 && $pager_page_array[$element] > 0) { if ($pager_total[$element] > 1 && $pager_page_array[$element] > 0) {
$li_previous = array( $li_previous = array(
'#theme' => 'pager_link__previous', '#type' => 'link',
'#text' => (isset($tags[1]) ? $tags[1] : t('')), '#title' => $tags[1],
'#attributes' => array('title' => t('Go to previous page')), '#href' => $current_path,
'#page_new' => pager_load_array($pager_page_array[$element] - 1, $element, $pager_page_array), '#options' => array(
'#element' => $element, 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
'#interval' => 1, 'attributes' => array(
'#parameters' => $parameters, 'title' => t('Go to previous page'),
'rel' => 'prev',
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'previous',
'element' => $element,
'interval' => -1,
),
),
); );
} }
$li_next = array(); $li_next = array();
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) { if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
$li_next = array( $li_next = array(
'#theme' => 'pager_link__next', '#type' => 'link',
'#text' => (isset($tags[3]) ? $tags[3] : t('')), '#title' => $tags[3],
'#attributes' => array('title' => t('Go to next page')), '#href' => $current_path,
'#page_new' => pager_load_array($pager_page_array[$element] + 1, $element, $pager_page_array), '#options' => array(
'#element' => $element, 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
'#interval' => 1, 'attributes' => array(
'#parameters' => $parameters, 'title' => t('Go to next page'),
'rel' => 'next',
),
// Below is ignored by default, supplied to support hook_link_alter
// implementations.
'pager_context' => array(
'link_type' => 'previous',
'element' => $element,
'interval' => 1,
),
),
); );
} }
@ -1167,7 +1196,7 @@ function theme_views_mini_pager($vars) {
) + $li_next; ) + $li_next;
$item_list = array( $item_list = array(
'#theme' => 'item_list', '#theme' => 'item_list__pager',
'#items' => $items, '#items' => $items,
'#title' => NULL, '#title' => NULL,
'#list_type' => 'ul', '#list_type' => 'ul',