Skip to content

Commit

Permalink
UHF-8858: move update hook to drush command
Browse files Browse the repository at this point in the history
Update hook would not work, since the migration needs to be executed first.
Wouldn't the deploy fail the hook is not run immediately.
  • Loading branch information
hyrsky committed Oct 16, 2023
1 parent 4fb7e02 commit cedd378
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3129,4 +3129,60 @@ private function getListKey(string $endpoint): ?string {
return $key;
}

/**
* Fix policymaker node references to organizations.
*
* For some reason, the ahjo_decisionmakers:latest migration does not return
* all policymakers that exist in the production database.
*
* This command fixes the links and should be run on the production database
* once when UHF-8858 is deployed and the migration ahjo_organizations has
* been run.
*
* This should not be an issue once existing policymakers are treated, since
* new policymakers that are imported using the migration should get the
* reference automatically.
*
* @command ahjo-proxy:fix-decisionmaker-references.
*
* @usage ahjo-proxy:store-static-files
* Stores default static files into filesystem (for debugging migrations).
*/
private function fixPolicymakerReferences(): void {
$nodeStorage = \Drupal::entityTypeManager()
->getStorage('node');

// Load policymakers that are missing field_dm_organization.
$nids = $nodeStorage
->getQuery()
->accessCheck(FALSE)
->condition('type', 'policymaker')
->notExists('field_dm_organization')
->execute();

foreach ($nids as $nid) {
if (is_null($policymaker = $nodeStorage->load($nid))) {
continue;
}

$policymaker_id = $policymaker->get('field_policymaker_id')->getString();
$organizations = $nodeStorage->loadByProperties([
'type' => 'organization',
'field_policymaker_id' => $policymaker_id,
]);

if ($organization = reset($organizations)) {
// Set organization reference to policymaker.
$policymaker->set('field_dm_organization', $organization);
$policymaker->save();
}
else {
\Drupal::logger('paatokset_ahjo_proxy')->warning("Decisionmaker @dm: organization @id does not exist", [
'@dm' => $policymaker->toUrl('edit-form')->toString(),
'@id' => $policymaker_id,
]);
}
}
}

}

This file was deleted.

0 comments on commit cedd378

Please sign in to comment.