views_csv_source
Features
Initially based on Views JSON Source, this module is an extension of the Views module designed to work with data stored in a comma-separated value (CSV) file. The file can be uploaded or hosted on a remote server. The module provides a way to read the data from the CSV file and display it in any of the formats available through Views. The module also provides a way to filter and sort the data using the Views module. It's great for making tables and charts from an existing CSV file and not having to enter the data into an entity.
Why Use This Module?
There are several reasons you may want to use this module:
- You want the power of Views to view data, but you don't want to create a new content type or entity to store the data. This could be because the data do not fit with your site's content model, or because you don't want to create a new content type for a one-off use case.
- You want to connect to a data source that's not already within your site and is not in a database or easily accessible through an API.
- The amount of data you want to display at once is too large for Views to handle (in our experience, Views can rarely handle more than a few hundred rows of data at once).
Post-Installation
When creating a View, choose "CSV" in the "Show" field. On the "Edit View" page, in the "Advanced" section, find the "Query settings" section and click the "Settings" link. Inside the modal, you must enter a link to the CSV file. It can either be a link to a file within your filesystem or a link to a file sitting on a different server. Links that do not have a .csv extension will work as long as the file generated by the link is a CSV file. If you need authentication to access the file, you can pass headers like this: {"Authorization":"Basic xxxxx","Content-Type":"application/csv"} where xxxxx is the base64 encoded username and password.
In some cases, one single file will be too massive for efficient processing on
your server, and it may make sense to break the file into multiple files (e.g.
"my_file__category_a", "my_file__category_b", etc.). You can use contextual
filters ("arguments") to point your view to the correct file by adding a
token in the link to your CSV file, for example:
/sites/default/files/my_file__{{raw_arguments.value}}.csv
If you do so, you may have issues with the "Column Selector" working. You can
either type the column name in the text field that the "Column Selector"
becomes when it doesn't find any columns, or you can use a "settings" file
placed in the same location as your intended files that contains one or several
rows and is named: "my_file__{{raw_arguments.value}}.csv" (adjust for your
actual file name and the contextual filter ID).
After applying the settings, you can proceed to select the columns you want to
display in the view, with each column being represented by a field in the view.
All of your columns will use the same field plugin, which has the title:
"CSV Field". Inside the settings for the field, you can choose the column you
want to display using the "Column Selector" dropdown.
Like with other Views fields, you can rewrite the output of the field, add
custom classes, and even use the values with other field plugins, such as the
one provided by the contributed module Views Simple Math Field.
There are four types of filters available: "CSV Field", "CSV Field Options",
"CSV Field Date", and "CSV Field Numeric". The "CSV Field" filter is a
typical text filter. The "CSV Field Options" filter provides a dropdown that
is populated with the unique values from the selected column (sorted). If your
column has more than one option in a cell, you can select a separator in the
filter settings and click "Refresh value options" to update the dropdown
values. The date filter acts like a normal date filter, except it can filter
on year alone; if you input "2025" and the operator is "greater than", the
filter will use "2025-12-31", and if you select "less than", it will use
"2025-01-01". If you select "equals", it will use "between" the first and
last day of the year. The numeric filter allows for numeric comparisons.
All of these filters can be exposed.
The module also supports relationships to other files: in the
"Relationships" section, you can add a "CSV Relationship", which has
three settings: "Relationship CSV File", "Base left column", and
"Relationship Right Column". Here is an example to help explain this
concept:
Base file path (added in the "Query" settings):
/sites/default/files/my_file.csv
Base file contents:
country code,country,population
IN,India,1438069596
CN,China,1422584933
US,United States,343477335
Relationship file path (added to "Relationship" settings):
/sites/default/files/my_file_regions.csv
Relationship file contents:
country,region
India,Asia
China,Asia
United States,Americas
"Base left column" (added to "Relationship" settings): country
"Relationship Right Column" (added to "Relationship" settings): country
You could then add a "CSV Field Options" filter, select "CSV Relationship"
in the dropdown at the top of the settings, and then select "region" in
the column selector.
The module also comes with a sort and a contextual filter plugin.
Similar projects
Supporting this Module
If you would like to support development of this module, please consider funding us through our Open Collective: https://opencollective.com/drupal-charts