views_record_count.module in Views Record Count 8
Same filename and directory in other branches
display record count in header or footer if pager is implemented in view's display.
This module also has overridden feature. The main entry point of this module is views_record_count_form_alter(), where it add the views record count and message region selector with default values and its submit handler attached. If overridden values are being used in the view, in form alter hook there is an overridden handler also. So that they will be reflected in multiple display such as page, block, default etc.
File
views_record_count.moduleView source
<?php
/**
* @file
* display record count in header or footer if pager is implemented
* in view's display.
*
* This module also has overridden feature. The main entry
* point of this module is views_record_count_form_alter(), where it add the
* views record count and message region selector with default values and its
* submit handler attached. If overridden values are being used in the view, in
* form alter hook there is an overridden handler also. So that they will be
* reflected in multiple display such as page, block, default etc.
*
*/
/**
* Implements hook_views_pre_render().
*/
function views_record_count_views_pre_render(\Drupal\views\ViewExecutable $view) {
$total = isset($view->total_rows) ? $view->total_rows : count($view->result);
$current_display = $view->current_display;
$display_options = $view->storage;
$label_view = $display_options
->label();
$pager = $view
->getPager();
$region = \Drupal::config('views_record_count.settings')
->get("region.{$label_view}.{$current_display}");
$count = \Drupal::config('views_record_count.settings')
->get("count.{$label_view}.{$current_display}");
if ($count == 'yes' && isset($pager)) {
$view->element['#attached']['library'][] = 'views_record_count/drupal.recordcount.admin';
$message['#markup'] = '<div id="views-record-count-text"><strong>' . \Drupal::translation()
->formatPlural($total, '@count record', '@count records', array(
'@count' => $total,
)) . t(' found.') . '</strong></div>';
$message_location = !empty($region) ? $region : 'footer';
if ($message_location == 'footer') {
$message_locate = 'attachment_after';
}
else {
$message_locate = 'attachment_before';
}
$view->{$message_locate} = $message;
}
}
/**
* Implements hook_form_alter().
*/
function views_record_count_form_views_ui_edit_display_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if ($form_state
->get('section') == 'pager_options') {
$view_current_id = $form_state
->get('display_id');
$storage_view = $form_state
->getStorage();
$view = $storage_view['view'];
$display_options = $view
->get('storage');
$form['options'] += array(
views_record_count_options($display_options, $view_current_id),
);
$form['buttons']['#weight'] = 10;
$form['actions']['submit']['#submit'][] = 'views_record_count_submission';
}
}
/**
* function to get views record count options
*
* @param object $display
* This is the display object
*
* @return array
* a form element array
*/
function views_record_count_options($display, $view_current_id) {
$form = array();
$label_view = $display
->label();
$views_record_count = \Drupal::config('views_record_count.settings')
->get("count.{$label_view}.{$view_current_id}");
$views_record_count_region = \Drupal::config('views_record_count.settings')
->get("region.{$label_view}.{$view_current_id}");
$views_record_count_default = $views_record_count ? $views_record_count : 'no';
$views_record_count_options = array(
'yes' => t('Yes'),
'no' => t('No'),
);
$form['views_record_count'] = array(
'#type' => 'radios',
'#title' => t('Display Record Count'),
'#options' => $views_record_count_options,
'#default_value' => $views_record_count_default,
'#weight' => 1,
);
$views_record_count_region_default = $views_record_count_region ? $views_record_count_region : 'footer';
$views_record_count_region_options = array(
'header' => t('Header'),
'footer' => t('Footer'),
);
$form['views_record_count_region'] = array(
'#type' => 'radios',
'#title' => t('Choose Region for Record Count Message'),
'#options' => $views_record_count_region_options,
'#default_value' => $views_record_count_region_default,
'#weight' => 2,
'#dependency' => array(
'radio:views_record_count' => array(
'yes',
),
),
);
return $form;
}
/**
* Submit handler for views record count form.
*/
function views_record_count_submission(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$view_current_id = $form_state
->get('display_id');
$storage_view = $form_state
->getStorage();
$view = $storage_view['view'];
$display_options = $view
->get('storage');
$label_view = $display_options
->label();
$views_record_count = $form_state
->getValue('views_record_count');
$views_record_count_region = $form_state
->getValue('views_record_count_region');
$count = "count.{$label_view}.{$view_current_id}";
$region = "region.{$label_view}.{$view_current_id}";
\Drupal::service('config.factory')
->getEditable('views_record_count.settings')
->set($count, $views_record_count)
->save();
\Drupal::service('config.factory')
->getEditable('views_record_count.settings')
->set($region, $views_record_count_region)
->save();
}
Functions
Name![]() |
Description |
---|---|
views_record_count_form_views_ui_edit_display_form_alter | Implements hook_form_alter(). |
views_record_count_options | function to get views record count options |
views_record_count_submission | Submit handler for views record count form. |
views_record_count_views_pre_render | Implements hook_views_pre_render(). |