Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RW-829] Add script to retag MTI documents #677

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions scripts/retagging/RW-829.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @file
* Retagging script for RW-829.
*/

$proceed = TRUE;
$save = TRUE;

$sources = [
1545 => 2929,
];

$nids = \Drupal::database()->query("
SELECT DISTINCT n.nid
FROM {node_field_data} AS n
INNER JOIN {node__field_source} AS fs
ON fs.entity_id = n.nid
WHERE fs.field_source_target_id IN (:sources[])
ORDER BY nid ASC
", [
":sources[]" => array_keys($sources),
])->fetchCol();

$total = count($nids);

echo "Found " . $total . " nodes to update" . PHP_EOL;

if (!empty($proceed)) {
$storage = \Drupal::entityTypeManager()->getStorage("node");
$chunk_size = 100;
$now = time();
$progress = 1;

$results = [];
foreach (array_chunk($nids, $chunk_size) as $chunk) {
foreach ($storage->loadMultiple($chunk) as $node) {
foreach ($node->field_source as $item) {
if (isset($sources[$item->target_id])) {
$results[$node->bundle()][$item->target_id] = ($results[$node->bundle()][$item->target_id] ?? 0) + 1;
$item->target_id = $sources[$item->target_id];
}
}

$node->notifications_content_disable = TRUE;
$node->setRevisionLogMessage("Automatic retagging of source (Ref: RW-829).");
$node->setRevisionUserId(2);
$node->setRevisionCreationTime($now);
$node->setNewRevision(TRUE);
if ($save) {
$node->save();
}

echo "Progress: $progress / $total..." . PHP_EOL;
$progress++;
}
}

print_r($results);
}