Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
3.1 kB
1
Indexable
Never
name: 'Custom Export'
type: module
description: 'Custom module for exporting data using VBO'
core_version_requirement: ^8 || ^9
package: Custom
dependencies:
  - drupal:views_bulk_operations




use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsEntityActionBase;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;

/**
 * Implements hook_action_info_alter().
 */
function custom_export_action_info_alter(array &$actions) {
  // Define a new VBO action for exporting.
  $actions['views_bulk_operations_export_csv'] = [
    'type' => 'entity',
    'label' => t('Export as CSV'),
    'configurable' => FALSE,
    'behavior' => ['views_bulk_operations_field' => TRUE],
  ];
}

/**
 * VBO action callback for exporting entities as CSV.
 */
function custom_export_views_bulk_operations_export_csv($entity, $context = []) {
  if ($entity instanceof \Drupal\Core\Entity\EntityInterface) {
    // Customize this based on your entity type and export logic.
    $data = []; // Build an array of data to export.

    // Convert data to CSV format.
    $csv_content = '';
    foreach ($data as $row) {
      $csv_content .= implode(',', $row) . "\n";
    }

    // Create a batch for exporting.
    $batch = [
      'title' => t('Exporting entities as CSV'),
      'operations' => [
        [
          '_custom_export_batch_export',
          [$csv_content],
        ],
      ],
      'finished' => '_custom_export_batch_finished',
    ];

    batch_set($batch);
  }
}

/**
 * Batch operation: Export CSV content.
 */
function _custom_export_batch_export($csv_content, &$context) {
  $destination = 'public://exported_data.csv'; // Destination file path.
  $file = file_save_data($csv_content, $destination, FILE_EXISTS_REPLACE);
  $context['results'][] = $file;
}

/**
 * Batch operation finished callback.
 */
function _custom_export_batch_finished($success, $results, $operations) {
  if ($success) {
    // Download the exported CSV file.
    $file = reset($results);
    $response = new Response(file_get_contents($file->getFileUri()));
    $response->headers->set('Content-Type', 'text/csv');
    $response->headers->set('Content-Disposition', 'attachment; filename="exported_data.csv"');
    $response->headers->set('Cache-Control', 'no-cache, no-store, must-revalidate');
    $response->headers->set('Pragma', 'no-cache');
    $response->headers->set('Expires', '0');

    // Replace the content with the download.
    $response = new AjaxResponse();
    $response->addCommand(new ReplaceCommand(NULL, $response->getContent()));

    // Attach the response to the current render context.
    $render_context = new RenderContext();
    \Drupal::service('renderer')->executeInRenderContext($render_context, function () use ($response) {
      $response->send();
    });
  }
}