diff --git a/custom/modules/emmr_util/transupdate.php b/custom/modules/emmr_util/transupdate.php new file mode 100644 index 0000000..0dab0d3 --- /dev/null +++ b/custom/modules/emmr_util/transupdate.php @@ -0,0 +1,74 @@ +condition('type', 'emmr_recipe') + ->accessCheck(FALSE)->execute(); + +// Load all the recipes. +$recipes = Node::loadMultiple($recipe_nids); +// Iterate. +foreach ($recipes as $recipe) { + // Get transcription value. + $trans = $recipe->field_recipe_transcription->getValue()[0]['value']; + // Find transcription item matches. + preg_match_all('/(.*?)<\/trxn>/', $trans, $matches); + // Iterate. + foreach ($matches[1] as $match) { + // If transcription insert... + if (str_contains($match, 'trxn-caret')) { + // Get text. + preg_match('/(.*?)<\/span>/', $match, $text); + $text = $text[1]; + // Build new transcription item. + $new = + "^$text"; + // Replace old item with new in transcription. + $trans = str_replace("$match", $new, $trans, $count); + } + // If transcription replace... + if (str_contains($match, 'trxn-retext')) { + // Get text. + preg_match('/(.*?)<\/s>/', $match, $text); + $text = $text[1]; + // Get replacement text. + preg_match('/(.*?)<\/span>/', $match, $retext); + $retext = $retext[1]; + // Build new transcription item. + $new = + "$retext$text"; + // Replace old item with new in transcription. + $trans = str_replace("$match", $new, $trans, $count); + } + // If transcription marginalia... + if (str_contains($match, 'trxn-number')) { + // Get number. + preg_match('/(.*?)<\/span>/', $match, $num); + $num = $num[1]; + // Get text. + preg_match('/(.*?)<\/span>/', $match, $text); + $text = $text[1]; + // Build new transcription item. + $new = + "$num$text"; + // Replace old item with new in transcription. + $trans = str_replace("$match", $new, $trans, $count); + } + } + if ($trans != $recipe->field_recipe_transcription->getValue()[0]['value']) { + $recipe->set('field_recipe_transcription', $trans); + $recipe->save(); + $title = $recipe->title->getValue()[0]['value']; + echo "\nUpdated emmr_recipe [$title]"; + } +} +echo "\n";