3 namespace Drupal\views\Plugin\views\area;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\Xss;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\views\Plugin\views\style\DefaultSummary;
11 * Views area handler to display some configurable result summary.
13 * @ingroup views_area_handlers
15 * @ViewsArea("result")
17 class Result extends AreaPluginBase {
22 protected function defineOptions() {
23 $options = parent::defineOptions();
25 $options['content'] = [
26 'default' => $this->t('Displaying @start - @end of @total'),
35 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
36 parent::buildOptionsForm($form, $form_state);
38 '#theme' => 'item_list',
40 '@start -- the initial record number in the set',
41 '@end -- the last record number in the set',
42 '@total -- the total records in the set',
43 '@label -- the human-readable name of the view',
44 '@per_page -- the number of items per page',
45 '@current_page -- the current page number',
46 '@current_record_count -- the current page record count',
47 '@page_count -- the total page count',
50 $list = drupal_render($item_list);
52 '#title' => $this->t('Display'),
53 '#type' => 'textarea',
55 '#default_value' => $this->options['content'],
56 '#description' => $this->t('You may use HTML code in this field. The following tokens are supported:') . $list,
63 public function query() {
64 if (strpos($this->options['content'], '@total') !== FALSE) {
65 $this->view->get_total_rows = TRUE;
72 public function render($empty = FALSE) {
73 // Must have options and does not work on summaries.
74 if (!isset($this->options['content']) || $this->view->style_plugin instanceof DefaultSummary) {
78 $format = $this->options['content'];
79 // Calculate the page totals.
80 $current_page = (int) $this->view->getCurrentPage() + 1;
81 $per_page = (int) $this->view->getItemsPerPage();
82 // @TODO: Maybe use a possible is views empty functionality.
83 // Not every view has total_rows set, use view->result instead.
84 $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result);
85 $label = Html::escape($this->view->storage->label());
86 // If there is no result the "start" and "current_record_count" should be
87 // equal to 0. To have the same calculation logic, we use a "start offset"
88 // to handle all the cases.
89 $start_offset = empty($total) ? 0 : 1;
90 if ($per_page === 0) {
92 $start = $start_offset;
96 $page_count = (int) ceil($total / $per_page);
97 $total_count = $current_page * $per_page;
98 if ($total_count > $total) {
99 $total_count = $total;
101 $start = ($current_page - 1) * $per_page + $start_offset;
104 $current_record_count = ($end - $start) + $start_offset;
105 // Get the search information.
107 $replacements['@start'] = $start;
108 $replacements['@end'] = $end;
109 $replacements['@total'] = $total;
110 $replacements['@label'] = $label;
111 $replacements['@per_page'] = $per_page;
112 $replacements['@current_page'] = $current_page;
113 $replacements['@current_record_count'] = $current_record_count;
114 $replacements['@page_count'] = $page_count;
116 if (!empty($total) || !empty($this->options['empty'])) {
117 $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format));
119 // Return as render array.
121 '#markup' => $output,