JsonapiBatchHelper.php in Entity Share 8
File
modules/entity_share_client/src/JsonapiBatchHelper.php
View source
<?php
namespace Drupal\entity_share_client;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\entity_share_client\Entity\RemoteInterface;
class JsonapiBatchHelper {
public static function importEntityListBatch(RemoteInterface $remote, array $data, array &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = count($data);
}
$limit = 10;
$sub_data = array_slice($data, $context['sandbox']['progress'], $limit);
$jsonapi_helper = \Drupal::service('entity_share_client.jsonapi_helper');
$jsonapi_helper
->setRemote($remote);
$result_ids = $jsonapi_helper
->importEntityListData($sub_data);
$context['results'] = array_merge($context['results'], $result_ids);
$context['sandbox']['progress'] += count($sub_data);
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public static function importEntityListBatchBatchFinished($success, array $results, array $operations) {
if ($success) {
$message = new PluralTranslatableMarkup(count($results), 'One entity processed.', '@count entities processed.');
}
else {
$message = t('Finished with an error.');
}
drupal_set_message($message);
}
}