Last active
April 4, 2022 14:30
-
-
Save abarriosr/5f46675c437da4a61c6679b25af2f739 to your computer and use it in GitHub Desktop.
Bulk Import Script with pagination support. Allows massive import of entities from Content Hub.
This file contains hidden or 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 | |
/** | |
* @file | |
* Add entities from Content Hub to the Import Queue. | |
* | |
* Please locate this field in the 'scripts' directory as a sibling of docroot: | |
* <DOCROOT>/../scripts/ach-bulk-import.php | |
* | |
* To run the script, execute the drush command: | |
* $drush scr ../scripts/ach-bulk-import.php | |
* | |
* Make sure to enable the Import Queue before executing this script. | |
* | |
* Notes: | |
* | |
* 1) If you want to explicitly avoid importing a particular entity type, please | |
* add it to the list of $global_excluded_types. | |
* 2) By default importing includes all dependencies. To change this behavior | |
* change the variable $include_dependencies to FALSE. | |
* 3) You can decided whether to publish entities after importing them. To | |
* publish entities after importing, set variable $publishing_status to 1. | |
* Setting $publishing_status to 0 imports them as unpublished. | |
* 4) You can decide to use FIFO (first exported entities are imported first), | |
* or LIFO (last exported entities are imported first), according to the | |
* $fifo variable: $fifo = 1 uses FIFO, $fifo = 0 uses LIFO. | |
* 5) You can set the author of the nodes to be imported locally. Example: If | |
* you set the $uid = 1, it will import all nodes as administrator (author | |
* is administrator). Change it to specific UID to use as author. | |
*/ | |
use Drupal\acquia_contenthub\ContentHubEntityDependency; | |
use Drupal\Component\Serialization\Json; | |
// Global exclusion of entity types. | |
$global_excluded_types = [ | |
// 'redirect' => 'redirect', | |
]; | |
// Include importing dependencies. By default it is "TRUE". | |
$include_dependencies = TRUE; | |
// Determine if we want to publish imported entities or not. | |
// 1: Publish entities, 0: Do not publish. | |
$publishing_status = 1; | |
// If TRUE, it will import from the last page to the first (FIFO: first entities | |
// exported will be the first to import), otherwise will use LIFO (Last exported | |
// entities will be imported first). | |
$fifo = TRUE; | |
// Determine the author UUID for the nodes to be created. | |
$uid = 1; // administrator. | |
$user = \Drupal\user\Entity\User::load($uid); | |
$author = $user->uuid(); | |
/** @var \Drupal\acquia_contenthub\ContentHubEntitiesTracking $entities_tracking */ | |
$entities_tracking = \Drupal::service('acquia_contenthub.acquia_contenthub_entities_tracking'); | |
// Loading ClientManager to be able to execute requests to Content Hub and | |
// to check connection. | |
/** @var \Drupal\acquia_contenthub\Client\ClientManager $client_manager */ | |
$client_manager = \Drupal::service('acquia_contenthub.client_manager'); | |
$client = $client_manager->getConnection(); | |
// The ImportEntityManager Service allows to import entities. | |
/** @var \Drupal\acquia_contenthub\ImportEntityManager $import_manager */ | |
$import_manager = \Drupal::service("acquia_contenthub.import_entity_manager"); | |
// List all the 'dependent' entities type IDs. | |
$dependent_entity_type_ids = ContentHubEntityDependency::getPostDependencyEntityTypes(); | |
$excluded_types = array_merge($global_excluded_types, $dependent_entity_type_ids); | |
// Checks whether the import queue has been enabled. | |
$import_with_queue = \Drupal::config('acquia_contenthub.entity_config')->get('import_with_queue'); | |
if (!$import_with_queue) { | |
drush_user_abort('Please enable the Import Queue.'); | |
} | |
// Check if the site is connected to Content Hub. | |
if (!$client_manager->isConnected()) { | |
return; | |
} | |
$list = $client_manager->createRequest('listEntities', [[]]); | |
$total = floor($list['total'] / 1000) * 1000; | |
// Starting page. | |
$start = $fifo ? $total : 0; | |
// Step | |
$step = $fifo ? -1000 : 1000; | |
// Counter of queued entities. | |
$i = 0; | |
do { | |
// List all entities you want to import by modifying the $options array. | |
/* | |
* Example of how to structure the $options parameter: | |
* | |
* $options = [ | |
* 'type' => 'node', | |
* 'origin' => '11111111-1111-1111-1111-111111111111', | |
* 'filters' => [ | |
* 'status' => 1, | |
* 'title' => 'New*', | |
* 'body' => '/Boston/', | |
* ], | |
* ]; | |
* | |
*/ | |
$options = [ | |
'start' => $start, | |
]; | |
$list = $client_manager->createRequest('listEntities', [$options]); | |
foreach ($list['data'] as $entity) { | |
$i++; | |
// We do not want to import "dependent" entities. | |
// These 3 lines are not needed in this example, but if we are listing all | |
// entities, make sure to exclude dependent entities to be sent directly to | |
// the importRemoteEntity() method because you would not be sure if their | |
// host (parent) entity exist in the system yet. | |
if (in_array($entity['type'], $excluded_types)) { | |
drush_print("{$i}) Skipped entity type = {$entity['type']} , UUID = {$entity['uuid']} (Dependent or excluded entity type)"); | |
continue; | |
} | |
// Do not import the entity if it has been previously imported and has the | |
// same "modified" flag, which means there are no new updates on the entity. | |
if ($imported_entity = $entities_tracking->loadImportedByUuid($entity['uuid'])) { | |
if ($imported_entity->getModified() === $entity['modified']) { | |
drush_print("{$i}) Skipped entity type = {$entity['type']} , UUID = {$entity['uuid']} (Entity already imported)"); | |
continue; | |
} | |
} | |
// Add entity to import queue. | |
try { | |
$response = $import_manager->addEntityToImportQueue($entity['uuid'], $include_dependencies, $author, $publishing_status); | |
$status = Json::decode($response->getContent()); | |
if (!empty($status['status']) && $status['status'] == 200) { | |
drush_print("{$i}) Entity added to import queue: type = {$entity['type']} , UUID = {$entity['uuid']}"); | |
} | |
else { | |
drush_print("{$i}) ERROR: Cannot add entity to import queue: type = {$entity['type']} , UUID = {$entity['uuid']}"); | |
} | |
} catch (\Drupal\Core\Entity\EntityStorageException $ex) { | |
drush_print("{$i}) ERROR: Failed to add entity to import queue: type = {$entity['type']} , UUID = {$entity['uuid']} [{$ex->getMessage()}]"); | |
} | |
} | |
$start = $start + $step; | |
$exit_condition = $fifo ? $start >= 0 : $start <= $total; | |
} while ($exit_condition); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment