function processContentDirectoryBatch in Content Synchronization 8
Processes the content sync import batch
Parameters
$files: The batch content to persist.
$collection: files division - subfolder.
$operation: The type of import: create, update, delete.
array $context: The batch context.
1 string reference to 'processContentDirectoryBatch'
- ContentSync::submitForm in src/
Form/ ContentSync.php - Form submission handler.
File
- ./
content_sync.batch.inc, line 52
Code
function processContentDirectoryBatch($files, $collection, $operation, &$context) {
//Initialize Batch
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_number'] = 0;
$context['sandbox']['max'] = (int) count($files);
}
//Get the file to process
$file = $files[$context['sandbox']['progress']];
// Skip site.uuid as it is not a content entity.
if ($file == "site.uuid") {
$context['results']['errors'][] = t('File Skipped ') . $file . t(' - site uuid should not be changed');
$context['message'] = "File Skipped: " . $file;
$context['sandbox']['progress']++;
$operation = Null;
}
$directory = content_sync_get_content_directory('sync');
// Validate file YAML format
if (!empty($collection)) {
$file_path = $directory . "/" . str_replace(".", "/", $collection) . "/" . $file . ".yml";
}
else {
$file_path = $directory . "/" . $file . ".yml";
}
$info = pathinfo($file_path);
if (strtolower($info["extension"]) != "yml") {
$context['results']['errors'][] = t("File Skipped: ") . $file;
$context['message'] = "File Skipped: " . $file;
$context['sandbox']['progress']++;
}
else {
list($entity_type, $entity_bundle, $entity_uuid) = explode('.', $file);
// Skip superuser and current user -- Avoid batch errors for session lost.
// Get super user uuid
$current_user_uuid = \Drupal\user\Entity\User::load(\Drupal::currentUser()
->id())
->uuid();
if ($current_user_uuid == $entity_uuid) {
$context['results'][] = t('File Skipped ') . $file . t(' - current user info should not be updated');
$context['message'] = "File Skipped: " . $file;
$context['sandbox']['progress']++;
}
else {
// Get super admin uuid -- assuming it is id 1
$superadmin_user_uuid = \Drupal\user\Entity\User::load(1)
->uuid();
if ($superadmin_user_uuid == $entity_uuid) {
$context['results'][] = t('File Skipped ') . $file . t(' - super admin user info should not be updated');
$context['message'] = "File Skipped: " . $file;
$context['sandbox']['progress']++;
}
else {
if ($operation == "create" || $operation == "update") {
//Load the yml file and decode.
try {
$file_data = file_get_contents($file_path);
$data = Yaml::decode($file_data);
} catch (\Exception $e) {
$context['results']['errors'][] = t('Error: %message.', [
'%message' => $e
->getMessage(),
]);
$data = "";
}
//Verify that the uuid field is the same as the file name
if ($entity_uuid != $data['values'][0]['uuid'][0]['value'] || $entity_type != $data['entity_type'] || $entity_bundle != $data['bundle']) {
$context['results']['errors'][] = t('File Skipped ') . $file . t(" - Malformed file");
$context['message'] = "File Skipped: " . $file;
$context['sandbox']['progress']++;
}
else {
_content_sync_entity_to_db($data, $file, $context);
}
}
elseif ($operation == "delete") {
$context['results'][] = t("Content Entity Deleted: ") . $file;
$entityRepository = \Drupal::service('entity.repository');
$entity = $entityRepository
->loadEntityByUuid($entity_type, $entity_uuid);
if (!empty($entity)) {
$entity
->delete();
}
$context['message'] = "Entity Deleted: " . $file;
$context['sandbox']['progress']++;
}
}
}
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}