codepress / admin-columns-issues

Issues for Admin Columns Pro
https://www.admincolumns.com/
11 stars 0 forks source link

Filters to alter Export headers and rows #1250

Closed DGStefan closed 5 years ago

DGStefan commented 5 years ago

It would be great if there are hooks available to alter the export data. This allows users to add/remove extra columns to the export file.

Proposal to add the following hooks:

@param array $row Associative array of data for corresponding headers @param int $id Item ID to export for @param ListScreen $list_screen Exportable list screen instance $row = apply_filters( 'ac/export/row', $row, $id, $list_screen );

@param array $headers Associative array of data for corresponding headers @param ListScreen $list_screen Exportable list screen instance $headers = apply_filters( 'ac/export/headers', $headers, $list_screen );

zitrusblau commented 5 years ago

For our use case described here (https://www.admincolumns.com/forums/topic/csv-export-split-column-value-into-multiple-columns/) we might even need a hook that filters all available rows just before writing the final data to csv since only then we are able to determine how many extra columns we would actually need to add to the csv headers (depending on the row that holds the maximum amount of values inside a specific column).

i.e. we have an initial column set like

    ID  |             contacts           |     date  
---------------------------------------------------------
     1  |  Stephe, Caroline, Zelda       |   2019-05-19
    ...
    99  |  Harold, Mumbo, Jumbo, Helga   |   2018-02-23

Now we want to split up the values in the contacts column into as much extra columns as the one row with the maximum amount of values (string needs to be split by comma in this example of course) available in the data set, eg. row '99' which would yield four extra columns. That also means that rows that contain less values inside the contacts column will end up with empty additional columns (no problem).

Does this make sense to you?

DGStefan commented 5 years ago

@zitrusblau It does make sense. But the way our plugin exports the data, we need to determine the headers first, before we process the data rows. So I'm not sure if adding a hook for all data is something that we can provide. But we'll have a look at what we can do.

zitrusblau commented 5 years ago

Sure. Thanks for looking into this.

DGStefan commented 5 years ago

@zitrusblau I discussed this within the team and we're not going to create a call for the complete data. The main reason for this is that export works in batches, so it's not logical to get a hook that contains the complete dataset.

But we thought about your use case. You could use the header hook to do some checks to find out how many calls you need. Of course, you have to find a way to determine this for all your records, but you only have to do this once. Maybe you can find a way to do this in a performance-optimized way and maybe even cache the result.

zitrusblau commented 5 years ago

Sure, batch processing and our request might interfere. So thanks for letting me now about your decision. Does your proposal mean that you will at least add those two filter hooks mentioned in your initial posting?

DGStefan commented 5 years ago

Yes, the upcoming release will have two hook that allows you to alter both the headers and rows.