Skip to content

Instantly share code, notes, and snippets.

@smulvih2
Created July 25, 2023 19:40
Show Gist options
  • Save smulvih2/c3a406ecca47bf344fd2b36804c7d927 to your computer and use it in GitHub Desktop.
Save smulvih2/c3a406ecca47bf344fd2b36804c7d927 to your computer and use it in GitHub Desktop.
Drupal 9/10 Default Content Deploy Batch Export
<?php
namespace Drupal\my_module;
/**
* Class BatchService.
*/
class BatchService {
/**
* Batch process callback.
*
* @param int $id
* Id of the batch.
* @param string $operation_details
* Details of the operation.
* @param object $context
* Context for operations.
*/
public static function processDCDExportBatch($id, $entity_type, $bundle, $force, $entity_id, $count, &$context) {
// Create the DCD exporter service.
$exporter = \Drupal::service('default_content_deploy.exporter');
try {
// Set the entity type and bundle for export.
$exporter->setEntityTypeId($entity_type)->setEntityBundle($bundle);
// Set the node ID for export.
$exporter->setEntityIds([$entity_id]);
// Perform the export.
$exporter->setMode('reference')->setForceUpdate($force)->export();
}
catch (\Exception $e) {
// An error occurred during export.
\Drupal::logger('my_module')->error('Error exporting entities: @error', ['@error' => $e->getMessage()]);
}
// Add ID to results.
$context['results'][] = $id;
// Optional message displayed under the progressbar.
$percent_done = round(($id / $count) * 100, 2);
$context['message'] = t('Running Batch @id/@count - @percent%',
['@id' => $id, '@count' => $count, '@percent' => $percent_done]
);
}
/**
* Batch Finished callback.
*
* @param bool $success
* Success of the operation.
* @param array $results
* Array of results for post processing.
* @param array $operations
* Array of operations.
*/
public static function processDCDBatchFinished($success, array $results, array $operations) {
$messenger = \Drupal::messenger();
if ($success) {
// Here we could do something meaningful with the results.
// We just display the number of nodes we processed...
if (count($results) === 1) {
$messenger->addMessage(t('1 entity processed.'));
} else {
$messenger->addMessage(t('@count entities processed.', ['@count' => count($results)]));
}
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$messenger->addMessage(
t('An error occurred while processing @operation with arguments : @args[]',
[
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
]
)
);
}
}
}
<?php
namespace Drupal\my_module\Commands;
use Drush\Commands\DrushCommands;
/**
* Drush command file.
*/
class DCDCommands extends DrushCommands {
/**
* A custom Drush command to run default_content_deploy in batch operations.
*
* @command my_module:dcd-export-reference-batch
*
* @aliases dcderb
*/
public function DCDExportReferencesBatch($entity_type, $bundle, $force = FALSE) {
$this->logger('my_module')->notice('Exporting nodes in batch...');
// Get all entities for export.
$query = \Drupal::entityQuery($entity_type)->condition('type', $bundle);
$entity_ids = $query->execute();
$operations = [];
$num_operations = 0;
$batch_id = 1;
if (!empty($entity_ids)) {
foreach ($entity_ids as $key => $id) {
$operations[] = [
'\Drupal\my_module\BatchService::processDCDExportBatch',
[
$batch_id,
$entity_type,
$bundle,
$force,
$id,
count($entity_ids),
],
];
$batch_id++;
$num_operations++;
}
}
else {
$this->logger('my_module')->warning('There are no entities to export.');
}
$batch = [
'title' => t('Updating @num node(s)', ['@num' => $num_operations]),
'operations' => $operations,
'finished' => '\Drupal\my_module\BatchService::processDCDBatchFinished',
];
batch_set($batch);
drush_backend_batch_process();
$this->logger('my_module')->notice("Batch operations completed!");
}
}
services:
my_module.commands:
class: \Drupal\my_module\Commands\DCDCommands
tags:
- { name: drush.command }
@smulvih2
Copy link
Author

A simple drush command to export entities in batch. Uses the reference Mode in DCD which exports entities with references.
Suggest the following patches:

"drupal/default_content_deploy": {
  "3302464 - remove entities not supporting UUIDs": "https://www.drupal.org/files/issues/2022-08-08/dcd-remove-entities-without-uuid-support-3302464-2.patch",
  "3349952 - Export processed text references": "https://www.drupal.org/files/issues/2023-07-18/dcd-export-processed-text-references-3349952-3.patch",
  "3357503 - Allow users to configure which referenced entities to export": "https://www.drupal.org/files/issues/2023-05-01/default_content_deploy-referenced_entities_config-3357503-2.patch",
  "3368372 - Support book": "https://www.drupal.org/files/issues/2023-07-03/dcd-support-book-3368372-2.patch"
}

Example:
drush dcderb node page TRUE

@smulvih2
Copy link
Author

I have not found a good way to get DCD import working with batch. For now my workaround is to set the following in settings.php to get past the memory issue when importing large data sets.

if (PHP_SAPI === 'cli') {
  ini_set('memory_limit', '-1');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment