If there's a way to set the separator to a tab character, I sure don't know what it is. This type of file is actually quite common, and used by the likes of Amazon for sellers to upload product catalogs and ads. I was able to hack support for this in rather easily, but something cleaner would be nice.

What I did was set the separator to "{tab}" in the UI, and modified the views-data-export-csv-body.tpl.php and views-data-export-csv-header.tpl.php files to replace that string with an actual tab character on the fly.

<?php

// Print out exported items.
// Foo: Lets have some tabs...
$foosep = $separator==="{tab}" ? "\t" : $separator;
foreach ($themed_rows as $count => $item_row):
  print implode($foosep, $item_row) . "\r\n";
endforeach;

As I said, definitely a hack, but it works like a charm.
Thanks for an extremely useful module, it saved me a lot of time.

Comments

kevster’s picture

Would definitely be useful to roll this into the module as a feature - google merchant center also uses tsv format. Thanks for sharing your hack..

marleo’s picture

I'm using 6.x. I copied a tab character from a text editor (gedit on Linux) and just pasted it into the separator field in the export style options.
Not terribly user-friendly, since you see "nothing" when you come back to the config screen. But it appears to have worked perfectly - I got a tab-separated file out.

problue solutions’s picture

Hi DrFoo,

I need this for the exact reason kevster mentions in post #2, google merchant center.

Is the code you listed supposed to completely replace what's in:

views-data-export-csv-body.tpl.php
AND
views-data-export-csv-header.tpl.php

It looks like it should maybe only be in the first file, what should i do with the 2nd file?

the existing code in the 2nd file is:

<?php

// Print out header row, if option was selected.
if ($options['header']) {
  print implode($separator, $header) . "\r\n";
}
dave bruns’s picture

Just confirming that pasting a tab character from a text editor into the separator input worked for me using 7.x-3.x-dev. It seems wrong somehow, but it works.

problue solutions’s picture

If I paste a tab character into the separator input it works in the view preview, the values are separated by tabs, but the actual file output has no separator at all.

Any ideas?

screon’s picture

I'm also interested in a more user-friendly approach.

problue solutions’s picture

If anyone could even point me in the direction of where I can hard code a tab character as the separator for when the view is output as a file this would help me a lot as a workaround for now.

alfthecat’s picture

Yes, I'd very much want this feature too. Actually very surprised it's not supported right now.
TAB separation is very useful, I know of cases where exploding by feeds_tamper module will only work with TAB's.

woprrr’s picture

Issue summary: View changes

Hi,

pending a patch that allows retrieve the \ t properly I propose this solution rather "clean".

You can try this in a custom module or in your template (admin theme its not realy good its better in custom module).

function your_module_name_preprocess_views_data_export_csv_body(&$vars) {
  $vars['seperator'] = $vars['separator'] = '"\t"';
}

function your_module_name_preprocess_views_data_export_csv(&$vars) {
  $vars['seperator'] = $vars['separator'] = '"\t"';
}

function your_module_name_preprocess_views_data_export_csv_header(&$vars) {
  $vars['seperator'] = $vars['separator'] = '"\t"';
}

replace "your_module_name" by name of your module. If you want i can propose a small patch for the module for replace {tab} by real tabs.

If you want you can view the code in view_data_export_theme.inc l.130 ==> template_preprocess_views_data_export_csv_body() && template_preprocess_views_data_export_csv_header() && template_preprocess_views_data_export_csv().

yuvaraju.an’s picture

Hi Problue Solutions,

Paste the following code for views-data-export-csv-header.tpl.php it works for me.

<?php

// Print out header row, if option was selected.
if ($options['header']) {
print implode($foosep = $separator==="{tab}" ? "\t" :$separator, $header) . "\r\n";
}

robertwb’s picture

StatusFileSize
new931 bytes

This issue caught my eye as I needed something similar. I implemented the attached patch based on the sample code above, however, instead of the notation "{tab}", I set it up so that it would accept "\t" in the separator field. Since this is stashed in the database with what appears to be some encoding, I had to use stripslashes() to keep it from returnin "\\t". This should allow the use of other escaped special characters as well http://en.wikipedia.org/wiki/Escape_sequences_in_C

robertwb’s picture

Status: Active » Needs review

Marked for review.

IckZ’s picture

works for me! thx robertwb!

vincenzo gambino’s picture

StatusFileSize
new3.16 KB

robertwb solution did not work for me having a more complex CSV file to create. It strips out quotes to and takes commas as tab separator.

I also think that this implementation should not be done in tpl.php or in a preprocess function. So I have created a patch that add a Tab delimiter checkbox in the setting form of the cvs view.

vincenzo gambino’s picture

StatusFileSize
new3.64 KB

Added Tab delimiter option to be exportable.

robertwb’s picture

@Vincenzo Gambino - thanks for this - I think you are right on, putting this in the plugin class instead of the template makes perfect sense, as does making it exportable. However, it seems you're introducing a new option field specifically for tab?

I'm curious that I thought my approach which supports c-style delimiters may be more flexible - but you found it to be less so. Can you supply a use case for the more complex CSV that broke my implementation?

Thanks!

steven jones’s picture

Issue tags: +ComputerMinds
adamgerthel’s picture

Status: Needs review » Reviewed & tested by the community

Tested patch from #15. Looks good!

jasonsafro’s picture

Tested patch from #15. Looks good!

pallavi_sugandhi’s picture

Status: Reviewed & tested by the community » Needs work

The patch #15 is not working with "Provide as file" option in csv settings of data export view.

robertwb’s picture

@pallavi_sugandhi - can you provide some more detail? All the tests that I have done personally on this patch have been with "Provide as File" option set and I found it to be working fine.

steinmb’s picture

Version: 7.x-3.0-beta5 » 7.x-3.x-dev

Make sure the patch apply to dev.

steven jones’s picture

Status: Needs work » Closed (won't fix)

Sorry for the lack of attention to your issue, please accept my apologies.

Drupal 7 is going to be end-of-life'd by the community in approximately 1 month.

As such, I am closing all non-critical looking, non-PHP compatibility issues for Views Data Export to tidy up the issue queues and reduce the noise. You can read about this on #3492246: Close down Drupal 7 issues.

If you feel like this issue has been closed by mistake, please do comment about re-opening it.
If you feel like the ticket is still relevant for the 8.x-1.x version of the module, then please search for a duplicate issue first, and if there really isn't one (and you've looked properly) then change the version on the ticket and re-open.

Thanks to everyone involved in this issue: for reporting it, and moving it along, it is truly appreciated.
The Drupal community wouldn't be what it is today without your involvement and effort, so I'm sorry that we couldn't get this issue resolved. Hopefully we'll work together in a future issue though, and get that one resolved :)