Token based file names

Created on 12 June 2020, over 4 years ago
Updated 6 April 2024, 7 months ago

It would be useful to record data from the view, (such the specific subset of data being rendered, or general site information like the date) in the file name. We may produce a patch for this functionality for a client, so just making this ticket as a placeholder.

Any feedback or suggestions from the module maintainers before we start would be much appreciated.

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡³πŸ‡ΏNew Zealand thomasmurphy

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 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...

Production build 0.71.5 2024