You are here

public function ExportUser::executeMultiple in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  2. 8 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  3. 8.2 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  4. 8.3 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  5. 8.4 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  6. 8.5 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  7. 8.6 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  8. 8.7 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  9. 8.8 modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  10. 10.3.x modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  11. 10.0.x modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()
  12. 10.2.x modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php \Drupal\social_user_export\Plugin\Action\ExportUser::executeMultiple()

Executes the plugin for an array of objects.

Parameters

array $objects: An array of entities.

Overrides ViewsBulkOperationsActionBase::executeMultiple

3 calls to ExportUser::executeMultiple()
ExportEnrolments::executeMultiple in modules/social_features/social_event/modules/social_event_enrolments_export/src/Plugin/Action/ExportEnrolments.php
Execute action on multiple entities.
ExportMember::executeMultiple in modules/social_features/social_group/modules/social_group_members_export/src/Plugin/Action/ExportMember.php
Execute action on multiple entities.
ExportUser::execute in modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php
Executes the plugin.
2 methods override ExportUser::executeMultiple()
ExportEnrolments::executeMultiple in modules/social_features/social_event/modules/social_event_enrolments_export/src/Plugin/Action/ExportEnrolments.php
Execute action on multiple entities.
ExportMember::executeMultiple in modules/social_features/social_group/modules/social_group_members_export/src/Plugin/Action/ExportMember.php
Execute action on multiple entities.

File

modules/social_features/social_user_export/src/Plugin/Action/ExportUser.php, line 117

Class

ExportUser
Exports a user accounts to CSV.

Namespace

Drupal\social_user_export\Plugin\Action

Code

public function executeMultiple(array $entities) {

  // Check if headers exists.
  if (empty($this->context['sandbox']['results']['headers'])) {
    $headers = [];

    /** @var \Drupal\social_user_export\Plugin\UserExportPluginBase $instance */
    foreach ($this->pluginDefinitions as $plugin) {
      $instance = $this->userExportPlugin
        ->createInstance($plugin['id']);
      $headers[] = $instance
        ->getHeader();
    }
    $this->context['sandbox']['results']['headers'] = $headers;
  }

  // Create the file if applicable.
  if (empty($this->context['sandbox']['results']['file_path'])) {

    // Store only the name relative to the output directory. On platforms such
    // as Pantheon, different batch ticks can happen on different webheads.
    // This can cause the file mount path to change, thus changing where on
    // disk the tmp folder is actually located.
    $this->context['sandbox']['results']['file_path'] = $this
      ->generateFilePath();
    $file_path = $this
      ->getBaseOutputDirectory() . DIRECTORY_SEPARATOR . $this->context['sandbox']['results']['file_path'];
    $csv = Writer::createFromPath($file_path, 'w');
    $csv
      ->setDelimiter(',');
    $csv
      ->setEnclosure('"');
    $csv
      ->setEscape('\\');
    $csv
      ->insertOne($this->context['sandbox']['results']['headers']);
  }
  else {
    $file_path = $this
      ->getBaseOutputDirectory() . DIRECTORY_SEPARATOR . $this->context['sandbox']['results']['file_path'];
    $csv = Writer::createFromPath($file_path, 'a');
  }

  // Add formatter.
  $csv
    ->addFormatter([
    new CsvEncoder(),
    'formatRow',
  ]);

  // Now add the entities to export.
  foreach ($entities as $entity_id => $entity) {
    $row = [];

    /** @var \Drupal\social_user_export\Plugin\UserExportPluginBase $instance */
    foreach ($this->pluginDefinitions as $plugin) {
      $configuration = $this
        ->getPluginConfiguration($plugin['id'], $entity_id);
      $instance = $this->userExportPlugin
        ->createInstance($plugin['id'], $configuration);
      $row[] = $instance
        ->getValue($entity);
    }
    $csv
      ->insertOne($row);
  }
  if ($this->context['sandbox']['current_batch'] * $this->context['sandbox']['batch_size'] >= $this->context['sandbox']['total']) {
    $data = @file_get_contents($file_path);
    $name = basename($this->context['sandbox']['results']['file_path']);
    $path = 'private://csv';
    if (\Drupal::service('file_system')
      ->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS) && file_save_data($data, $path . '/' . $name)) {
      $url = Url::fromUri(file_create_url($path . '/' . $name));
      $link = Link::fromTextAndUrl($this
        ->t('Download file'), $url);
      $this
        ->messenger()
        ->addMessage($this
        ->t('Export is complete. @link', [
        '@link' => $link
          ->toString(),
      ]));
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Could not save the export file.'), 'error');
      $this->logger
        ->error('Could not save the export file on: %name.', [
        '%name' => $name,
      ]);
    }
  }
}