Skip to content
Snippets Groups Projects
Commit 93bfe448 authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3043675 by Masami, yas, Xiaohua Guan, baldwinlouie: Enable to configure...

Issue #3043675 by Masami, yas, Xiaohua Guan, baldwinlouie: Enable to configure views pager options from admin configuration page
parent 5cda33bb
No related branches found
No related tags found
No related merge requests found
......@@ -554,6 +554,39 @@ function aws_cloud_update_8132() {
aws_cloud_add_fields('cloud_config', 'aws_ec2', 'field_spreadsheet_pricing_url');
}
/**
* Set default pager options for views.
*/
function aws_cloud_update_8133() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('aws_cloud.settings');
$config->set('aws_cloud_view_items_per_page', 50);
$config->set('aws_cloud_view_expose_items_per_page', TRUE);
$config->save();
$views = [
'views.view.aws_cloud_key_pairs',
'views.view.aws_elastic_ip',
'views.view.aws_images',
'views.view.aws_instances',
'views.view.aws_network_interfaces',
'views.view.aws_security_group',
'views.view.aws_snapshot',
'views.view.aws_volume',
];
$options = [
'display.default.display_options.pager.options.items_per_page' => 50,
'display.default.display_options.pager.options.expose.items_per_page' => TRUE,
'display.default.display_options.pager.options.expose.items_per_page_options' => implode(',', AWS_CLOUD_VIEWS_ITEMS_PER_PAGE_OPTIONS),
'display.default.display_options.pager.options.expose.items_per_page_options_all' => TRUE,
];
foreach ($views as $view_name) {
aws_cloud_update_views_configuration($view_name, $options);
}
}
/**
* Helper function to add fields to the entity type.
*
......
......@@ -29,6 +29,16 @@ use Drupal\Core\Database\Query\Condition;
// 1 day in seconds.
const AWS_CLOUD_REIMPORT_INTERVAL = 86400;
// Items per page option of views.
const AWS_CLOUD_VIEWS_ITEMS_PER_PAGE_OPTIONS = [
10 => '10',
15 => '15',
20 => '20',
25 => '25',
50 => '50',
100 => '100',
];
/**
* Implements hook_libraries_info().
*/
......@@ -2112,3 +2122,21 @@ function aws_cloud_ping_metadata_security_server() {
}
return $pinged;
}
/**
* Update the configuration of view.
*
* @param string $view_name
* The name of view.
* @param array $options
* The key and value array of view configuration.
*/
function aws_cloud_update_views_configuration($view_name, array $options) {
$config_factory = \Drupal::configFactory();
$view = $config_factory->getEditable($view_name);
foreach ($options as $key => $value) {
$view->set($key, $value);
}
$view->save(TRUE);
}
......@@ -8,4 +8,6 @@ aws_cloud_scheduler_tag: "Schedule"
aws_cloud_scheduler_periods: ""
aws_cloud_instance_terminate: false
aws_cloud_view_refresh_interval: 10
aws_cloud_view_items_per_page: 50
aws_cloud_view_expose_items_per_page: true
google_credential_file_path: 'private://aws_cloud/.gapps/client_secrets.json'
......@@ -23,5 +23,9 @@ aws_cloud.settings:
type: boolean
aws_cloud_view_refresh_interval:
type: integer
aws_cloud_view_items_per_page:
type: integer
aws_cloud_view_expose_items_per_page:
type: boolean
google_credential_file_path:
type: string
......@@ -49,11 +49,17 @@ class AwsCloudAdminSettings extends ConfigFormBase {
'#type' => 'details',
'#title' => $this->t('Views'),
'#open' => TRUE,
'#description' => $this->t("Note that selecting the default option will overwrite View's settings."),
];
$form['views']['aws_cloud_view_refresh_interval'] = [
'#type' => 'number',
$form['views']['refresh_options'] = [
'#type' => 'details',
'#title' => $this->t('View refresh interval'),
'#open' => TRUE,
];
$form['views']['refresh_options']['aws_cloud_view_refresh_interval'] = [
'#type' => 'number',
'#description' => $this->t('Refresh content of views at periodical intervals.'),
'#default_value' => $config->get('aws_cloud_view_refresh_interval'),
'#min' => 1,
......@@ -61,6 +67,27 @@ class AwsCloudAdminSettings extends ConfigFormBase {
'#field_suffix' => 'seconds',
];
$form['views']['pager_options'] = [
'#type' => 'details',
'#title' => $this->t('Pager options'),
'#open' => TRUE,
];
$form['views']['pager_options']['aws_cloud_view_expose_items_per_page'] = [
'#type' => 'checkbox',
'#title' => $this->t('Allow user to control the number of items displayed in views.'),
'#default_value' => $config->get('aws_cloud_view_expose_items_per_page'),
'#description' => $this->t('When enabled, an "Items per page" dropdown listbox is shown.'),
];
$form['views']['pager_options']['aws_cloud_view_items_per_page'] = [
'#type' => 'select',
'#options' => AWS_CLOUD_VIEWS_ITEMS_PER_PAGE_OPTIONS,
'#title' => $this->t('Items per page'),
'#description' => $this->t('Number of items to display on each page in views.'),
'#default_value' => $config->get('aws_cloud_view_items_per_page'),
];
$form['notification'] = [
'#type' => 'details',
'#title' => $this->t('Notification'),
......@@ -190,11 +217,60 @@ class AwsCloudAdminSettings extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory()->getEditable('aws_cloud.settings');
$form_state->cleanValues();
$views_settings = [];
foreach ($form_state->getValues() as $key => $value) {
if ($key == 'aws_cloud_view_items_per_page') {
$views_settings[$key] = (int) $value;
}
elseif ($key == 'aws_cloud_view_expose_items_per_page') {
$views_settings[$key] = (boolean) $value;
}
$config->set($key, Html::escape($value));
}
$config->save();
if (!empty($views_settings)) {
$this->updateViewsPagerOptions($views_settings);
}
parent::submitForm($form, $form_state);
}
/**
* Update views pager options.
*
* @param array $views_settings
* The key and value array of views pager options.
*/
private function updateViewsPagerOptions(array $views_settings) {
$views = [
'views.view.aws_cloud_key_pairs',
'views.view.aws_elastic_ip',
'views.view.aws_images',
'views.view.aws_instances',
'views.view.aws_network_interfaces',
'views.view.aws_security_group',
'views.view.aws_snapshot',
'views.view.aws_volume',
];
$options = [];
foreach ($views_settings as $key => $value) {
$view_key = str_replace('aws_cloud_view_', '', $key);
if (strpos($view_key, 'expose_') !== FALSE) {
$view_key = str_replace('expose_', 'expose.', $view_key);
if ($value) {
$options["display.default.display_options.pager.options.expose.items_per_page_options"] = implode(',', AWS_CLOUD_VIEWS_ITEMS_PER_PAGE_OPTIONS);
$options["display.default.display_options.pager.options.expose.items_per_page_options_all"] = TRUE;
}
}
$options["display.default.display_options.pager.options.$view_key"] = $value;
}
foreach ($views as $view_name) {
aws_cloud_update_views_configuration($view_name, $options);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment