In Drupal 7, there was an option to use tokens for the fields selected with the values of the first result row. That option doesn't exist in D8+
I'm going to mark this as Normal, since the functionality existed in D7
@maddentim You should use the view's ID. That's always present and always unique.
/** * Implements hook_views_post_execute(). */ function MODULE_views_post_execute(ViewExecutable $view): void { if ($view->id() !== 'some_value') { return; } }
- πΊπΈUnited States tea.time
I ran into this and found it surprising there isn't more support for tokens or otherwise dynamically generating pieces of the filename, like using values from an argument.
Thanks @maddentim for the approach in #7. In my case, I found I needed to use `hook_views_pre_view()` to alter the display handler 'filename' config value early enough to happen before the file is generated - see `Drupal\views_data_export\Plugin\views\display\DataExport::processBatch()`.
- π¦πΉAustria maxilein
This worked for me in D10.2.5
use Drupal\views\ViewExecutable; /** * Implements hook_views_pre_build(&$view) * To alter download filename */ function MYMODULE_views_pre_view(ViewExecutable $view, $display_id, array &$args) { if ($view->current_display == 'machinename_of_view_export_display') { if (!empty(\Drupal::request()->query->get('myqueryparam'))) { $param = \Drupal::request()->query->get('myqueryparam'); $dateoffilecreation = date("Ymd_His"); $file = 'meinfilename'.$param.'_v'.$dateoffilecreation.'.csv'; // Alter the downloadable csv sheets name $view->display_handler->options['filename'] = $file; } // $view->display_handler->options['filename'] = 'heheh.csv'; } }
@maxilein The
$display_id
is a parameter to the function. You can check that instead of$view->current_display
.You should also check
$view->id()
, because multiple views can have a display with the same ID.- π¦πΉAustria maxilein
use Drupal\views\ViewExecutable; /** * Implements hook_views_pre_build(&$view) * To alter download filename */ function MYMODULE_views_pre_view(ViewExecutable $view, $display_id, array &$args) { if ($view->id() == 'your_view_id" && $display_id == 'machinename_of_view_export_display') { if (!empty(\Drupal::request()->query->get('myqueryparam'))) { $param = \Drupal::request()->query->get('myqueryparam'); $dateoffilecreation = date("Ymd_His"); $file = 'myfilename'.$param.'_v'.$dateoffilecreation.'.csv'; // Alter the downloadable csv sheets name $view->display_handler->options['filename'] = $file; } } }
And for anybody looking: here is how you find view id etc.
https://stackoverflow.com/questions/72370467/how-do-i-find-the-view-name... - πΊπΈUnited States brad.bulger
The workarounds are helpful, but the available tokens need to be expanded. For example, in Drupal 7 we were using
%view-%exposed-%timestamp-full.csv
for the filename. Including the query parameters - the %exposed filters - is very useful to have in the filename of the export.