Purpose
Wonderful Relations provides a default way to export data using the get_datatable_as_file
action. This action exports the entire query result underlying a DataTable into an Excel file for download. The export respects any active filters but ignores pagination, ensuring that all relevant data is included.
Visual Example
Custom Export
However, if the default export is not sufficient, for example, when additional data needs to be included beyond what is displayed in the DataTable, a custom export function can be implemented.
Below is an example of how to define a custom CSV export action that retrieves all persons from the system and provides the data as a downloadable CSV file.
namespace TS\YourProject\Modules\Person\Actions;
use TS\WonderfulRelations\Includes\Constants;
use TS\WonderfulRelations\Modules\Actions\ActionType\ActionType;
use TS\WonderfulRelations\Modules\Actions\ActionType\ActionTypeCallback;
use TS\WonderfulRelations\Modules\Query\Executor;
use TS\WonderfulRelations\Utils\Utils;
class Export extends ActionTypeCallback {
public function __construct() {
parent::__construct();
new ActionType( "your_project_person_csv_export", $this );
}
function execute_callback(): array {
$result = ( new Executor( "your_project_get_all_persons", array() ) )->get_results();
$result_with_header = array_merge( [ array_keys( $result[0] ) ];
$data = Utils::array_to_csv_string( $result_with_header, $result ), ';' );
$filename = "Persons.csv";
return array( Constants::CALLBACK_TYPE_FILE, array( 'filename' => $filename, 'data' => $data ) );
}
}
new Export();