diff --git a/plugins/views_data_export_plugin_style_export_csv.inc b/plugins/views_data_export_plugin_style_export_csv.inc index daebeaf..b2daefc 100644 --- a/plugins/views_data_export_plugin_style_export_csv.inc +++ b/plugins/views_data_export_plugin_style_export_csv.inc @@ -44,6 +44,10 @@ class views_data_export_plugin_style_export_csv extends views_data_export_plugin 'default' => TRUE, 'translatable' => FALSE, ); + $options['header_machine_names'] = array( + 'default' => FALSE, + 'translatable' => FALSE, + ); $options['keep_html'] = array( 'default' => FALSE, 'translatable' => FALSE, @@ -104,6 +108,13 @@ class views_data_export_plugin_style_export_csv extends views_data_export_plugin '#title' => t('Make first row a list of column headers.'), '#default_value' => !empty($this->options['header']), ); + $form['header_machine_names'] = array( + '#type' => 'checkbox', + '#title' => t('Convert header labels to machine name format.'), + '#description' => t('This replaces all non-alphanumeric characters in the header with underscores and converts to lowercase.'), + '#default_value' => !empty($this->options['header_machine_names']), + '#dependency' => array('edit-style-options-header' => array(TRUE)), + ); $form['keep_html'] = array( '#type' => 'checkbox', '#default_value' => !empty($this->options['keep_html']), diff --git a/theme/views_data_export.theme.inc b/theme/views_data_export.theme.inc index 8416d96..6d6a973 100644 --- a/theme/views_data_export.theme.inc +++ b/theme/views_data_export.theme.inc @@ -123,6 +123,16 @@ function template_preprocess_views_data_export_csv_header(&$vars) { $output = $converted; } } + if (!empty($vars['options']['header_machine_names'])) { + // Convert the header labels to machine-friendly format: conver to + // lowercase, replace non-alphanumeric characters with underscores. + $output = strtolower($output); + $replace_pattern = '@[^a-z0-9_]+@'; + $replace = '_'; + + $output = preg_replace($replace_pattern, $replace, $output); + } + $vars['header'][$key] = $wrap . str_replace('"', $replace_value, $output) . $wrap; } }