Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Using the ImportExportController with the List behavior (useList: true)

0
by mjauvin, last modified on August 13th, 2024

Here's a few tricks to use the ImportExportController:

Note: In this example, my controller is called Invoices and my model Invoice.

create a seperate list definition to export your records in your controller:

Add this to your controller:

public $implement = [
    'Backend.Behaviors.FormController',
    'Backend.Behaviors.ImportExportController',
    'Backend.Behaviors.ListController',
];

public $listConfig = [
    'list' => 'config_list.yaml', # your default list definition
    'export' => [
        'list' => '$/author/plugin/models/invoice/export_columns.yaml',
        'modelClass' => 'Author\Plugin\Models\Invoice',
    ],
];

public $importExportConfig = 'config_import_export.yaml';

Use this config_import_export.yaml (note the use of raw: true):

export:
  fileName: invoices_export.csv
  useList:
    raw: true
    definition: export

And add this to your controller's _list_toolbar.php:

<a
    href="<?= Backend::url('author/plugin/invoices/export') ?>"
    class="wn-icon-download">
    Export
</a>

You can also modify the query for your export list by adding this method to your controller:

public function listExtendQuery($query, $definition)
{
    if ($definition === 'export') {
        return $query->where('status', 'complete');
    }
}

And modify the records by adding this method to your controller:

public function listExtendRecords($records, $definition = null)
{
    if ($definition === 'export') {
        foreach ($records as $record) {
            ...
        }
    }
}

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?