I have a large website with 240 urls that need to be updated. I would like to do this programmatically to avoid making errors. I created the following function which seems to work. There seems to be a good bit of complexity here for what I am trying to accomplish. Is there a simplier way to update the url of a node?
function alias_replace($path, $lang, $new_alias) {
$path_alias_manager = \Drupal::entityTypeManager()->getStorage('path_alias');
$alias_objects = $path_alias_manager->loadByProperties([
'path' => $path,
'langcode' => $lang
]);
foreach($alias_objects as $alias_object) {
$alias_object->delete();
}
$path_alias_manager->create([
'path' => $path,
'alias' => $new_alias,
'langcode' => $lang
])->save();
}