In a custom module (association), I have two content entities (member and person).
A function _export_association() is called at the end of each hook_entity (insert, update, delete) in order to run the corresponding View that export as csv the content of the entity:

function _export_association($entityType)
  switch ($entityType) {
    case "member":
      _export_association_CSV('association_members', 'rest_export_1');
      _export_association_CSV('association_persons', 'rest_export_3');
    case "person":
      _export_association_CSV('association_persons', 'rest_export_1');
      _export_association_CSV('association_persons', 'rest_export_2');
  _export_association_CSV('association_members_and_persons', 'rest_export_1');

function _export_association_CSV($view, $page)
  $view = Views::getView($view);
  $path = $view->getPath();
  $data = $view->preview($page)['#markup'];
  file_unmanaged_save_data($data, 'private://' . $path, FILE_EXISTS_REPLACE);

When it is executed from CRUD forms, everything is OK.
For example, here is the first 3 lines of export_members.csv:


I have also a MemberImportStatus that allows to select a file with a list of members to run on each one an update, this being done using batch API.
And here is the problem! In that case, the export is not properly formatted.
The same export_members.csv is giving:


Any idea what is wrong?

Some details about the Views... The format they use is "Serializer" (from module csv_serialization) with 'csv' as 'Accepted request formats'.
As far as I understand, the 'wrong' format when the Views are run from batch appears to correspond to the 'json' request format...

as requested by @sonfd, my batch API code:

$fileId = $form_state->getValue('file_to_import')[0];
$file = File::load($fileId);
$filename = $file->filename->value;

$members = file('sites/default/files/_private/' . $filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$temp = array_shift($members);
$operations = [];
foreach ($members as $member) {
  $operations[] = ['\Drupal\association\Form\MemberImportStatus::updateMember', [$member]];

$batch = [
  'operations'       => $operations,
  'title'            => t('Members Status Import'),
  'init_message'     => t('Members status import is starting.'),
  'progress_message' => t('Processed @current out of @total. Estimated time: @estimate.'),
  'error_message'    => t('The importation process has encountered an error.'),
  'finished'         => '\Drupal\association\Form\MemberImportStatus::end_of_update',

with an example of input file:


and the code for operation:

public static function updateMember($member, &$context)
  $aTemp = explode(";", $member);
  $storage = \Drupal::entityTypeManager()->getStorage('member');
  $member = $storage->load($aTemp[0]);
  if ($member) {
    $member->set("status", $aTemp[1]);
    $member->set("enddate", $aTemp[2]);
    $context['results'][] = $aTemp[0];
    $context['message'] = t('Updating status for member @id', array('@id' => $aTemp[0]));

The purpose is to "mass"-update the entity (one member per batch operation).
The View that outputs results as a csv file is triggered by hook_entity, the same way it is when I update the entity thru CRUD form...

  • Maybe add your Batch API code that ultimately results in the issue?
    – sonfd
    Mar 13, 2021 at 13:37

1 Answer 1


Finally, I found this solution: I installed Views data export and I changed "Format" from 'Serializer' to 'Data export' for all the views that may be run from batch. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.