Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions modules/helfi_tpr_config/assets/hyte_services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"kasvatus-koulutus": [4213,4604,4641],
"kuva": [10002,10005,10028,10040,10041,10043,10049,10051,10053,10055,10057,10129,10133,10158,10167,10168,10207,10210,10221,10222,10223,10224,10225,10226,10227,10228,10229,10241,10247,10249,10250,10251,10253,10254,10255,10256,10256,10257,10258,10280,10281,10282,10295,10298,10304,10305,10306,10308,10309,10311,10312,10313,10321,10322,3209,3812,4148,4149,4151,4153,4157,4206,4208,4212,4213,4214,4215,4216,4217,7589,9218,9780,9934,9936,9945,9946,9947,9951,9952,9953,9954,9955,9956,9957,9959,9960,9961,9962,9964,9966,9968,9969,9970,9971,9972,9973,9974,9975,9976,9978,9979,9980,9981,9983,9986,9995,9997],
"terveys": [3250,3251,3253,3254,3255,3256,3258,3260,3261,3844,4418,7517,8927,8935,8936,8938,8939,8941,8942,9196],
"strategia": [10335]
}
77 changes: 77 additions & 0 deletions modules/helfi_tpr_config/helfi_tpr_config.install
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

declare(strict_types=1);

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface;
use Drupal\linkit\Entity\Profile;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
Expand Down Expand Up @@ -211,3 +214,77 @@ function helfi_tpr_config_update_9079(): void {
\Drupal::entityDefinitionUpdateManager()->uninstallFieldStorageDefinition($fieldStorageDefinition);
}
}

/**
* UHF-12809: Publish selected HYTE services.
*/
function helfi_tpr_config_update_11001(&$sandbox): ?FormattableMarkup {
$skip = new FormattableMarkup('Skipped helfi_tpr_config_update_11001().', []);
$modulePath = Drupal::service(ModuleExtensionList::class)->getPath('helfi_tpr_config');
$json = file_get_contents($modulePath . '/assets/hyte_services.json');

// Load a list of services from HYTE services JSON.
$services = json_decode($json, TRUE);

if (empty($services)) {
return $skip;
}

// Get current project.
try {
$project = Drupal::service(EnvironmentResolverInterface::class)
->getActiveProject()
->getName();
}
catch (\InvalidArgumentException) {
return $skip;
}

// Bail if current project is not found from the HYTE services JSON.
if (empty($project) || !array_key_exists($project, $services)) {
return $skip;
}

if (!isset($sandbox['current'])) {
$sandbox['current'] = 0;
$sandbox['total'] = count($services[$project]);
$sandbox['processed'] = 0;
}

$storage = Drupal::entityTypeManager()->getStorage('tpr_service');
$serviceIds = $services[$project];
$batchSize = 50;
$batchEnd = min($sandbox['current'] + $batchSize, $sandbox['total']);

// Process current batch.
for ($i = $sandbox['current']; $i < $batchEnd; $i++) {
$id = $serviceIds[$i];
/** @var \Drupal\helfi_tpr_config\Entity\Service $tprService */
$tprService = $storage->load($id);

if ($tprService) {
foreach ($tprService->getTranslationLanguages() as $language) {
/** @var \Drupal\helfi_tpr_config\Entity\Service $translation */
$translation = $tprService->getTranslation($language->getId());
$translation->setPublished()->save();
}
$sandbox['processed']++;
}
}

// Update progress.
$sandbox['current'] = $batchEnd;
$sandbox['#finished'] = $sandbox['current'] / $sandbox['total'];

// Return progress message if not finished.
if ($sandbox['#finished'] < 1) {
return new FormattableMarkup('Processed @processed/@total TPR services...', [
'@processed' => $sandbox['processed'],
'@total' => $sandbox['total'],
]);
}

return new FormattableMarkup('Published @num TPR services.', [
'@num' => $sandbox['processed'],
]);
}