Skip to content
Snippets Groups Projects

Issue #2887450: Re-add Drush commands

3 files
+ 341
90
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 112
0
<?php
namespace Drupal\views_data_export\Drush\Commands;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandError;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\UserSession;
use Drupal\views\Views;
use Drupal\views_data_export\Plugin\views\display\DataExport;
use Drush\Attributes as CLI;
use Drush\Commands\AutowireTrait;
use Drush\Commands\DrushCommands;
/**
* Provides Drush commands for exporting views.
*/
class ViewsDataExportCommands extends DrushCommands {
use AutowireTrait;
public function __construct(
protected AccountSwitcherInterface $accountSwitcher,
) {
parent::__construct();
}
/**
* Implements views_data_export command arguments validation.
*
* @hook validate views_data_export:views-data-export
*/
public function viewsDataExportValidate(CommandData $commandData) {
$input = $commandData->input();
$view_name = $input->getArgument('view_name');
$display_id = $input->getArgument('display_id');
$output_file = $input->getArgument('output_file');
$options = $commandData->options();
$force = $options['force'];
// Verify view existence.
$view = Views::getView($view_name);
if (is_null($view)) {
return new CommandError(dt('The view !view does not exist.', ['!view' => $view_name]));
}
// Verify existence of the display.
if ($view->setDisplay($display_id) === FALSE) {
return new CommandError(dt('The view !view does not have the !display display.', [
'!view' => $view_name,
'!display' => $display_id,
]));
}
// Verify the display type.
$view_display = $view->getDisplay();
if ($view_display->getPluginId() !== 'data_export') {
return new CommandError(dt('Incorrect display_id provided, expected a views data export display, found !display instead.', [
'!display' => $view_display->getPluginId(),
]));
}
// Verify final file location.
if (!empty($output_file)) {
if (!$force && file_exists($output_file)) {
return new CommandError(dt('The desired output file already exists. Please remove the file and try again.'));
}
if (!is_writable(dirname($output_file))) {
return new CommandError(dt('The desired output location or file is unwritable. Please check your permissions or location and try again.'));
}
}
}
/**
* Executes views_data_export display of a view and writes the output to file.
*/
#[CLI\Command(name: 'views_data_export:views-data-export', aliases: ['vde'])]
#[CLI\Argument(name: 'view_name', description: 'The name of the view.')]
#[CLI\Argument(name: 'display_id', description: 'The id of the views_data_export display to execute on the view.')]
#[CLI\Argument(name: 'output_file', description: 'he location to place the final generated file. Leave blank for default location in private files.')]
#[CLI\Argument(name: 'arguments', description: 'The views contextual filter arguments, if any.')]
#[CLI\Usage(name: 'views_data_export:views-data-export my_view_name views_data_export_display_id output_file_path', description: 'Export my_view_name:views_data_export_display_id.')]
#[CLI\Option(name: 'force', description: 'Overwrite the output file if the file exists.')]
public function viewsDataExport(string $view_name, string $display_id, ?string $output_file = NULL, string $arguments = ''): void {
$this->logger()->notice(dt('Starting data export..'));
$args = [];
if ($arguments) {
$args = explode('/', $arguments);
}
$this->accountSwitcher->switchTo(new UserSession(['uid' => 1]));
$result = DataExport::buildResponse($view_name, $display_id, $args, [
'vde_drush',
$output_file,
]);
if (!empty($result['drush_batch_process_finished']) || !empty($result['drush_process_finished'])) {
if (!empty($result[0]['vde_file'])) {
$this->logger()->success(dt('Data export saved to !output_file', ['!output_file' => $result[0]['vde_file']]));
}
$this->logger()->success(dt('Data export finished.'));
}
else {
$this->logger()->error(dt('Unable to export views data. Please check site logs.'));
}
$this->accountSwitcher->switchBack();
}
}
Loading