Skip to main content
added 1931 characters in body
Source Link
gbmapo
  • 1.1k
  • 17
  • 45

EDIT2:
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);
$file->delete();
$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',
];
batch_set($batch);

with an example of input file:

id;status;enddate
104;0;2014-12-31
205;0;2014-12-31
400;0;2014-12-31

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]);
    $member->save();
    $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...

EDIT2:
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);
$file->delete();
$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',
];
batch_set($batch);

with an example of input file:

id;status;enddate
104;0;2014-12-31
205;0;2014-12-31
400;0;2014-12-31

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]);
    $member->save();
    $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...

added 348 characters in body
Source Link
gbmapo
  • 1.1k
  • 17
  • 45

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');
      break;
    case "person":
      _export_association_CSV('association_persons', 'rest_export_1');
      _export_association_CSV('association_persons', 'rest_export_2');
      break;
    default:
  }
  _export_association_CSV('association_members_and_persons', 'rest_export_1');
}

function _export_association_CSV($view, $page)
{
  $view = Views::getView($view);
  $view->setDisplay($page);
  $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:

id,designation,addresssupplement,street,postalcode,city,country,telephone,status,startdate,enddate,contact_id,comment,owner_id,created,changed
4,Adhérent004,,,78700,,,,0,2010-01-31,2014-12-31,,,,,
6,Adhérent006,,,78300,,,,0,2010-01-31,2013-12-31,,,,,

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:

[{"id":[{"value":4}],"designation":[{"value":"Adh\u00e9rent004"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78700"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2014-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]},{"id":[{"value":6}],"designation":[{"value":"Adh\u00e9rent006"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78300"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2013-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]}

Any idea what is wrong?

EDIT:
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...

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');
      break;
    case "person":
      _export_association_CSV('association_persons', 'rest_export_1');
      _export_association_CSV('association_persons', 'rest_export_2');
      break;
    default:
  }
  _export_association_CSV('association_members_and_persons', 'rest_export_1');
}

function _export_association_CSV($view, $page)
{
  $view = Views::getView($view);
  $view->setDisplay($page);
  $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:

id,designation,addresssupplement,street,postalcode,city,country,telephone,status,startdate,enddate,contact_id,comment,owner_id,created,changed
4,Adhérent004,,,78700,,,,0,2010-01-31,2014-12-31,,,,,
6,Adhérent006,,,78300,,,,0,2010-01-31,2013-12-31,,,,,

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:

[{"id":[{"value":4}],"designation":[{"value":"Adh\u00e9rent004"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78700"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2014-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]},{"id":[{"value":6}],"designation":[{"value":"Adh\u00e9rent006"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78300"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2013-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]}

Any idea what is wrong?

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');
      break;
    case "person":
      _export_association_CSV('association_persons', 'rest_export_1');
      _export_association_CSV('association_persons', 'rest_export_2');
      break;
    default:
  }
  _export_association_CSV('association_members_and_persons', 'rest_export_1');
}

function _export_association_CSV($view, $page)
{
  $view = Views::getView($view);
  $view->setDisplay($page);
  $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:

id,designation,addresssupplement,street,postalcode,city,country,telephone,status,startdate,enddate,contact_id,comment,owner_id,created,changed
4,Adhérent004,,,78700,,,,0,2010-01-31,2014-12-31,,,,,
6,Adhérent006,,,78300,,,,0,2010-01-31,2013-12-31,,,,,

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:

[{"id":[{"value":4}],"designation":[{"value":"Adh\u00e9rent004"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78700"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2014-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]},{"id":[{"value":6}],"designation":[{"value":"Adh\u00e9rent006"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78300"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2013-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]}

Any idea what is wrong?

EDIT:
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...

Source Link
gbmapo
  • 1.1k
  • 17
  • 45

Why does rest export give bad result when executed in batch?

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');
      break;
    case "person":
      _export_association_CSV('association_persons', 'rest_export_1');
      _export_association_CSV('association_persons', 'rest_export_2');
      break;
    default:
  }
  _export_association_CSV('association_members_and_persons', 'rest_export_1');
}

function _export_association_CSV($view, $page)
{
  $view = Views::getView($view);
  $view->setDisplay($page);
  $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:

id,designation,addresssupplement,street,postalcode,city,country,telephone,status,startdate,enddate,contact_id,comment,owner_id,created,changed
4,Adhérent004,,,78700,,,,0,2010-01-31,2014-12-31,,,,,
6,Adhérent006,,,78300,,,,0,2010-01-31,2013-12-31,,,,,

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:

[{"id":[{"value":4}],"designation":[{"value":"Adh\u00e9rent004"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78700"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2014-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]},{"id":[{"value":6}],"designation":[{"value":"Adh\u00e9rent006"}],"addresssupplement":[],"street":[],"postalcode":[{"value":"78300"}],"city":[],"country":[],"telephone":[],"status":[{"value":0}],"startdate":[{"value":"2010-01-31"}],"enddate":[{"value":"2013-12-31"}],"contact_id":[],"comment":[],"owner_id":[],"created":[],"changed":[]}

Any idea what is wrong?