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
* retrieve a pager control so that users can view other results. Format a list
* of nearby pages with additional query results.
*
* @param $variables
* @param array $variables
* An associative array containing:
* - tags: An array of labels for the controls in the pager.
* - 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
* the pager links.
* - quantity: The number of pages in the list.
*
* @ingroup themeable
*/
function theme_pager($variables) {
function template_preprocess_pager(&$variables) {
$tags = $variables['tags'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$quantity = $variables['quantity'];
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:
// Middle is used to "center" pages around the current page.
$pager_middle = ceil($quantity / 2);
@ -197,59 +212,105 @@ function theme_pager($variables) {
$li_previous = '';
$li_next = '';
$li_last = '';
$current_path = current_path();
// Create the "first" and "previous" links if we are not on the first page.
if ($pager_page_array[$element] > 0) {
$li_first = theme('pager_link__first', array(
'text' => (isset($tags[0]) ? $tags[0] : t('« first')),
'page_new' => pager_load_array(0, $element, $pager_page_array),
'element' => $element,
'parameters' => $parameters,
'attributes' => array('rel' => 'first'),
));
$li_previous = theme('pager_link__previous', array(
'text' => isset($tags[1]) ? $tags[1] : t(' previous'),
'page_new' => pager_load_array($pager_page_array[$element] - 1, $element, $pager_page_array),
'element' => $element,
'parameters' => $parameters,
'attributes' => array('rel' => 'prev'),
));
$li_first = array(
'#type' => 'link',
'#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,
),
),
);
$li_previous = array(
'#type' => 'link',
'#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,
),
),
);
}
// Create the "last" and "next" links if we are not on the last page.
if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
$li_next = theme('pager_link__next', array(
'text' => isset($tags[3]) ? $tags[3] : t('next '),
'page_new' => pager_load_array($pager_page_array[$element] + 1, $element, $pager_page_array),
'element' => $element,
'parameters' => $parameters,
'attributes' => array('rel' => 'next'),
));
$li_last = theme('pager_link__last', array(
'text' => (isset($tags[4]) ? $tags[4] : t('last »')),
'page_new' => pager_load_array($pager_total[$element] - 1, $element, $pager_page_array),
'element' => $element,
'parameters' => $parameters,
'attributes' => array('rel' => 'last'),
));
$li_next = array(
'#type' => 'link',
'#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,
),
),
);
$li_last = array(
'#type' => 'link',
'#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,
),
),
);
}
if ($pager_total[$element] > 1) {
if ($li_first) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-first')),
'#markup' => $li_first,
'link' => $li_first,
);
}
if ($li_previous) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-previous')),
'#markup' => $li_previous,
'link' => $li_previous,
);
}
// When there is more than one page, create the pager list.
if ($i != $pager_max) {
// Check whether there are further previous pages.
if ($i > 1) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-ellipsis')),
@ -261,13 +322,24 @@ function theme_pager($variables) {
if ($i < $pager_current) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-item')),
'#markup' => theme('pager_link', array(
'text' => $i,
'page_new' => pager_load_array($i - 1, $element, $pager_page_array),
'element' => $element,
'interval' => ($pager_current - $i),
'parameters' => $parameters,
)),
'link' => array(
'#type' => 'link',
'#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,
'interval' => ($pager_current - $i),
),
),
),
);
}
if ($i == $pager_current) {
@ -279,16 +351,28 @@ function theme_pager($variables) {
if ($i > $pager_current) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-item')),
'#markup' => theme('pager_link', array(
'text' => $i,
'page_new' => pager_load_array($i - 1, $element, $pager_page_array),
'element' => $element,
'interval' => ($i - $pager_current),
'parameters' => $parameters,
)),
'link' => array(
'#type' => 'link',
'#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,
'interval' => ($i - $pager_current),
),
),
),
);
}
}
// Check whether there are further next pages.
if ($i < $pager_max) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-ellipsis')),
@ -300,84 +384,55 @@ function theme_pager($variables) {
if ($li_next) {
$items[] = array(
'#wrapper_attributes' => array('class' => array('pager-next')),
'#markup' => $li_next,
'link' => $li_next,
);
}
if ($li_last) {
$items[] = array(
'#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,
'attributes' => array('class' => array('pager')),
));
$variables['items'] = array(
'#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
* An associative array containing:
* - text: The link text. Also used to figure out the title attribute of the
* link, if it is not provided in $variables['attributes']['title']; in
* this case, $variables['text'] must be one of the standard pager link
* text strings that would be generated by the pager theme functions, such
* 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.
* @param array $query
* 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.
*
* @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) {
$text = $variables['text'];
$page_new = $variables['page_new'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$attributes = $variables['attributes'];
function pager_query_add_page(array $query, $element, $index) {
global $pager_page_array;
// Determine the first result to display on the linked page.
$page_new = pager_load_array($index, $element, $pager_page_array);
$page = isset($_GET['page']) ? $_GET['page'] : '';
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
$parameters['page'] = $new_page;
}
$query = array();
if (count($parameters)) {
$query = drupal_get_query_parameters($parameters, array());
$query['page'] = $new_page;
}
if ($query_pager = pager_get_query_parameters()) {
$query = array_merge($query, $query_pager);
}
// 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));
return $query;
}
/**

View File

@ -3154,21 +3154,7 @@ function drupal_common_theme() {
// From pager.inc.
'pager' => array(
'variables' => array('tags' => array(), 'element' => 0, 'parameters' => array(), 'quantity' => 9),
),
'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()),
'template' => 'pager',
),
// From menu.inc.
'menu_link' => array(

View File

@ -97,10 +97,7 @@ function template_preprocess_search_results(&$variables) {
'#module' => $variables['module'],
);
}
$variables['pager'] = array(
'#theme' => 'pager',
'#tags' => NULL,
);
$variables['pager'] = array('#theme' => 'pager');
// @todo Revisit where this help text is added, see also
// http://drupal.org/node/1918856.
$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 {
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) {
$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(
0 => $this->options['tags']['first'],
1 => $this->options['tags']['previous'],

View File

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

View File

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