Created
July 25, 2023 19:40
-
-
Save smulvih2/c3a406ecca47bf344fd2b36804c7d927 to your computer and use it in GitHub Desktop.
Drupal 9/10 Default Content Deploy Batch Export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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), | |
] | |
) | |
); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
my_module.commands: | |
class: \Drupal\my_module\Commands\DCDCommands | |
tags: | |
- { name: drush.command } |
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
A simple drush command to export entities in batch. Uses the
reference
Mode in DCD which exports entities with references.Suggest the following patches:
Example:
drush dcderb node page TRUE