We have numerous clients that request the ability to periodically export various portions of their content as CSV files. Craft provides a native element export tool that appears at the bottom of every element index page in the control panel, but the generated CSV files contain far more columns than our clients need and the resulting values are often limited to their respective database column values, which doesn’t always suit our clients’ needs.
For example, a site may contain a single Checkboxes field to store a user’s email communication preferences. The client would like a users CSV export formatted in such a way that each checkbox option has its own column with a simple “yes” or “no” value that represents whether or not the user has opted to receive those particular communications. In order to accomplish that, we need a custom reporting/exporting tool.
We previously used another third-party option to construct such exports but the plugin went through a long period of instability that resulted in some of our clients’ control panels crashing entirely without any kind of intervention. We couldn’t have that! Additionally, the plugin struggled or failed entirely when generating CSVs with thousands and thousands of rows and many of our clients’ sites have an extraordinary amount of content. We had to do something to give our clients what they needed without breaking their mission-critical websites.
That’s why we came up with the idea for Lab Reports. We wanted to create a custom reporting/exporting tool that felt familiar, but could handle very large datasets without causing the control panel to hang or crash.
The report configurations are not limited to any particular Craft element type or database table. Basic report queries are configured via Twig templates using syntax all Craft developers are familiar with. For larger datasets, we recommend using an Advanced report configuration which requires a little bit of PHP knowledge but examples are provided with the plugin.
Reports can be generated manually from the control panel or they can be generated on a schedule via cron jobs. Lab Reports keeps a complete history of every report generated and provides administrative users with download links in the control panel.
The formats of the various CSV column values are left entirely up to the client/developer. If someone requests a column containing an entry title rather than its ID, that’s not a problem for Lab Reports! Check out how easy it is to configure a Basic export of entry content in the example below:
{# Initialize the rows with the column headers. #}
{% set rows = [[
'ID',
'Title',
'Date Created',
'Cover Image',
'Publish Year',
'Edition'
]] %}
{# Construct a query and execute it. #}
{% set entries = craft.entries.section('books')
.with('coverImage')
.orderBy('title').all() %}
{# Loop through the entries and add them to the report. #}
{% for entry in entries %}
{% set coverImage = entry.coverImage[0] ?? null %}
{% set coverImageUrl = coverImage ? coverImage.url : null %}
{% set rows = rows|merge([[
entry.id,
entry.title,
entry.dateCreated|date('F J, Y g:i A'),
coverImageUrl,
entry.publishYear,
entry.edition
]]) %}
{% endfor %}
{# Tell Lab Reports to construct the CSV file and save the report record. #}
{% do report.build(rows) %}
If you need the ability to construct and execute customized, routine CSV content exports of any size for your client, consider checking out Lab Reports in the Craft Plugin Store.