From 9481d3b59ad3d63c1e660d23f8fadee0deeae9b6 Mon Sep 17 00:00:00 2001
From: dereine <dereine@99340.no-reply.drupal.org>
Date: Sat, 9 Jun 2012 16:02:47 +0200
Subject: [PATCH] Issue #1602372 by dawehner: Convert views settings to config.

---
 config/views.settings.yml | 24 ++++++++++
 drush/views.drush.inc     | 29 +++++++-----
 includes/admin.inc        | 99 +++++++++++++++++++++++++++++++--------
 includes/plugins.inc      |  2 +-
 views.install             |  7 +++
 views.module              |  2 +-
 6 files changed, 128 insertions(+), 35 deletions(-)
 create mode 100644 config/views.settings.yml

diff --git a/config/views.settings.yml b/config/views.settings.yml
new file mode 100644
index 000000000000..7348bad698d4
--- /dev/null
+++ b/config/views.settings.yml
@@ -0,0 +1,24 @@
+# Basic settings
+views_ui_show_listing_filters: '0'
+views_ui_show_advanced_help_warning: '1'
+views_ui_show_master_display: '0'
+views_ui_show_advanced_column: '0'
+views_ui_display_embed: '0'
+views_ui_custom_theme: '_default'
+views_exposed_filter_any_label: 'new_any'
+views_ui_always_live_preview: '1'
+views_ui_show_preview_information: '1'
+views_ui_show_sql_query_where: 'above'
+views_ui_show_sql_query: '0'
+views_ui_show_performance_statistics: '0'
+views_show_additional_queries: '0'
+
+# Advanced settings
+views_skip_cache: '0'
+views_sql_signature: '0'
+views_no_javascript: '0'
+views_devel_output: '0'
+views_localization_plugin: ''
+views_devel_region: 'footer'
+views_display_extenders: { }
+
diff --git a/drush/views.drush.inc b/drush/views.drush.inc
index 771c1c38dafd..f5bc2dce5401 100644
--- a/drush/views.drush.inc
+++ b/drush/views.drush.inc
@@ -234,19 +234,22 @@ function views_revert_view($view) {
  * Change the settings to a more developer oriented value.
  */
 function views_development_settings() {
-  variable_set('views_ui_show_listing_filters', TRUE);
-  variable_set('views_ui_show_master_display', TRUE);
-  variable_set('views_ui_show_advanced_column', TRUE);
-  variable_set('views_ui_always_live_preview', FALSE);
-  variable_set('views_ui_always_live_preview_button', TRUE);
-  variable_set('views_ui_show_preview_information', TRUE);
-  variable_set('views_ui_show_sql_query', TRUE);
-  variable_set('views_ui_show_performance_statistics', TRUE);
-  variable_set('views_show_additional_queries', TRUE);
-  variable_set('views_devel_output', TRUE);
-  variable_set('views_devel_region', 'message');
-  variable_set('views_ui_display_embed', TRUE);
-  $message = dt("Setup the new views settings.");
+  config('views.settings')
+    ->set('views_ui_show_listing_filters', TRUE)
+    ->set('views_ui_show_master_display', TRUE)
+    ->set('views_ui_show_advanced_column', TRUE)
+    ->set('views_ui_always_live_preview', FALSE)
+    ->set('views_ui_always_live_preview_button', TRUE)
+    ->set('views_ui_show_preview_information', TRUE)
+    ->set('views_ui_show_sql_query', TRUE)
+    ->set('views_ui_show_performance_statistics', TRUE)
+    ->set('views_show_additional_queries', TRUE)
+    ->set('views_devel_output', TRUE)
+    ->set('views_devel_region', 'message')
+    ->set('views_ui_display_embed', TRUE)
+    ->save();
+
+  $message = dt('Setup the new views settings.');
   drush_log($message, 'success');
 }
 
diff --git a/includes/admin.inc b/includes/admin.inc
index e0b171b1c04d..99b58ba4f152 100644
--- a/includes/admin.inc
+++ b/includes/admin.inc
@@ -4670,6 +4670,8 @@ function views_ui_admin_settings_basic() {
   $form = array();
   $form['#attached']['css'] = views_ui_get_admin_css();
 
+  $config = config('views.settings');
+
   $options = array();
   foreach (list_themes() as $name => $theme) {
     if ($theme->status) {
@@ -4684,40 +4686,40 @@ function views_ui_admin_settings_basic() {
   $form['basic']['views_ui_show_listing_filters'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show filters on the list of views'),
-    '#default_value' => variable_get('views_ui_show_listing_filters', FALSE),
+    '#default_value' => $config->get('views_ui_show_listing_filters'),
   );
   $form['basic']['views_ui_show_advanced_help_warning'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show advanced help warning'),
-    '#default_value' => variable_get('views_ui_show_advanced_help_warning', TRUE),
+    '#default_value' => $config->get('views_ui_show_advanced_help_warning'),
   );
 
   $form['basic']['views_ui_show_master_display'] = array(
     '#type' => 'checkbox',
     '#title' => t('Always show the master display'),
     '#description' => t('Advanced users of views may choose to see the master (i.e. default) display.'),
-    '#default_value' => variable_get('views_ui_show_master_display', FALSE),
+    '#default_value' => $config->get('views_ui_show_master_display'),
   );
 
   $form['basic']['views_ui_show_advanced_column'] = array(
     '#type' => 'checkbox',
     '#title' => t('Always show advanced display settings'),
     '#description' => t('Default to showing advanced display settings, such as relationships and contextual filters.'),
-    '#default_value' => variable_get('views_ui_show_advanced_column', FALSE),
+    '#default_value' => $config->get('views_ui_show_advanced_column'),
   );
 
   $form['basic']['views_ui_display_embed'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show the embed display in the ui.'),
     '#description' => t('Allow advanced user to use the embed view display. The plugin itself works if it\'s not visible in the ui'),
-    '#default_value' => variable_get('views_ui_display_embed', FALSE),
+    '#default_value' => $config->get('views_ui_display_embed'),
   );
 
   $form['basic']['views_ui_custom_theme'] = array(
     '#type' => 'select',
     '#title' => t('Custom admin theme for the Views UI'),
     '#options' => array('_default' => t('- Use default -')) + $options,
-    '#default_value' => variable_get('views_ui_custom_theme', '_default'),
+    '#default_value' => $config->get('views_ui_custom_theme'),
     '#description' => t('In some cases you might want to select a different admin theme for the Views UI.')
   );
 
@@ -4725,7 +4727,7 @@ function views_ui_admin_settings_basic() {
     '#type' => 'select',
     '#title' => t('Label for "Any" value on non-required single-select exposed filters'),
     '#options' => array('old_any' => '<Any>', 'new_any' => t('- Any -')),
-    '#default_value' => variable_get('views_exposed_filter_any_label', 'new_any'),
+    '#default_value' => $config->get('views_exposed_filter_any_label'),
   );
 
   $form['live_preview'] = array(
@@ -4736,7 +4738,7 @@ function views_ui_admin_settings_basic() {
   $form['live_preview']['views_ui_always_live_preview'] = array(
     '#type' => 'checkbox',
     '#title' => t('Automatically update preview on changes'),
-    '#default_value' => variable_get('views_ui_always_live_preview', TRUE),
+    '#default_value' => $config->get('views_ui_always_live_preview'),
   );
 
 //  $form['live_preview']['views_ui_always_live_preview_button'] = array(
@@ -4748,7 +4750,7 @@ function views_ui_admin_settings_basic() {
   $form['live_preview']['views_ui_show_preview_information'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show information and statistics about the view during live preview'),
-    '#default_value' => variable_get('views_ui_show_preview_information', TRUE),
+    '#default_value' => $config->get('views_ui_show_preview_information'),
   );
 
   $form['live_preview']['views_ui_show_sql_query_where'] = array(
@@ -4758,7 +4760,7 @@ function views_ui_admin_settings_basic() {
       'below' => t('Below the preview'),
     ),
 //    '#id' => 'edit-show-sql',
-    '#default_value' => variable_get('views_ui_show_sql_query_where', 'above'),
+    '#default_value' => $config->get('views_ui_show_sql_query_where'),
     // @todo: try to fix this states behavior.
 //    '#dependency' => array('edit-views-ui-show-preview-information' => array(TRUE)),
 //    '#states' => array(
@@ -4773,7 +4775,7 @@ function views_ui_admin_settings_basic() {
   $form['live_preview']['views_ui_show_sql_query'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show the SQL query'),
-    '#default_value' => variable_get('views_ui_show_sql_query', FALSE),
+    '#default_value' => $config->get('views_ui_show_sql_query'),
 //    '#states' => array(
 //      'visible' => array(
 //        ':input[name="views_ui_show_preview_information"]' => array('checked' => TRUE),
@@ -4783,7 +4785,7 @@ function views_ui_admin_settings_basic() {
   $form['live_preview']['views_ui_show_performance_statistics'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show performance statistics'),
-    '#default_value' => variable_get('views_ui_show_performance_statistics', FALSE),
+    '#default_value' => $config->get('views_ui_show_performance_statistics'),
 //    '#states' => array(
 //      'visible' => array(
 //        ':input[name="views_ui_show_preview_information"]' => array('checked' => TRUE),
@@ -4795,7 +4797,7 @@ function views_ui_admin_settings_basic() {
     '#type' => 'checkbox',
     '#title' => t('Show other queries run during render during live preview'),
     '#description' => t("Drupal has the potential to run many queries while a view is being rendered. Checking this box will display every query run during view render as part of the live preview."),
-    '#default_value' => variable_get('views_show_additional_queries', FALSE),
+    '#default_value' => $config->get('views_show_additional_queries'),
 //    '#states' => array(
 //      'visible' => array(
 //        ':input[name="views_ui_show_preview_information"]' => array('checked' => TRUE),
@@ -4805,7 +4807,37 @@ function views_ui_admin_settings_basic() {
 
 //  $form['live_preview']['views_ui_show_performance_statistics_where'] = array(
 
-  return system_settings_form($form);
+  $form['actions']['#type'] = 'actions';
+  $form['actions']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save configuration'),
+  );
+  $form['#submit'][] = 'views_ui_admin_settings_basic_submit';
+
+  return $form;
+}
+
+/**
++ * Form builder submit handler; Handle submission the basic views settings.
++ * @ingroup forms
++ * @see system_settings_form()
++ */
+function views_ui_admin_settings_basic_submit(&$form, &$form_state) {
+  config('views.settings')
+    ->set('views_ui_show_listing_filters', $form_state['values']['views_ui_show_listing_filters'])
+    ->set('views_ui_show_advanced_help_warning', $form_state['values']['views_ui_show_advanced_help_warning'])
+    ->set('views_ui_show_master_display', $form_state['values']['views_ui_show_master_display'])
+    ->set('views_ui_show_advanced_column', $form_state['values']['views_ui_show_advanced_column'])
+    ->set('views_ui_display_embed', $form_state['values']['views_ui_display_embed'])
+    ->set('views_ui_custom_theme', $form_state['values']['views_ui_custom_theme'])
+    ->set('views_exposed_filter_any_label', $form_state['values']['views_exposed_filter_any_label'])
+    ->set('views_ui_always_live_preview', $form_state['values']['views_ui_always_live_preview'])
+    ->set('views_ui_show_preview_information', $form_state['values']['views_ui_show_preview_information'])
+    ->set('views_ui_show_sql_query_where', $form_state['values']['views_ui_show_sql_query_where'])
+    ->set('views_ui_show_sql_query', $form_state['values']['views_ui_show_sql_query'])
+    ->set('views_ui_show_performance_statistics', $form_state['values']['views_ui_show_performance_statistics'])
+    ->set('views_show_additional_queries', $form_state['values']['views_show_additional_queries'])
+    ->save();
 }
 
 /**
@@ -4815,6 +4847,8 @@ function views_ui_admin_settings_advanced() {
   $form = array();
   $form['#attached']['css'] = views_ui_get_admin_css();
 
+  $config = config('views.settings');
+
   $form['cache'] = array(
     '#type' => 'fieldset',
     '#title' => t('Caching'),
@@ -4824,7 +4858,7 @@ function views_ui_admin_settings_advanced() {
     '#type' => 'checkbox',
     '#title' => t('Disable views data caching'),
     '#description' => t("Views caches data about tables, modules and views available, to increase performance. By checking this box, Views will skip this cache and always rebuild this data when needed. This can have a serious performance impact on your site."),
-    '#default_value' => variable_get('views_skip_cache', FALSE),
+    '#default_value' => $config->get('views_skip_cache'),
   );
 
   $form['cache']['clear_cache'] = array(
@@ -4843,21 +4877,21 @@ function views_ui_admin_settings_advanced() {
     '#title' => t('Add Views signature to all SQL queries'),
     '#description' => t("All Views-generated queries will include the name of the views and display 'view-name:display-name' as a string  at the end of the SELECT clause. This makes identifying Views queries in database server logs simpler, but should only be used when troubleshooting."),
 
-    '#default_value' => variable_get('views_sql_signature', FALSE),
+    '#default_value' => $config->get('views_sql_signature'),
   );
 
   $form['debug']['views_no_javascript'] = array(
     '#type' => 'checkbox',
     '#title' => t('Disable JavaScript with Views'),
     '#description' => t("If you are having problems with the JavaScript, you can disable it here. The Views UI should degrade and still be usable without javascript; it's just not as good."),
-    '#default_value' => variable_get('views_no_javascript', FALSE),
+    '#default_value' => $config->get('views_no_javascript'),
   );
 
   $form['debug']['views_devel_output'] = array(
     '#type' => 'checkbox',
     '#title' => t('Enable views performance statistics/debug messages via the Devel module'),
     '#description' => t("Check this to enable some Views query and performance statistics/debug messages <em>if Devel is installed</em>."),
-    '#default_value' => variable_get('views_devel_output', FALSE),
+    '#default_value' => $config->get('views_devel_output'),
   );
 
   $form['locale'] = array(
@@ -4883,7 +4917,7 @@ function views_ui_admin_settings_advanced() {
   $form['debug']['views_devel_region'] = array(
     '#type' => 'select',
     '#title' => t('Page region to output performance statistics/debug messages'),
-    '#default_value' => variable_get('views_devel_region', 'footer'),
+    '#default_value' => $config->get('views_devel_region'),
     '#options' => $regions,
     '#states' => array(
       'visible' => array(
@@ -4907,7 +4941,32 @@ function views_ui_admin_settings_advanced() {
     );
   }
 
-  return system_settings_form($form);
+
+  $form['actions']['#type'] = 'actions';
+  $form['actions']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save configuration'),
+  );
+  $form['#submit'][] = 'views_ui_admin_settings_advanced_submit';
+
+  return $form;
+}
+
+/**
+ * Form builder submit handler; Handle submission the basic views settings..
+ * @ingroup forms
+ * @see system_settings_form()
+ */
+function views_ui_admin_settings_advanced_submit(&$form, &$form_state) {
+  config('views.settings')
+    ->set('views_skip_cache', $form_state['values']['views_skip_cache'])
+    ->set('views_sql_signature', $form_state['values']['views_sql_signature'])
+    ->set('views_no_javascript', $form_state['values']['views_no_javascript'])
+    ->set('views_devel_output', $form_state['values']['views_devel_output'])
+    ->set('views_localization_plugin', $form_state['values']['views_localization_plugin'])
+    ->set('views_devel_region', $form_state['values']['views_devel_region'])
+    ->set('views_display_extenders', isset($form_state['values']['views_display_extenders']) ? $form_state['values']['views_display_extenders'] : array())
+    ->save();
 }
 
 /**
diff --git a/includes/plugins.inc b/includes/plugins.inc
index 0df73ac0a4f8..10f21733b625 100644
--- a/includes/plugins.inc
+++ b/includes/plugins.inc
@@ -575,7 +575,7 @@ function plugin_title() {
  * Get enabled display extenders.
  */
 function views_get_enabled_display_extenders() {
-  $enabled = array_filter(variable_get('views_display_extenders', array()));
+  $enabled = array_filter(config('views.settings')->get('views_display_extenders'));
   $options = views_fetch_plugin_names('display_extender');
   foreach ($options as $name => $plugin) {
     $enabled[$name] = $name;
diff --git a/views.install b/views.install
index 95ed67e97a96..c5d0a552ff00 100644
--- a/views.install
+++ b/views.install
@@ -632,3 +632,10 @@ function views_update_7301() {
   );
   db_change_field('views_view', 'name', 'name', $new_field);
 }
+
+/**
+ * Convert settings to the config system.
+ */
+function views_update_8000() {
+  update_variables_to_config('views.settings');
+}
diff --git a/views.module b/views.module
index 27bb3aa9f079..9ad9e24697d4 100644
--- a/views.module
+++ b/views.module
@@ -1255,7 +1255,7 @@ function views_get_plugin($type, $plugin, $reset = FALSE) {
  * @return The name of the localization plugin.
  */
 function views_get_localization_plugin() {
-  $plugin = variable_get('views_localization_plugin', '');
+  $plugin = config('views.settings')->get('views_localization_plugin');
   // Provide sane default values for the localization plugin.
   if (empty($plugin)) {
     if (module_exists('locale')) {
-- 
GitLab