e8d194773282bfce14da6558c1ed9cad2e9ab7d5
[yaffs-website] / web / core / modules / views / src / Plugin / views / area / Result.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\area;
4
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;
9
10 /**
11  * Views area handler to display some configurable result summary.
12  *
13  * @ingroup views_area_handlers
14  *
15  * @ViewsArea("result")
16  */
17 class Result extends AreaPluginBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function defineOptions() {
23     $options = parent::defineOptions();
24
25     $options['content'] = [
26       'default' => $this->t('Displaying @start - @end of @total'),
27     ];
28
29     return $options;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
36     parent::buildOptionsForm($form, $form_state);
37     $item_list = [
38       '#theme' => 'item_list',
39       '#items' => [
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',
48       ],
49     ];
50     $list = \Drupal::service('renderer')->render($item_list);
51     $form['content'] = [
52       '#title' => $this->t('Display'),
53       '#type' => 'textarea',
54       '#rows' => 3,
55       '#default_value' => $this->options['content'],
56       '#description' => $this->t('You may use HTML code in this field. The following tokens are supported:') . $list,
57     ];
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function query() {
64     if (strpos($this->options['content'], '@total') !== FALSE) {
65       $this->view->get_total_rows = TRUE;
66     }
67   }
68
69   /**
70    * {@inheritdoc}
71    */
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) {
75       return [];
76     }
77     $output = '';
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) {
91       $page_count = 1;
92       $start = $start_offset;
93       $end = $total;
94     }
95     else {
96       $page_count = (int) ceil($total / $per_page);
97       $total_count = $current_page * $per_page;
98       if ($total_count > $total) {
99         $total_count = $total;
100       }
101       $start = ($current_page - 1) * $per_page + $start_offset;
102       $end = $total_count;
103     }
104     $current_record_count = ($end - $start) + $start_offset;
105     // Get the search information.
106     $replacements = [];
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;
115     // Send the output.
116     if (!empty($total) || !empty($this->options['empty'])) {
117       $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format));
118       // Return as render array.
119       return [
120         '#markup' => $output,
121       ];
122     }
123
124     return [];
125   }
126
127 }