Skip to content

Instantly share code, notes, and snippets.

@paul121
Created August 18, 2022 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul121/543067101027e09b63462f4e3ffb5b4b to your computer and use it in GitHub Desktop.
Save paul121/543067101027e09b63462f4e3ffb5b4b to your computer and use it in GitHub Desktop.
Script for merging logs from two assets
<?php
use Drupal\asset\Entity\Asset;
use Drupal\log\Entity\Log;
// Debug flag, leave TRUE to not perform any actions.
$debug = TRUE;
#$debug = FALSE;
// Hosts to to not run this script on.
$denylist = [
//'farmos.ddev.site',
];
$host = \Drupal::request()->getHost();
if (in_array($host, $denylist)) {
log_message('Host in denylist.');
return;
}
print(PHP_EOL);
// Get the canonical and merge arguments.
$canonical_id = $extra[0];
$merge_id = $extra[1];
if (empty($canonical_id) || empty($merge_id)) {
log_message("Missing arguments. Need canonical id and merge id.", 'ERROR');
return;
}
// Load each asset.
$canonical_asset = Asset::load($canonical_id);
if (empty($canonical_asset)) {
log_message("Canonical asset ID '$canonical_id' not found.", 'ERROR');
return;
}
$canonical_label = $canonical_asset->label();
log_message("Found canonical asset $canonical_id: '$canonical_label'");
$merge_asset = Asset::load($merge_id);
if (empty($merge_asset)) {
log_message("Merge asset ID '$merge_id' not found.", 'ERROR');
return;
}
$merge_label = $merge_asset->label();
log_message("Found merge asset $merge_id: '$merge_label'");
// Load logs that reference the merge asset.
$log_ids = \Drupal::entityQuery('log')
->condition('asset.entity:asset.id', $merge_id)
->execute();
$logs = Log::loadMultiple($log_ids);
// Remove the merge asset id from each log and add the canonical asset id.
$log_count = count($logs);
log_message("$log_count logs need update:");
foreach ($logs as $log) {
/** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $asset_field */
$asset_field = $log->get('asset');
$log_assets = $asset_field->getValue();
// Find the existing asset_id.
$index = array_search($merge_id, array_column($log_assets, 'target_id'));
if (is_int($index)) {
// Remove the index.
$asset_field->removeItem($index);
}
// Add the canonical if doesn't already exist.
$index = array_search((int) $canonical_id, array_column($log_assets, 'target_id'));
if ($index === FALSE) {
$asset_field->appendItem($canonical_id);
}
// Finally, save the log.
$log_id = $log->id();
$log_name = $log->label();
if (!$debug) {
$log->save();
log_message("Updated log $log_id: '$log_name'", 'operation');
}
else {
log_message("Log $log_id: '$log_name'");
}
}
// Delete the merge asset if not debugging.
if (!$debug) {
try {
$merge_asset->delete();
log_message("Deleted asset $merge_id: '$merge_label'", 'operation');
} catch (Exception $e) {
log_message("Could not delete asset $merge_id: '$merge_label'. Reason: " . $e->getMessage(), 'error');
return;
}
}
print(PHP_EOL);
/**
* Helper function to print/log messages.
*
* @param $message
* The message to print.
* @param $level
* The message level prefix.
*/
function log_message($message, $level = 'info') {
$prefix = [
'info' => '',
'operation' => ' OP: ',
'warning' => 'WARNING: ',
'error' => 'ERROR: ',
];
print($prefix[$level] . $message . PHP_EOL);
}

Usage:

Run the script with two arguments, the canonical asset ID and the merge asset ID. All logs referencing the merge asset ID will be updated to reference the canonical asset ID and the merge asset will be deleted.

Set the $debug flag to TRUE to run the script without performing any operations.

To separate the script arguments from the drush scr arguments using --. eg: drush scr asset_merge.php -- 1 15

Example output:

Dry run:

$ drush scr asset_merge.php -- 1 15

Found canonical asset 1: 'corn 5/05/20'
Found merge asset 15: 'corn 5/05/20'
1 logs need update:
Log 16: 'Move corn 5/05/20 to Field 2'

Executing

$ drush scr asset_merge.php -- 1 15

Found canonical asset 1: 'corn 5/05/20'
Found merge asset 15: 'corn 5/05/20'
1 logs need update:
    OP: Updated log 16: 'Move corn 5/05/20 to Field 2'
    OP: Deleted asset 15: 'corn 5/05/20'

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