function acquia_contenthub_audit_publisher in Acquia Content Hub 8
Checks published entities and compares them with Content Hub.
This method also republishes entities if they are not in sync with what exists currently in Content Hub.
Parameters
array $entities: An array of records from the tracking table.
bool $republish: 1 to republish entities, FALSE to just print.
mixed $context: The context array.
Return value
mixed|false Drush Output.
2 string references to 'acquia_contenthub_audit_publisher'
- AcquiaContenthubCommands::contenthubAuditPublisher in src/
Commands/ AcquiaContenthubCommands.php - Checks published entities and republishes them to Content Hub.
- drush_acquia_contenthub_audit_publisher in ./
acquia_contenthub.drush.inc - Audits exported entities and republishes if inconsistencies are found.
File
- ./
acquia_contenthub.module, line 322 - Contains acquia_contenthub.module.
Code
function acquia_contenthub_audit_publisher(array $entities, $republish, &$context) {
if (empty($context['sandbox'])) {
$context['results']['not_published'] = !empty($context['results']['not_published']) ? $context['results']['not_published'] : 0;
$context['results']['outdated'] = !empty($context['results']['outdated']) ? $context['results']['outdated'] : 0;
}
/** @var \Drupal\acquia_contenthub\Client\ClientManager $client_manager */
$client_manager = \Drupal::service('acquia_contenthub.client_manager');
if (!$client_manager
->isConnected()) {
throw new \Exception(dt('The Content Hub client is not connected so no operations could be performed.'));
}
// Collect UUIDs.
$uuids = [];
foreach ($entities as $entity) {
$uuids[] = $entity->entity_uuid;
}
/** @var \Drupal\acquia_contenthub\EntityManager $entity_manager */
$entity_manager = \Drupal::service('acquia_contenthub.entity_manager');
/** @var \Acquia\ContentHubClient\Entity[] $ch_entities */
$ch_entities = $client_manager
->createRequest('readEntities', [
$uuids,
]);
foreach ($entities as $entity) {
$out_of_sync = FALSE;
$uuid = $entity->entity_uuid;
$ch_entity = isset($ch_entities[$uuid]) ? $ch_entities[$uuid] : FALSE;
if (!$ch_entity) {
// Entity does not exist in Content Hub.
Drush::output()
->writeln(dt('Entity not published: Entity Type = @type, UUID = @uuid, ID = @id, Modified = @modified', [
'@type' => $entity->entity_type,
'@uuid' => $entity->entity_uuid,
'@id' => $entity->entity_id,
'@modified' => $entity->modified,
]));
$out_of_sync = TRUE;
$context['results']['not_published']++;
}
elseif ($ch_entity && $entity->modified !== $ch_entity
->getModified()) {
// Entity exists in Content Hub but the modified flag does not match.
Drush::output()
->writeln(dt('Outdated entity: Entity Type = @type, UUID = @uuid, ID = @id, Modified (local) = @lmodified, Modified (remote) = @rmodified', [
'@type' => $entity->entity_type,
'@uuid' => $entity->entity_uuid,
'@id' => $entity->entity_id,
'@lmodified' => $entity->modified,
'@rmodified' => $ch_entity
->getModified(),
]));
$out_of_sync = TRUE;
$context['results']['outdated']++;
}
if ($out_of_sync) {
$entity_id = FALSE;
if ($republish) {
$drupal_entity = \Drupal::entityTypeManager()
->getStorage($entity->entity_type)
->load($entity->entity_id);
if ($drupal_entity) {
$entity_id = $drupal_entity
->id();
if ($entity_manager
->isEligibleEntity($drupal_entity)) {
/** @var \Drupal\acquia_contenthub\Controller\ContentHubEntityExportController $export_controller */
$export_controller = \Drupal::service('acquia_contenthub.acquia_contenthub_export_entities');
// Export Entities.
$export_controller
->exportEntities([
$drupal_entity,
]);
}
else {
// Entity is not eligible for exporting anymore.
Drush::output()
->writeln(dt('Entity exists in the tracking table but is no longer eligible for exporting: Entity Type = @type, UUID = @uuid, ID = @id, Modified (local) = @lmodified, Modified (remote) = @rmodified', [
'@type' => $entity->entity_type,
'@uuid' => $entity->entity_uuid,
'@id' => $entity->entity_id,
'@lmodified' => $entity->modified,
'@rmodified' => $ch_entity
->getModified(),
]));
}
}
}
else {
$entity_type = \Drupal::entityTypeManager()
->getStorage($entity->entity_type)
->getEntityType();
$table = $entity_type
->getBaseTable();
$id_col = $entity_type
->getKey("id");
$query = \Drupal::database()
->select($table)
->fields($table, [
$id_col,
]);
$query
->condition("{$table}.{$id_col}", $entity->entity_id);
$entity_id = $query
->execute()
->fetchField();
}
if (empty($entity_id)) {
// The drupal entity could not be loaded.
throw new \Exception(dt('This entity exists in the tracking table but could not be loaded in Drupal: Entity Type = @type, UUID = @uuid, ID = @id, Modified = @modified', [
'@type' => $entity->entity_type,
'@uuid' => $entity->entity_uuid,
'@id' => $entity->entity_id,
'@modified' => $entity->modified,
]));
}
}
}
}