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

VACMS-20371: Clean up extensions #20730

Merged
merged 8 commits into from
Mar 3, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,21 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
if (empty($original_extension)) {
return "No extension found for paragraph id $item";
}
$phone_parent_field_name = $phone_paragraph->get('parent_field_name')->value;
// Don't try to change 'field_phone_numbers_paragraph'.
if ($phone_parent_field_name === 'field_phone_numbers_paragraph') {
return "The phone data from 'field_phone_numbers_paragraph' '$item' on 'health_care_local_health_service' has already been migrated to the Service location paragraph previously. This is a vestigial field that is unused.";
}
$number_only_extension = $this->replaceNonNumerals($original_extension);
if ($original_extension === $number_only_extension) {
return "No change to extension $original_extension in paragraph id $item";
return "No change to extension '$original_extension' in paragraph id $item";
}
$phone_paragraph->set(name: 'field_phone_extension', value: $number_only_extension);
$phone_paragraph->save();
return "Extension updated for paragraph id $item from '$original_extension' to '$number_only_extension'";
}
catch (\Exception $e) {
$message = "Exception during update of paragraph id $item with extension: $original_extension";
$message = "Exception during update of paragraph id $item with extension: '$original_extension'";
return $message;
}

Expand All @@ -98,15 +103,15 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
*/
public static function replaceNonNumerals(string $extension): string {

$pattern_to_ignore = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/';
$pattern_to_ignore = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i';

// If the extension contains two numbers separate numbers, don't change it.
if (preg_match($pattern_to_ignore, $extension) > 0) {
return $extension;
}

// Remove non-numerical characters from the extension.
$just_numbers = preg_replace('/[^0-9]/', '', $extension);
$just_numbers = trim(preg_replace('/[^0-9]/', '', $extension));

return $just_numbers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
// Do the extension work.
$original_extension = $phone_paragraph->get('field_phone_extension')->value;
$separated_extensions = $this->splitExtensions($original_extension);
// There is something amiss with the extensions.
if (empty($separated_extensions)) {
return "Either no change is necessary for paragraph $item or '$original_extension' cannot be successfully separated.";
}
$original_label = $phone_paragraph->get('field_phone_label')->value;

try {
Expand All @@ -96,7 +100,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
}
$phone_paragraph->set(name: 'field_phone_extension', value: $separated_extensions[0]);
$phone_paragraph->save();
$message = "1st extension for paragraph $item changed from '$original_extension' to $separated_extensions[0]' .";
$message = "1st extension for paragraph $item changed from '$original_extension' to '$separated_extensions[0]'. ";
}

// Create the second extension and phone.
Expand All @@ -116,7 +120,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
$phone_parent_paragraph = \Drupal::entityTypeManager()->getStorage('paragraph')->load($phone_parent_id);
$phone_parent_paragraph->get($phone_parent_field_name)->appendItem($second_phone);
$phone_parent_paragraph->save();
$message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to $separated_extensions[1]'." . PHP_EOL;
$message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to '$separated_extensions[1]'.";
}
}
catch (\Exception $e) {
Expand All @@ -133,20 +137,25 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
* @param string $dual_extension
* The two-part extension.
*
* @return array
* @return array|bool
* Array with two extensions (or empty).
* E.g. '2132,2995' becomes ['2132','2995']
*/
public static function splitExtensions(string $dual_extension): array {
if (!preg_match('/[^0-9]/', $dual_extension)) {
$pattern = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i';
if (!preg_match($pattern, $dual_extension)) {
return [];
}
$first_extension = [];
preg_match('/^\d+/', $dual_extension, $first_extension);
$second_extension = [];
preg_match('/\d+$/', $dual_extension, $second_extension);
// If either of these are empty, we want to bail out of this.
if (empty($first_extension[0]) or empty($second_extension[0])) {
return [];
}

return [$first_extension[0], $second_extension[0]];
return [trim($first_extension[0]), trim($second_extension[0])];
}

}
Loading