Skip to content

Commit

Permalink
Merge pull request #1020 from UN-OCHA/RW-1175
Browse files Browse the repository at this point in the history
[RW-1175] Set Other as default OCHA product
  • Loading branch information
orakili authored Feb 20, 2025
2 parents 9853273 + fbb6ef3 commit 5bdd228
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
10 changes: 7 additions & 3 deletions config/field.field.node.report.field_ocha_product.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ dependencies:
- field.storage.node.field_ocha_product
- node.type.report
- taxonomy.vocabulary.ocha_product
content:
- 'taxonomy_term:ocha_product:a3ba79f0-7af9-40d0-bb1a-bf1e4d80613f'
id: node.report.field_ocha_product
field_name: field_ocha_product
entity_type: node
Expand All @@ -14,7 +16,9 @@ label: 'OCHA product'
description: ''
required: false
translatable: false
default_value: { }
default_value:
-
target_uuid: a3ba79f0-7af9-40d0-bb1a-bf1e4d80613f
default_value_callback: ''
settings:
handler: any_term
Expand All @@ -23,7 +27,7 @@ settings:
ocha_product: ocha_product
sort:
field: name
direction: asc
auto_create: false
direction: ASC
auto_create: 0
auto_create_bundle: ''
field_type: entity_reference
64 changes: 64 additions & 0 deletions scripts/retagging/RW-1175.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

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

$proceed = TRUE;
$save = TRUE;

$terms = [
12350 => "Press Release",
12352 => "Statement/Speech",
12353 => "Other",
];

$nids = \Drupal::database()->query("
SELECT DISTINCT n.nid
FROM {node_field_data} AS n
INNER JOIN {node__field_ocha_product} AS fs
ON fs.entity_id = n.nid
WHERE fs.field_ocha_product_target_id IS NOT NULL
AND fs.field_ocha_product_target_id NOT IN (:terms[])
ORDER BY nid ASC
", [
":terms[]" => array_keys($terms),
])->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_ocha_product as $item) {
if (!isset($terms[$item->target_id])) {
$results[$node->bundle()][$item->target_id] = ($results[$node->bundle()][$item->target_id] ?? 0) + 1;
$item->target_id = 12353;
}
}

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

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

print_r($results);
}

0 comments on commit 5bdd228

Please sign in to comment.