diff --git a/accessibility_scanner.module b/accessibility_scanner.module index 570dd77..4957258 100644 --- a/accessibility_scanner.module +++ b/accessibility_scanner.module @@ -11,5 +11,39 @@ function accessibility_scanner_theme($existing, $type, $theme, $path) { 'wpa-achecker-preview' => [ 'variables' => ['summary' => NULL, 'url' => NULL, 'view_button' => NULL], ], + 'wpa-achecker-summary' => [ + 'variables' => ['result' => NULL], + ], + ]; +} + +/** + * Implements hook_views_pre_view(). + */ +function accessibility_scanner_views_pre_view($view, $display_id, array &$args) { + $item = array( + 'id' => 'achecker_result', + 'table' => 'views', + 'field' => 'achecker_result', + 'relationship' => 'none', + 'group_type' => 'none', + 'admin_label' => '', + 'plugin_id' => 'achecker_result', + ); + + $view->setHandler('default', 'header', 'achecker_result', $item); +} + +/** + * Implements hook_views_data(). + */ +function accessibility_scanner_views_data() { + $data['views']['achecker_result'] = [ + 'title' => t('AChecker Summary Results'), + 'help' => t('Shows achecker result summary'), + 'area' => [ + 'id' => 'achecker_result', + ], ]; + return $data; } diff --git a/css/achecker.css b/css/achecker.css index 7309ad0..349c6b1 100644 --- a/css/achecker.css +++ b/css/achecker.css @@ -101,3 +101,20 @@ .achecker-clear { clear: both; } + +.achecker-runSummary { + background-color: #f8f8f8; + margin: 5px 0; + padding: 1px 5px 5px; +} + +.achecker-runSummaryColumn { + float: left; + max-width: 33%; + padding: 0 15px; +} + +.achecker-runSummaryList { + margin: 0; + padding: 0 20px; +} diff --git a/src/Plugin/views/area/AcheckerResult.php b/src/Plugin/views/area/AcheckerResult.php new file mode 100644 index 0000000..eefe15e --- /dev/null +++ b/src/Plugin/views/area/AcheckerResult.php @@ -0,0 +1,141 @@ + $this->t('Displaying @start - @end of @total'), + ]; + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + $item_list = [ + '#theme' => 'item_list', + '#items' => [ + '@start -- the initial record number in the set', + '@end -- the last record number in the set', + '@total -- the total records in the set', + '@label -- the human-readable name of the view', + '@per_page -- the number of items per page', + '@current_page -- the current page number', + '@current_record_count -- the current page record count', + '@page_count -- the total page count', + ], + ]; + $list = \Drupal::service('renderer')->render($item_list); + $form['content'] = [ + '#title' => $this->t('Display'), + '#type' => 'textarea', + '#rows' => 3, + '#default_value' => $this->options['content'], + '#description' => $this->t('You may use HTML code in this field. The following tokens are supported:') . $list, + ]; + } + + /** + * {@inheritdoc} + */ + public function query() { + if (strpos($this->options['content'], '@total') !== FALSE) { + $this->view->get_total_rows = TRUE; + } + } + + /** + * {@inheritdoc} + */ + public function render($empty = FALSE) { + + if (isset($this->view->argument['vid_1'])) { + $run_id = intval($this->view->argument['vid_1']->getValue()); + // TODO: Inject this? + $storage = \Drupal::service('accessibility_scanner.achecker_summary_storage'); + if ($result = $storage->getResult($run_id)) { + return [ + '#theme' => 'wpa-achecker-summary', + '#result' => $result, + ]; + } + } + return ['#markup' => 'todo']; + // Must have options and does not work on summaries. + if (!isset($this->options['content']) || $this->view->style_plugin instanceof DefaultSummary) { + return []; + } + $output = ''; + $format = $this->options['content']; + // Calculate the page totals. + $current_page = (int) $this->view->getCurrentPage() + 1; + $per_page = (int) $this->view->getItemsPerPage(); + // @TODO: Maybe use a possible is views empty functionality. + // Not every view has total_rows set, use view->result instead. + $total = isset($this->view->total_rows) ? $this->view->total_rows : count($this->view->result); + $label = Html::escape($this->view->storage->label()); + // If there is no result the "start" and "current_record_count" should be + // equal to 0. To have the same calculation logic, we use a "start offset" + // to handle all the cases. + $start_offset = empty($total) ? 0 : 1; + if ($per_page === 0) { + $page_count = 1; + $start = $start_offset; + $end = $total; + } + else { + $page_count = (int) ceil($total / $per_page); + $total_count = $current_page * $per_page; + if ($total_count > $total) { + $total_count = $total; + } + $start = ($current_page - 1) * $per_page + $start_offset; + $end = $total_count; + } + $current_record_count = ($end - $start) + $start_offset; + // Get the search information. + $replacements = []; + $replacements['@start'] = $start; + $replacements['@end'] = $end; + $replacements['@total'] = $total; + $replacements['@label'] = $label; + $replacements['@per_page'] = $per_page; + $replacements['@current_page'] = $current_page; + $replacements['@current_record_count'] = $current_record_count; + $replacements['@page_count'] = $page_count; + // Send the output. + if (!empty($total) || !empty($this->options['empty'])) { + $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format)); + // Return as render array. + return [ + '#markup' => $output, + ]; + } + + return []; + } + +} diff --git a/templates/wpa-achecker-preview.html.twig b/templates/wpa-achecker-preview.html.twig index 58e6959..9bcc589 100644 --- a/templates/wpa-achecker-preview.html.twig +++ b/templates/wpa-achecker-preview.html.twig @@ -14,13 +14,6 @@